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目錄下,主要保留其文件頭:
1<?xml version="1.0" encoding="GBK" ?>
2<!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6<struts>
7 <package name="struts2" extends="struts-default">
8
9 </package>
10</struts> 4、配置web.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<web-app version="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/javaee
5 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.FilterDispatcher
11 </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.jar
6、在src目錄下建立三個文件
applicationContext-actions.xml
applicationContext-beans.xml
applicationContext-common.xml
這三個文件其實是applicationContext.xml的分解,為的是避免所有配置放在同一文件,造成混亂。
結(jié)構(gòu)均如下:
1<?xml version="1.0" encoding="GBK"?>
2
3<beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
11 http://www.springframework.org/schema/aop http://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文件,我們現(xiàn)在改到src目錄下并進行文件分解。
8、需要引入Struts2中的另一個包
struts2-spring-plugin-2.1.6.jar
9、測試是否整合成功
(1)建立頁面login.jsp、welcome.jsp、error.jsp分別為登錄頁面、登錄成功頁面、出錯頁面
login.jsp
1<%@ page language="java" contentType="text/html; charset=GB18030"
2 pageEncoding="GB18030"%>
3<%@ taglib prefix="s" uri="/struts-tags"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6 <head>
7 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
8 <title>登錄頁面</title>
9 </head>
10 <body>
11 <s:form action="login" method="post">
12 <s:textfield name="username" label="username" />
13 <s:password name="password" label="password" />
14 <s:submit value="submit" />
15 </s:form>
16 </body>
17</html> welcome.jsp
1<%@ page language="java" contentType="text/html; charset=GB18030"
2 pageEncoding="GB18030"%>
3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4<html>
5 <head>
6 <meta http-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分別存放業(yè)務邏輯處理的接口和其實現(xiàn),分別建立接口LoginManager.java和其實現(xiàn)LoginManagerImpl.java
LoginManager.java
1package com.test.manager;
2
3public interface LoginManager {
4 public boolean isLogin(String username, String password);
5} LoginManagerImpl.java,只是測試用,判斷用戶名密碼是否為intrl、intrl,若是則登錄成功
1package com.test.manager.impl;
2
3import com.test.manager.LoginManager;
4
5public class LoginManagerImpl implements LoginManager{
6 public boolean isLogin(String username, String password)
7 {
8 if(null!=username&&null!=password&&"intrl".equals(username.trim())&&"intrl".equals(password.trim()))
9 {
10 return true;
11 }
12 return false;
13 }
14} (3)在applicationContext-beans.xml把實現(xiàn)類配置上,以讓Spring進行管理
<bean id="loginManager"
class="com.test.manager.impl.LoginManagerImpl">
</bean> (4)創(chuàng)建包com.test.action用于存放action,并新建LoginAction.java,繼承ActionSupport類
包含從頁面所接收參數(shù)username、password,以及業(yè)務邏輯處理類LoginManager類型的loginManager,給username和password設(shè)置get、set,給loginManager設(shè)置set方法,以讓Spring為我們自動注入;overwrite父類中的
1package com.test.action;
2
3import com.opensymphony.xwork2.ActionSupport;
4import com.test.manager.LoginManager;
5
6@SuppressWarnings("serial")
7public class LoginAction extends ActionSupport {
8 private LoginManager loginManager;
9 private String username;
10 private String password;
11
12 public String getUsername() {
13 return username;
14 }
15
16 public void setUsername(String username) {
17 this.username = username;
18 }
19
20 public String getPassword() {
21 return password;
22 }
23
24 public void setPassword(String password) {
25 this.password = password;
26 }
27
28 public void setLoginManager(LoginManager loginManager) {
29 this.loginManager = loginManager;
30 }
31
32 @Override
33 public String execute() throws Exception {
34
35 if(loginManager.isLogin(username, password))
36 {
37 return SUCCESS;
38 }
39 return INPUT;
40 }
41} (5)在applicationContext-actions.xml中進行配置,也讓Spring來管理LoginAction
<bean id="loginAction" class="com.test.action.LoginAction"
scope="prototype">
<property name="loginManager" ref="loginManager"></property>
</bean> (6)在struts.xml中進行配置,處理頁面提交的請求,配置action:login,login一定要和login.jsp中form的action屬性名匹配。此時struts.xml文件如下:
1<?xml version="1.0" encoding="GBK" ?>
2<!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6<struts>
7 <package name="struts2" extends="struts-default">
8 <action name="login" class="loginAction">
9 <result name="success">welcome.jsp</result>
10 <result name="input">login.jsp</result>
11 <result name="error">error.jsp</result>
12 </action>
13 </package>
14</struts> (7)此時目錄結(jié)構(gòu)如下:
(8)部署項目Tomcat服務器,啟動服務器,進入登錄頁面,進行登錄測試:
若用戶名密碼不為intrl/intrl,則登錄失敗,返回登錄頁面,注意地址欄的變化
若輸入intrl/intrl則登錄成功,跳至welcome.jsp頁面,顯示用戶名和密碼
10、以上結(jié)果證明,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/mysql\MYSQL123 5 5.0.zip:
http://www.rayfile.com/files/dee8bc17-66f1-11de-a255-0014221b798a/