`
梦想家dream
  • 浏览: 63230 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

struts2从零开始(七)

 
阅读更多
知识点:  struts2与sping的集成

    步骤一:在web.xml中添加如下代码(首先是一个struts的过滤器,然后是spring的ContextLoaderListener监听器)
   
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	
	<filter>  
        <filter-name>struts2</filter-name>  
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
    </filter>  
       
    <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
       
    <welcome-file-list>  
        <welcome-file>index.jsp</welcome-file>  
    </welcome-file-list>  
       
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
       
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



      步骤二:在src文件夹下建立一个struts.xml文件 配置action 但是要将action交给spring创建,这里通过引用spring bean的一个id与之对应(代码如下)

     
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC 
	"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	"http://struts.apache.org/dtds/struts-2.0.dtd">
	
	<struts>
    
    <constant name="objectFactory" value="spring"></constant>  
    <package name="struts2tutoial" extends="struts-default" namespace="/">  
        <action name="helloWorld" class="helloWorld">  
            <result name="success">/helloWorld.jsp</result>  
        </action>  
        
    </package>  
</struts>  

   步骤三:在appicationContext.xml中添加如下代码:

    
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

      <bean id="helloWorld" class="cn.zhuojingxinxi.web.HelloWorld"></bean> 

</beans>


       步骤四:编写action(代码如下)
     
package cn.zhuojingxinxi.web;

import com.opensymphony.xwork2.ActionSupport;


public class HelloWorld extends ActionSupport {
	
	  public final static String MESSAGE="struts is up and running";
	
	  public String execute(){
		  System.out.println("进来了");
		  setMessage(MESSAGE);
		  return ActionSupport.SUCCESS;
	  }
	  
	  private String message;

    	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	  

}


    步骤五:jsp页面代码:
    
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
         <h1>helloWorld jsp</h1>
         
         <h2><s:property value="message" /></h2>  
  
  </body>
</html>



   源码下载请点这里:
分享到:
评论

相关推荐

    struts2从零开始(一)

    NULL 博文链接:https://1136051009.iteye.com/blog/1768000

    struts2从零开始(六)

    Struts2从零开始系列教程旨在帮助初学者深入理解并掌握Struts2框架的应用与开发。在第六部分中,我们将探讨Struts2的核心概念、工作原理以及如何在实际项目中运用它。Struts2是一个流行的Java Web应用框架,用于构建...

    struts2从零开始(三)

    在"Struts2从零开始(三)"这个主题中,我们将会深入探讨Struts2的核心概念和实际应用,逐步了解如何使用Struts2来构建一个Web应用程序。 1. **MVC设计模式**:Struts2遵循Model-View-Controller(MVC)设计模式,...

    struts2从零开始(四)

    本教程将从零开始,帮助你深入理解并掌握Struts2的核心概念和实践技巧。 首先,我们要理解Struts2的核心架构。Struts2通过拦截器(Interceptor)机制实现了请求处理流程的控制,它允许开发者插入自定义的逻辑来扩展...

    struts2从零开始(二)

    在“Struts2从零开始(二)”这个主题中,我们将会深入探讨Struts2的核心概念和实践操作,以帮助初学者更好地理解和掌握这个框架。 首先,Struts2的核心在于它的Action类,它是业务逻辑处理的主要组件。Action类...

    struts2从零开始(五)

    本篇文章将从零开始,逐步介绍如何使用Struts2进行开发,带你走进Struts2的世界。 首先,让我们理解Struts2的核心概念。Struts2是一个轻量级、可扩展的MVC框架,它继承了Struts1的优点并解决了其不足,如性能问题和...

    Struts 从零开始

    这个"Struts 从零开始"的压缩包显然是为初学者提供的一套学习资源,包含了六个基础练习工程,帮助理解并实践 Struts 框架的关键概念和功能。 首先,让我们深入了解一下 Struts 框架的核心组件和原理: 1. **控制器...

    简述STRUTS2_Convention零配置

    从Struts2.1版本开始,推荐使用Convention插件替代Codebehind插件,因为它更加自动化,几乎无需手动配置。 Convention插件的工作原理是基于“约定优于配置”(Convention over Configuration)的原则,它会自动识别...

    struts2零配置个人整理文档

    ### Struts2零配置个人整理文档 #### 一、Convention插件详解 ##### 1. 设置结果页面路径 - **背景介绍**:Struts2框架提供了便捷的方式来处理Web请求,并返回响应视图。为了简化配置过程,Struts2提供了一个名为...

    如何使用struts2的零配置插件convention

    从Struts2.1版本开始,`Codebehind`插件被`Convetion`插件取代,以提供更全面的零配置支持。`Convetion`插件通过自动扫描和推理应用程序中的类和方法来配置Action和结果映射,不需要开发者显式地在`struts.xml`或...

    struts2资源包

    使用这个“struts2资源包”,开发者可以快速搭建一个基于Struts2的Web应用,无需从零开始配置所有的依赖。通过导入这些库,你可以实现动作类的定义、拦截器的设置、结果页面的跳转、表单验证、国际化、文件上传下载...

    从零开始创建基于struts1.2 + Hibernate3.0的Web工程

    2. 例子工程说明..........................................................3 3. 建立工程..............................................................4 3.1. 创建新的Web Project ..........................

    使用Struts2和jQuery EasyUI实现简单CRUD系统

    在本文中,我们将探讨如何使用Struts2和jQuery EasyUI框架创建一个简单的CRUD(创建、读取、更新、删除)系统。首先,我们需要理解这两个技术的基础概念。 Struts2是一个基于MVC(Model-View-Controller)架构的...

    struts2零配置入门代码

    在“struts2零配置入门代码”这个主题中,我们将深入探讨如何在不编写大量XML配置文件的情况下,启动并运行一个基本的Struts2应用程序。 Struts2的核心在于它的Action类,它是业务逻辑处理的主要组件。在“零配置”...

    struts2参考文档(word版)

    最后,文档可能包含了一些实战示例,引导读者从零开始搭建一个完整的Struts2项目,通过实际操作加深对理论知识的理解。 总的来说,这份Struts2参考文档全面覆盖了Struts2框架的主要概念和技术,对于想要学习或已经...

    Struts2权威指南完整版

    12. **实战案例**:通过实际项目示例,展示如何从零开始构建一个完整的Struts2应用,涵盖需求分析、设计、编码和测试的全过程。 通过阅读《Struts2权威指南》,开发者不仅可以掌握Struts2框架的使用,还能深入理解...

    轻松入门之struts2 3.0版

    10. **实战项目**:通过一个实际的小型项目,展示如何从零开始搭建一个基于Struts2 3.0的应用,涵盖了上述所有知识点的综合运用。 通过阅读“轻松入门之Struts2 3.0版”,开发者不仅能掌握Struts2的基本用法,还能...

    ajax与Struts2交互实例

    使用Struts2和jQuery EasyUI实现简单CRUD系统,从零开始,从基础的ajax与Struts2的交互开始。

Global site tag (gtag.js) - Google Analytics