首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2.1.6 与Spring2.5.6框架调整

2012-10-20 
Struts2.1.6 与Spring2.5.6框架整合1、在MyEclipse中新建项目(test)2、Struts包导入???暂时导入所必需的包,

Struts2.1.6 与Spring2.5.6框架整合

1、在MyEclipse中新建项目(test)
2、Struts包导入
???暂时导入所必需的包,其他包将在用到时导入:
???commons-fileupload-1.2.1.jar
???commons-logging-1.0.4.jar
???freemarker-2.3.13.jar
???ognl-2.6.11.jar
???struts2-core-2.1.6.jar
???xwork-2.1.2.jar
?3、复制在Struts目录的例子程序中WEB-INF\classes\struts.xml文件,粘贴到项目的src目录下,主要保留其文件头:

?1Struts2.1.6 与Spring2.5.6框架调整<?xml?version="1.0"?encoding="GBK"??>
?2Struts2.1.6 与Spring2.5.6框架调整<!DOCTYPE?struts?PUBLIC
?3Struts2.1.6 与Spring2.5.6框架调整????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
?4Struts2.1.6 与Spring2.5.6框架调整????"http://struts.apache.org/dtds/struts-2.0.dtd">
?5Struts2.1.6 与Spring2.5.6框架调整
?6Struts2.1.6 与Spring2.5.6框架调整<struts>
?7Struts2.1.6 与Spring2.5.6框架调整????<package?name="struts2"?extends="struts-default">
?8Struts2.1.6 与Spring2.5.6框架调整
?9Struts2.1.6 与Spring2.5.6框架调整????</package>
10Struts2.1.6 与Spring2.5.6框架调整</struts>


4、配置web.xml

?1Struts2.1.6 与Spring2.5.6框架调整<?xml?version="1.0"?encoding="UTF-8"?>
?2Struts2.1.6 与Spring2.5.6框架调整<web-app?version="2.5"?xmlns="http://java.sun.com/xml/ns/javaee"
?3Struts2.1.6 与Spring2.5.6框架调整????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?4Struts2.1.6 与Spring2.5.6框架调整????xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?
?5Struts2.1.6 与Spring2.5.6框架调整????http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
?6Struts2.1.6 与Spring2.5.6框架调整
?7Struts2.1.6 与Spring2.5.6框架调整????<filter>
?8Struts2.1.6 与Spring2.5.6框架调整????????<filter-name>struts2</filter-name>
?9Struts2.1.6 与Spring2.5.6框架调整????????<filter-class>
10Struts2.1.6 与Spring2.5.6框架调整????????????org.apache.struts2.dispatcher.FilterDispatcher
11Struts2.1.6 与Spring2.5.6框架调整????????</filter-class>
12Struts2.1.6 与Spring2.5.6框架调整????</filter>
13Struts2.1.6 与Spring2.5.6框架调整????<filter-mapping>
14Struts2.1.6 与Spring2.5.6框架调整????????<filter-name>struts2</filter-name>
15Struts2.1.6 与Spring2.5.6框架调整????????<url-pattern>/*</url-pattern>
16Struts2.1.6 与Spring2.5.6框架调整????</filter-mapping>
17Struts2.1.6 与Spring2.5.6框架调整
18Struts2.1.6 与Spring2.5.6框架调整????<welcome-file-list>
19Struts2.1.6 与Spring2.5.6框架调整????????<welcome-file>index.jsp</welcome-file>
20Struts2.1.6 与Spring2.5.6框架调整????</welcome-file-list>
21Struts2.1.6 与Spring2.5.6框架调整</web-app>

5、引入Spring包,在dist目录下
???spring.jar
6、在src目录下建立三个文件
???applicationContext-actions.xml
???applicationContext-beans.xml
???applicationContext-common.xml
???这三个文件其实是applicationContext.xml的分解,为的是避免所有配置放在同一文件,造成混乱。
???结构均如下:

?1Struts2.1.6 与Spring2.5.6框架调整<?xml?version="1.0"?encoding="GBK"?>
?2Struts2.1.6 与Spring2.5.6框架调整
?3Struts2.1.6 与Spring2.5.6框架调整<beans?xmlns="http://www.springframework.org/schema/beans"
?4Struts2.1.6 与Spring2.5.6框架调整????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?5Struts2.1.6 与Spring2.5.6框架调整????xmlns:context="http://www.springframework.org/schema/context"
?6Struts2.1.6 与Spring2.5.6框架调整????xmlns:tx="http://www.springframework.org/schema/tx"
?7Struts2.1.6 与Spring2.5.6框架调整????xmlns:aop="http://www.springframework.org/schema/aop"
?8Struts2.1.6 与Spring2.5.6框架调整????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
?9Struts2.1.6 与Spring2.5.6框架调整????????????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-2.5.xsd
10Struts2.1.6 与Spring2.5.6框架调整????????????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.5.xsd?
11Struts2.1.6 与Spring2.5.6框架调整????????????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
12Struts2.1.6 与Spring2.5.6框架调整
13Struts2.1.6 与Spring2.5.6框架调整</beans>


7、需要在web.xml进行配置

Struts2.1.6 与Spring2.5.6框架调整????<context-param>
Struts2.1.6 与Spring2.5.6框架调整????????<param-name>contextConfigLocation</param-name>
Struts2.1.6 与Spring2.5.6框架调整????????<param-value>classpath*:applicationContext-*.xml</param-value>
Struts2.1.6 与Spring2.5.6框架调整????</context-param>

?

Struts2.1.6 与Spring2.5.6框架调整????<listener>
Struts2.1.6 与Spring2.5.6框架调整????????<listener-class>
Struts2.1.6 与Spring2.5.6框架调整????????????org.springframework.web.context.ContextLoaderListener
Struts2.1.6 与Spring2.5.6框架调整????????</listener-class>
Struts2.1.6 与Spring2.5.6框架调整????</listener>

???前一段代码的配置是因为我们配置了后一段代码的配置后它默认是到WEB-INF下查找applicationContext.xml文件,我们现在改到src目录下并进行文件分解。

8、需要引入Struts2中的另一个包
???struts2-spring-plugin-2.1.6.jar
9、测试是否整合成功
(1)建立页面login.jsp、welcome.jsp、error.jsp分别为登录页面、登录成功页面、出错页面
login.jsp

?1Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整<%Struts2.1.6 与Spring2.5.6框架调整@?page?language="java"?contentType="text/html;?charset=GB18030"
?2Struts2.1.6 与Spring2.5.6框架调整????pageEncoding="GB18030"%>
?3Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整<%Struts2.1.6 与Spring2.5.6框架调整@?taglib?prefix="s"?uri="/struts-tags"%>
?4Struts2.1.6 与Spring2.5.6框架调整<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">
?5Struts2.1.6 与Spring2.5.6框架调整<html>
?6Struts2.1.6 与Spring2.5.6框架调整????<head>
?7Struts2.1.6 与Spring2.5.6框架调整????????<meta?http-equiv="Content-Type"?content="text/html;?charset=GB18030">
?8Struts2.1.6 与Spring2.5.6框架调整????????<title>登录页面</title>
?9Struts2.1.6 与Spring2.5.6框架调整????</head>
10Struts2.1.6 与Spring2.5.6框架调整????<body>
11Struts2.1.6 与Spring2.5.6框架调整????????<s:form?action="login"?method="post">
12Struts2.1.6 与Spring2.5.6框架调整????????????<s:textfield?name="username"?label="username"?/>
13Struts2.1.6 与Spring2.5.6框架调整????????????<s:password?name="password"?label="password"?/>
14Struts2.1.6 与Spring2.5.6框架调整????????????<s:submit?value="submit"?/>
15Struts2.1.6 与Spring2.5.6框架调整????????</s:form>
16Struts2.1.6 与Spring2.5.6框架调整????</body>
17Struts2.1.6 与Spring2.5.6框架调整</html>

welcome.jsp

?1Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整<%Struts2.1.6 与Spring2.5.6框架调整@?page?language="java"?contentType="text/html;?charset=GB18030"
?2Struts2.1.6 与Spring2.5.6框架调整????pageEncoding="GB18030"%>
?3Struts2.1.6 与Spring2.5.6框架调整<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">
?4Struts2.1.6 与Spring2.5.6框架调整<html>
?5Struts2.1.6 与Spring2.5.6框架调整????<head>
?6Struts2.1.6 与Spring2.5.6框架调整????????<meta?http-equiv="Content-Type"?content="text/html;?charset=GB18030">
?7Struts2.1.6 与Spring2.5.6框架调整????????<title>登录成功</title>
?8Struts2.1.6 与Spring2.5.6框架调整????</head>
?9Struts2.1.6 与Spring2.5.6框架调整????<body>
10Struts2.1.6 与Spring2.5.6框架调整????????用户名:${username?}
11Struts2.1.6 与Spring2.5.6框架调整????????<br>
12Struts2.1.6 与Spring2.5.6框架调整????????密码:${password?}
13Struts2.1.6 与Spring2.5.6框架调整????????<br>
14Struts2.1.6 与Spring2.5.6框架调整????</body>
15Struts2.1.6 与Spring2.5.6框架调整</html>


(2)建包com.test.manager和com.test.manager.impl分别存放业务逻辑处理的接口和其实现,分别建立接口LoginManager.java和其实现LoginManagerImpl.java
LoginManager.java

1Struts2.1.6 与Spring2.5.6框架调整package?com.test.manager;
2Struts2.1.6 与Spring2.5.6框架调整
3Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整public?interface?LoginManager?Struts2.1.6 与Spring2.5.6框架调整{
4Struts2.1.6 与Spring2.5.6框架调整????public?boolean?isLogin(String?username,?String?password);
5Struts2.1.6 与Spring2.5.6框架调整}

LoginManagerImpl.java,只是测试用,判断用户名密码是否为intrl、intrl,若是则登录成功

?1Struts2.1.6 与Spring2.5.6框架调整package?com.test.manager.impl;
?2Struts2.1.6 与Spring2.5.6框架调整
?3Struts2.1.6 与Spring2.5.6框架调整import?com.test.manager.LoginManager;
?4Struts2.1.6 与Spring2.5.6框架调整
?5Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整public?class?LoginManagerImpl?implements?LoginManagerStruts2.1.6 与Spring2.5.6框架调整{
?6Struts2.1.6 与Spring2.5.6框架调整????public?boolean?isLogin(String?username,?String?password)
?7Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整????Struts2.1.6 与Spring2.5.6框架调整{
?8Struts2.1.6 与Spring2.5.6框架调整????????if(null!=username&&null!=password&&"intrl".equals(username.trim())&&"intrl".equals(password.trim()))
?9Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整????????Struts2.1.6 与Spring2.5.6框架调整{
10Struts2.1.6 与Spring2.5.6框架调整????????????return?true;
11Struts2.1.6 与Spring2.5.6框架调整????????}
12Struts2.1.6 与Spring2.5.6框架调整????????return?false;
13Struts2.1.6 与Spring2.5.6框架调整????}
14Struts2.1.6 与Spring2.5.6框架调整}

(3)在applicationContext-beans.xml把实现类配置上,以让Spring进行管理

Struts2.1.6 与Spring2.5.6框架调整????<bean?id="loginManager"
Struts2.1.6 与Spring2.5.6框架调整????????class="com.test.manager.impl.LoginManagerImpl">
Struts2.1.6 与Spring2.5.6框架调整????</bean>

(4)创建包com.test.action用于存放action,并新建LoginAction.java,继承ActionSupport类
包含从页面所接收参数username、password,以及业务逻辑处理类LoginManager类型的loginManager,给username和password设置get、set,给loginManager设置set方法,以让Spring为我们自动注入;overwrite父类中的

?1Struts2.1.6 与Spring2.5.6框架调整package?com.test.action;
?2Struts2.1.6 与Spring2.5.6框架调整
?3Struts2.1.6 与Spring2.5.6框架调整import?com.opensymphony.xwork2.ActionSupport;
?4Struts2.1.6 与Spring2.5.6框架调整import?com.test.manager.LoginManager;
?5Struts2.1.6 与Spring2.5.6框架调整
?6Struts2.1.6 与Spring2.5.6框架调整@SuppressWarnings("serial")
?7Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整public?class?LoginAction?extends?ActionSupport?Struts2.1.6 与Spring2.5.6框架调整{
?8Struts2.1.6 与Spring2.5.6框架调整????private?LoginManager?loginManager;
?9Struts2.1.6 与Spring2.5.6框架调整????private?String?username;
10Struts2.1.6 与Spring2.5.6框架调整????private?String?password;
11Struts2.1.6 与Spring2.5.6框架调整
12Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整????public?String?getUsername()?Struts2.1.6 与Spring2.5.6框架调整{
13Struts2.1.6 与Spring2.5.6框架调整????????return?username;
14Struts2.1.6 与Spring2.5.6框架调整????}
15Struts2.1.6 与Spring2.5.6框架调整
16Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整????public?void?setUsername(String?username)?Struts2.1.6 与Spring2.5.6框架调整{
17Struts2.1.6 与Spring2.5.6框架调整????????this.username?=?username;
18Struts2.1.6 与Spring2.5.6框架调整????}
19Struts2.1.6 与Spring2.5.6框架调整
20Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整????public?String?getPassword()?Struts2.1.6 与Spring2.5.6框架调整{
21Struts2.1.6 与Spring2.5.6框架调整????????return?password;
22Struts2.1.6 与Spring2.5.6框架调整????}
23Struts2.1.6 与Spring2.5.6框架调整
24Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整????public?void?setPassword(String?password)?Struts2.1.6 与Spring2.5.6框架调整{
25Struts2.1.6 与Spring2.5.6框架调整????????this.password?=?password;
26Struts2.1.6 与Spring2.5.6框架调整????}
27Struts2.1.6 与Spring2.5.6框架调整
28Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整????public?void?setLoginManager(LoginManager?loginManager)?Struts2.1.6 与Spring2.5.6框架调整{
29Struts2.1.6 与Spring2.5.6框架调整????????this.loginManager?=?loginManager;
30Struts2.1.6 与Spring2.5.6框架调整????}
31Struts2.1.6 与Spring2.5.6框架调整????
32Struts2.1.6 与Spring2.5.6框架调整????@Override
33Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整????public?String?execute()?throws?Exception?Struts2.1.6 与Spring2.5.6框架调整{
34Struts2.1.6 与Spring2.5.6框架调整
35Struts2.1.6 与Spring2.5.6框架调整????????if(loginManager.isLogin(username,?password))
36Struts2.1.6 与Spring2.5.6框架调整Struts2.1.6 与Spring2.5.6框架调整????????Struts2.1.6 与Spring2.5.6框架调整{
37Struts2.1.6 与Spring2.5.6框架调整????????????return?SUCCESS;
38Struts2.1.6 与Spring2.5.6框架调整????????}
39Struts2.1.6 与Spring2.5.6框架调整????????return?INPUT;
40Struts2.1.6 与Spring2.5.6框架调整????}
41Struts2.1.6 与Spring2.5.6框架调整}

(5)在applicationContext-actions.xml中进行配置,也让Spring来管理LoginAction

Struts2.1.6 与Spring2.5.6框架调整????<bean?id="loginAction"?class="com.test.action.LoginAction"
Struts2.1.6 与Spring2.5.6框架调整????????scope="prototype">
Struts2.1.6 与Spring2.5.6框架调整????????<property?name="loginManager"?ref="loginManager"></property>
Struts2.1.6 与Spring2.5.6框架调整????</bean>

(6)在struts.xml中进行配置,处理页面提交的请求,配置action:login,login一定要和login.jsp中form的action属性名匹配。此时struts.xml文件如下:

?1Struts2.1.6 与Spring2.5.6框架调整<?xml?version="1.0"?encoding="GBK"??>
?2Struts2.1.6 与Spring2.5.6框架调整<!DOCTYPE?struts?PUBLIC
?3Struts2.1.6 与Spring2.5.6框架调整????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
?4Struts2.1.6 与Spring2.5.6框架调整?

热点排行