Spring整合Struts2(1)

本文详细介绍如何将Spring框架与Struts2进行整合,包括配置步骤、关键代码示例及登录示例。通过具体实例展示了如何让Struts2的Action由Spring管理。

Spring整合Struts2的步骤
1、先配置运行一个Struts2工程。
* 拷贝相关jar包到lib目录下
* 拷贝struts.xml到src目录下
* 在web.xml中加入Struts2的拦截器
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、配置Spring:
* 相关jar包拷贝到lib目录下。
* 在src目录下创建Spring的配置文件:bean.xml
* 在web.xml文件中加入ContextLoaderListener,用来创建Spring容器:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:bean.xml</param-value>
</context-param>

<!-- 使用ContextLoaderListener初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
3、Struts2和Spring的整合
* 把struts2-spring-plugin-2.2.3.1.jar拷贝到lib路径下
* 把Struts2的Action的创建交给Spring
* 在Struts2的配置文件中配置Action时,class属性指定为Spring中生成的Action的bean ID。
注意:必须配置Struts2的Action的scope为prototype

具体的实例如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login">
用户名:<input type="text" name="username"><br/>
密 码:<input type="text" name="password"><br/>
<input type="submit" value="提交"> <input type="reset" value="重置"><br/>
</form>

</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="login.jsp">登陆页面</a><br/>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆成功!<br/>
你的用户名是${username },密码是${password }.
</body>
</html>

web .xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ss</display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:bean.xml</param-value>
</context-param>

<!-- 使用ContextLoaderListener初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

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

package cn.hnpi.lis.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private String username;
private String password;

public String login(){
if(username == null || password == null ||
username.trim().equals("") || password.trim().equals("")){
return LOGIN;
}else{
return SUCCESS;
}
}



public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}

}

bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">

<!-- 由Spring生成Struts2的Action -->
<bean id="loginAction" class="cn.hnpi.lis.action.LoginAction" scope="prototype"></bean>

</beans>

<?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="struts.devMode" value="false" />
<constant name="struts.i18n.encoding" value="UTF-8" />

<package name="default" namespace="/" extends="struts-default">
<!-- 没有Spring时的配置方式
<action name="login" class="cn.hnpi.lis.action.LoginAction" method="login">
<result name="success">login_success.jsp</result>
<result name="login">login.jsp</result>
</action>
-->

<!-- 由Spring创建Action -->
<action name="login" class="loginAction" method="login">
<result name="success">login_success.jsp</result>
<result name="login">login.jsp</result>
</action>
</package>


</struts>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值