SSH之路 (二) SSH的配置 (2) 集成Struts2

来源:岁月联盟 编辑:zhuzhu 时间:2009-05-01

  上一篇已经讲了Spring2.5的配置,这章讲的就是怎么在Spring2.5上集成Struts.

  三,在Spring2.5 集成 Struts2

   3.1   修改现有的web.xml .  

  加上下面两段代码:

<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>  

   3.2  完整的web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   
    <!-- ###################################### -->
    <!-- ##########  Struts2  ################## -->
    <!-- ###################################### -->
   
    <!--
     * Struts2的主要的Filter,负责四个方面的功能:
     * (1)执行Actions
     * (2)清除ActionContext
     * (3)维护静态内容
     * (4)清除request生命周期内的XWork的interceptors
     * 另注:该过滤器应该过滤所有的请求URL。一般被设置为/*
     ************ -->
    <filter> 
        <filter-name>struts2</filter-name> 
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
    </filter>
   
    <!-- ###################################### -->
    <!-- ##########  Spring2  ################## -->
    <!-- ###################################### -->
   
    <!--
     * [ <context-param></context-param ] =>用来设定web站台的环境参数
     * [ <param-name></param-name> ]  (子元素)=> 用来指定参数的名称
     * [ <param-value></param-value> ] (子元素)=> 用来设定参数值
     * ************
     * 从类路径下加载spring的配置文件, 多个配置文件可以用逗号和空格区分
     * classpath:  关键字特指类路径下加载
     ******************** -->
   
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
   
    <!--
     * [<listener></listener>]=>用来设定监听接口
     * [<listener-class></listener-class>](子元素)=>定义Listener的类名称
     * *******
     * 负责启动spring的监听器
     * 它将引用处的上下文参数获得spring配置文件地址
     * 指定Spring提供的ContextLoaderListener Web 容器监听器,
     * 该监听器在web容器启动时自动运行并且根据ContextLoaderListener参数
     * 获取Spring配置文件,并启动Spring容器。
     ************** -->
       
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
   
   
   
   
    <filter-mapping> 
        <filter-name>struts2</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 
   
   
    <display-name>miziStudy</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

   3.3 在src下面新建struts.xml

SSH之路 (二) SSH的配置 (2) 集成Struts2

   3.4 修改struts.xml内容修改为

<?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>
    <!-- ############################# -->
    <!-- ############ 通过这个配置指定使用struts-plugin.xml中的StrutsSpringObjectFactory作为创建Action的工厂类################# -->
    <!-- ############################# -->
     <constant name= "struts.objectFactory" value="spring" />

</struts>

   3.5 导入struts2包

SSH之路 (二) SSH的配置 (2) 集成Struts2

  要注意的是 struts2-spring-plugin-2.0.11.1.jar 有了它,struts2就可以与spring整合了

   3.6 测试struts2 与spring 整合成功。

   OK,我们就来仿照之前我们测试spring的例子( 忽然发现我包名起的不好,之后我会专门列出一篇来讲命名规范,现在也只能将错就错了 )

   3.6.1 在src下面建一个叫test.action的包并创建一个叫Hello.java的文件

SSH之路 (二) SSH的配置 (2) 集成Struts2

   并修改Hello.java的内容如下:

package test.action;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class Hello extends ActionSupport {
    //获得表单参数值
    private String username;
    private int age;
    private String sex;
   
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

    private String hello;
   
    public void setHello(String hello) {
        this.hello = hello;
    }
    public String getHello() {
        return hello;
    }
    @Override
    public String execute() throws Exception {
        hello = "Hello:" username ";u is " age " age; and u is a " sex;
        return SUCCESS;
    }
}

   3.6.2  在application.xml加上bean:(把昨天做的例子删掉,要不tomcat启动不起来)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
       
        <!--
        * 这里bean的ID对应的是 struts.xml action的class
        ****************-->
        <bean id="Hello" class="test.action.Hello" scope="prototype"></bean>

</beans>

   3.6.3 修改struts.xml 内容:

<?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>
    <!-- ############################# -->
    <!-- ############ 通过这个配置指定使用struts-plugin.xml中的StrutsSpringObjectFactory作为创建Action的工厂类################# -->
    <!-- ############################# -->
     <constant name= "struts.objectFactory" value="spring" />

    <package name="mizi" namespace="" extends="struts-default">
     <!-- ############################# -->
    <!-- ############ 注意,现在action的class属性不再是类的名字了,而是在spring中的bean的id ################# -->
    <!-- ############################# -->
   
        <action name="Hello" class="Hello">
            <result>/hello.jsp</result>
        </action>
       
    </package>

</struts>

   3.6.4 在webContent目录下创建两个jsp页面

SSH之路 (二) SSH的配置 (2) 集成Struts2

   3.6.4.1 index.jsp

  <%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <body>
        <s:form action="Hello">
            <s:textfield name="username"  label="u name"/>
            <s:textfield name="age" label="u age" />
            <s:textfield name="sex" label="u sex" />
               <s:submit />
        </s:form>
    </body>
</html>    3.6.4.2   hello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <body>
        <s:property value="hello"/>
    </body>
</html>  

   3.6.5  在浏览器中输入 http://localhost/miziStudy

SSH之路 (二) SSH的配置 (2) 集成Struts2

  并在输入框输入信息, 注意age为整数

  如果你最后得出的结果和我一致,那么恭喜你,string struts 集成成功

SSH之路 (二) SSH的配置 (2) 集成Struts2

  OK , 今天到此为止, 下次我们将继续集成 Hibernate