Struts2.1.6與Spring2.5.6框架整合
<?xmlversion="1.0"encoding="GBK"?>2
<!DOCTYPEstrutsPUBLIC3
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"4
"http://struts.apache.org/dtds/struts-2.0.dtd">5
6
<struts>7
<packagename="struts2"extends="struts-default">8
9
</package>10
</struts>4、配置web.xml1
<?xmlversion="1.0"encoding="UTF-8"?>2
<web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee5
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">6
7
<filter>8
<filter-name>struts2</filter-name>9
<filter-class>10
org.apache.struts2.dispatcher.FilterDispatcher11
</filter-class>12
</filter>13
<filter-mapping>14
<filter-name>struts2</filter-name>15
<url-pattern>/*</url-pattern>16
</filter-mapping>17
18
<welcome-file-list>19
<welcome-file>index.jsp</welcome-file>20
</welcome-file-list>21
</web-app>5、引入Spring包,在dist目錄下spring.jar6、在src目錄下建立三個文件applicationContext-actions.xmlapplicationContext-beans.xmlapplicationContext-common.xml這三個文件其實是applicationContext.xml的分解,為的是避免所有配置放在同一文件,造成混亂。結構均如下:1
<?xmlversion="1.0"encoding="GBK"?>2
3
<beansxmlns="http://www.springframework.org/schema/beans"4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"5
xmlns:context="http://www.springframework.org/schema/context"6
xmlns:tx="http://www.springframework.org/schema/tx"7
xmlns:aop="http://www.springframework.org/schema/aop"8
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd9
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd10
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd11
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">12
13
</beans>7、需要在web.xml進行配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>前一段代碼的配置是因為我們配置了後一段代碼的配置後它默認是到WEB-INF下查找applicationContext.xml文件,我們現在改到src目錄下並進行文件分解。8、需要引入Struts2中的另一個包struts2-spring-plugin-2.1.6.jar9、測試是否整合成功(1)建立頁面login.jsp、welcome.jsp、error.jsp分別為登錄頁面、登錄成功頁面、出錯頁面login.jsp1
<%
@pagelanguage="java"contentType="text/html;charset=GB18030"2
pageEncoding="GB18030"%>3
<%
@taglibprefix="s"uri="/struts-tags"%>4
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">5
<html>6
<head>7
<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">8
<title>登錄頁面</title>9
</head>10
<body>11
<s:formaction="login"method="post">12
<s:textfieldname="username"label="username"/>13
<s:passwordname="password"label="password"/>14
<s:submitvalue="submit"/>15
</s:form>16
</body>17
</html>welcome.jsp1
<%
@pagelanguage="java"contentType="text/html;charset=GB18030"2
pageEncoding="GB18030"%>3
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">4
<html>5
<head>6
<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">7
<title>登錄成功</title>8
</head>9
<body>10
用戶名:${username}11
<br>12
密碼:${password}13
<br>14
</body>15
</html>(2)建包com.test.manager和com.test.manager.impl分別存放業務邏輯處理的介面和其實現,分別建立介面LoginManager.java和其實現LoginManagerImpl.javaLoginManager.java1
packagecom.test.manager;2
3
publicinterfaceLoginManager
{4
publicbooleanisLogin(Stringusername,Stringpassword);5
}LoginManagerImpl.java,只是測試用,判斷用戶名密碼是否為intrl、intrl,若是則登錄成功1
packagecom.test.manager.impl;2
3
importcom.test.manager.LoginManager;4
5
publicclassLoginManagerImplimplementsLoginManager
{6
publicbooleanisLogin(Stringusername,Stringpassword)7
{8
if(null!=username&&null!=password&&"intrl".equals(username.trim())&&"intrl".equals(password.trim()))9
{10
returntrue;11
}12
returnfalse;13
}14
}(3)在applicationContext-beans.xml把實現類配置上,以讓Spring進行管理
<beanid="loginManager"
class="com.test.manager.impl.LoginManagerImpl">
</bean>(4)創建包com.test.action用於存放action,並新建LoginAction.java,繼承ActionSupport類包含從頁面所接收參數username、password,以及業務邏輯處理類LoginManager類型的loginManager,給username和password設置get、set,給loginManager設置set方法,以讓Spring為我們自動注入;overwrite父類中的1
packagecom.test.action;2
3
importcom.opensymphony.xwork2.ActionSupport;4
importcom.test.manager.LoginManager;5
6
@SuppressWarnings("serial")7
publicclassLoginActionextendsActionSupport
{8
privateLoginManagerloginManager;9
privateStringusername;10
privateStringpassword;11
12
publicStringgetUsername()
{13
returnusername;14
}15
16
publicvoidsetUsername(Stringusername)
{17
this.username=username;18
}19
20
publicStringgetPassword()
{21
returnpassword;22
}23
24
publicvoidsetPassword(Stringpassword)
{25
this.password=password;26
}27
28
publicvoidsetLoginManager(LoginManagerloginManager)
{29
this.loginManager=loginManager;30
}31
32
@Override33
publicStringexecute()throwsException
{34
35
if(loginManager.isLogin(username,password))36
{37
returnSUCCESS;38
}39
returnINPUT;40
}41
}(5)在applicationContext-actions.xml中進行配置,也讓Spring來管理LoginAction
<beanid="loginAction"class="com.test.action.LoginAction"
scope="prototype">
<propertyname="loginManager"ref="loginManager"></property>
</bean>(6)在struts.xml中進行配置,處理頁面提交的請求,配置action:login,login一定要和login.jsp中form的action屬性名匹配。此時struts.xml文件如下:1
<?xmlversion="1.0"encoding="GBK"?>2
<!DOCTYPEstrutsPUBLIC3
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"4
"http://struts.apache.org/dtds/struts-2.0.dtd">5
6
<struts>7
<packagename="struts2"extends="struts-default">8
<actionname="login"class="loginAction">9
<resultname="success">welcome.jsp</result>10
<resultname="input">login.jsp</result>11
<resultname="error">error.jsp</result>12
</action>13
</package>14
</struts>
(7)此時目錄結構如下:
(8)部署項目Tomcat伺服器,啟動伺服器,進入登錄頁面,進行登錄測試:
若用戶名密碼不為intrl/intrl,則登錄失敗,返回登錄頁面,注意地址欄的變化
若輸入intrl/intrl則登錄成功,跳至welcome.jsp頁面,顯示用戶名和密碼
10、以上結果證明,Struts2.1.6與Spring2.5.6框架整合成功11、源代碼下載http://www.rayfile.com/files/b82cd5e3-66b2-11de-836f-0014221b798a/12、其他資源struts-2.1.6-all.zip:http://www.rayfile.com/files/d71417ae-66dd-11de-9d35-0014221b798a/spring-framework-2.5.6-with-dependencies.zip:http://www.rayfile.com/files/6819bd23-66e2-11de-ad79-0014221b798a/jdk-6u12-windows-i586-p.exe:http://www.rayfile.com/files/2879e173-66f0-11de-9cd8-0014221b798a/apache-tomcat-6.0.16.exe:http://www.rayfile.com/files/918febc7-66ed-11de-ab58-0014221b798a/mysqlMYSQL123 5 5.0.zip:http://www.rayfile.com/files/dee8bc17-66f1-11de-a255-0014221b798a/
posted on 2009-04-12 16:38 intrl 閱讀(1059) 評論(20) 編輯收藏 引用 所屬分類: Java 、Struts2 、Spring
推薦閱讀: