`
ccii
  • 浏览: 55037 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring整合Struts2

阅读更多
1.启动Spring容器
  对于使用Spring的Web应用,无须手动创建Spring容器,而是通过配置文件,声明式地创建Spring容器。其中有两种方式:一是直接在web.xml文件中配置创建Spring容器;二是利用三方MVC框架的扩展点,创建Spring容器。下面使用方法一:
    <!-- 使用ContextLoaderListener初始化Spring容器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    如果有多个Spring配置文件,则还要加上下面的配置
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 多个配置文件之间以逗号隔开,或者使用 classpath:test/application*.xml 全部一起引入(src/test下) -->
        <param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    如果需要在应用中获取ApplicationContext实例,则可以通过如下代码获取:
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); 当然也可以通过Servlet的getAttribute("WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE")方法获取ApplicationContext。

完整web.xml配置文件如下:
<?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">

    <!-- 使用ContextLoaderListener初始化Spring容器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <!-- 定义Struts 2的FilterDispathcer的Filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <!-- FilterDispatcher用来初始化Struts 2并且处理所有的WEB请求。 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

2. Action(LoginAction.java)

package test.action;

import com.opensymphony.xwork2.Action;
import test.service.*;

public class LoginAction implements Action {
    // 下面是用于封装用户请求参数的两个属性
    private String username;
    private String password;
    // 用于封装处理结果的属性
    private String tip;
    // 系统所用的业务逻辑组件
    private MyService ms;
    public void setMs(MyService ms) {
        this.ms = ms;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUsername() {
        return this.username;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getPassword() {
        return this.password;
    }
    public void setTip(String tip) {
        this.tip = tip;
    }
    public String getTip() {
        return this.tip;
    }
    // 处理用户请求的execute方法
    public String execute() throws Exception {
        // 调用业务逻辑组件的valid方法来,验证用户输入的用户名和密码是否正确
        if (ms.valid(getUsername(), getPassword())) {
            setTip("哈哈,整合成功!");
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
}

3. Struts配置文件(struts.xml)

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="GBK"/>   
    <constant name="struts.devMode" value="true"/>   
    <package name="lee" extends="struts-default">
        <!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类, 而是Spring容器中的Bean实例-->
        <action name="loginPro" class="loginAction">
            <result name="error">/content/error.jsp</result>
            <result name="success">/content/welcome.jsp</result>
        </action>
        <!-- 让用户直接访问该应用时列出所有视图页面 -->
        <action name="*">
            <result>/content/{1}.jsp</result>
        </action>
    </package>
</struts>

4. 业务处理Service(接口MyService.java 和 实现MyServiceImpl.java)

接口:

package test.service;
public interface MyService {
    boolean valid(String username, String pass);
}

实现:

package test.service.impl;
import test.service.*;
public class MyServiceImpl implements MyService {
    public boolean valid(String username, String pass) {
        // 此处只是简单示范,故直接判断用户名、密码是否符合要求
        if (username.equals("aaa") && pass.equals("sss")) {
            return true;
        }
        return false;
    }
}

5. Spring配置文件(applicationContext.xml)

<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="myService" class="test.service.impl.MyServiceImpl"/>
    <!-- 让Spring管理的Action实例 -->
    <bean id="loginAction" class="test.action.LoginAction" scope="prototype">
        <property name="ms" ref="myService"/>
    </bean>
</beans>

6. 测试页面

login.jsp:

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>登录页面</title></head>
<body>
<h3>用户登录</h3>
<s:form action="loginPro">
    <s:textfield name="username" label="用户名"/><s:textfield name="password" label="密码"/>
    <tr align="center"><td colspan="2"><s:submit value="登录" theme="simple"/><s:reset value="重设" theme="simple"/></td></tr>
</s:form>
</body>
</html>

error.jsp:

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>错误页面</title></head>
<body>
    您不能登录!
</body>
</html>

welcome.jsp:

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>错误页面</title></head>
<body>
    您不能登录!
</body>
</html>

7. 使用自动装配
  使用自动装配,即让Spring管理Bean与Bean之间的依赖关系,自动将业务逻辑组件注入Struts2的Action实例中。下面采用按name来完成自动装配,按name装配时无须设置任何的Struts2常量。只需要修改上例中的两个配置文件。

struts.xml:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="GBK"/>   
    <constant name="struts.devMode" value="true"/>   
    <package name="lee" extends="struts-default">
        <!-- 定义处理用户请求的Action,此处修改为原本的类路径-->
        <action name="loginPro" class="test.action.LoginAction">
            <result name="error">/content/error.jsp</result>
            <result name="success">/content/welcome.jsp</result>
        </action>
        <!-- 让用户直接访问该应用时列出所有视图页面 -->
        <action name="*">
            <result>/content/{1}.jsp</result>
        </action>
    </package>
</struts>

applicationContext.xml:

<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- 此处去掉原来Action类的Bean,让其自动装配。只需让业务Bean ID和Action类中的变量名相同 -->
    <bean id="ms" class="test.service.impl.MyServiceImpl"/>
</beans>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics