Spring整合Struts2簡單示例(轉)

 

Spring整合Struts2簡單示例

1.SpringIntegrateStruts2Demo項目結構:


2.LoginAction.java源代碼:

  1. package com.xqh.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. import com.xqh.service.MyService;  
  5.   
  6. public class LoginAction extends ActionSupport{  
  7.       
  8.     //下面是用於封裝用戶請求參數的兩個屬性   
  9.     private String username;  
  10.     private String password;  
  11.     //用於封裝處理結果的屬性   
  12.     private String tip;  
  13.     //系統所用的業務邏輯組件   
  14.     private MyService ms;  
  15.     //設置注入業務邏輯組件所必需的setter方法   
  16.     public void setMs(MyService ms)  
  17.     {  
  18.         this.ms = ms;  
  19.     }  
  20.     //username屬性的setter和getter方法   
  21.     public void setUsername(String username)  
  22.     {  
  23.         this.username = username;  
  24.     }  
  25.     public String getUsername()  
  26.     {  
  27.         return this.username;  
  28.     }  
  29.     //password屬性所必需的setter和getter方法   
  30.     public void setPassword(String password)  
  31.     {  
  32.         this.password = password;  
  33.     }  
  34.     public String getPassword()  
  35.     {  
  36.         return this.password;  
  37.     }  
  38.     //tip屬性的getter和setter方法   
  39.     public void setTip(String tip)  
  40.     {  
  41.         this.tip = tip;  
  42.     }  
  43.     public String getTip()  
  44.     {  
  45.         return this.tip;  
  46.     }  
  47.     //處理用戶請求的execute方法   
  48.     public String execute() throws Exception  
  49.     {  
  50.         //調用業務邏輯組件的valid方法來   
  51.         //驗證用戶輸入的用戶名和密碼是否正確   
  52.         if (ms.valid(getUsername(), getPassword()))  
  53.         {  
  54.             setTip("Spring與Struts2整合成功!");  
  55.             return SUCCESS;  
  56.         }  
  57.         else  
  58.         {  
  59.             return ERROR;  
  60.         }  
  61.     }  
  62. }  

3.MyService.java源代碼:

  1. package com.xqh.service;  
  2.   
  3. public interface MyService {  
  4.     public boolean valid(String username, String pass);  
  5. }  

4.MyServiceImpl.java源代碼:

  1. package com.xqh.service;  
  2.   
  3. public class MyServiceImpl implements MyService{  
  4.   
  5.     @Override  
  6.     public boolean valid(String username, String pass) {  
  7.         if (username.equals("xqh") && pass.equals("123"))  
  8.             return true;  
  9.         return false;  
  10.     }  
  11.       
  12. }  

5.struts.xml配置文件:

  1. <?xml version="1.0" encoding="GBK"?>  
  2. <!-- 指定Struts2配置文件的DTD信息 -->  
  3. <!DOCTYPE struts PUBLIC  
  4.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  5.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  6. <!-- Struts2配置文件的根元素 -->  
  7. <struts>  
  8.     <!-- 配置了系列常量 -->  
  9.     <constant name="struts.i18n.encoding" value="GBK"/>     
  10.     <package name="login" extends="struts-default">  
  11.         <!-- 定義處理用戶請求的Action,該Action的class屬性不是實際處理類  
  12.             , 而是Spring容器中的Bean實例-->  
  13.         <action name="login" class="loginAction">  
  14.             <!-- 爲兩個邏輯視圖配置視圖頁面 -->  
  15.             <result name="error">/error.jsp</result>  
  16.             <result name="success">/welcome.jsp</result>  
  17.         </action>  
  18.         <!-- 讓用戶直接訪問該應用時列出所有視圖頁面 -->  
  19.         <action name="">  
  20.             <result>.</result>  
  21.         </action>  
  22.     </package>  
  23. </struts>  

6.applicationContext.xml配置文件:

  1. <?xml version="1.0" encoding="GBK"?>  
  2. <!-- 指定Spring配置文件的DTD信息 -->  
  3. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"  
  4.     "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  5. <!-- Spring配置文件的根元素 -->  
  6. <beans>  
  7.     <!-- 定義一個業務邏輯組件,實現類爲com.xqh.service.MyServiceImpl -->  
  8.     <bean id="myService" class="com.xqh.service.MyServiceImpl"/>  
  9.     <!-- 讓Spring管理的Action實例 -->  
  10.     <bean id="loginAction" class="com.xqh.action.LoginAction"  
  11.         scope="prototype">  
  12.         <!-- 依賴注入業務邏輯組件 -->  
  13.         <property name="ms" ref="myService"/>  
  14.     </bean>  
  15. </beans>  
注意:當Spring容器管理Struts2的Action時,由於每個Action對應一次用戶請求,且封裝了該請求的請求百狀態信息,所以不應該將Action配置成單例模式,因些必須指定scope屬性,該屬性值可指定爲prototype和request兩種

7.web.xml配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>login.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.   <!-- 使用ContextLoaderListener初始化Spring容器 -->  
  11.     <listener>  
  12.         <listener-class>org.springframework.web.context.ContextLoaderListener  
  13.         </listener-class>  
  14.     </listener>  
  15.     <!-- 定義Struts2的FilterDispathcer的Filter -->  
  16.     <filter>  
  17.         <filter-name>struts2</filter-name>  
  18.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  19.     </filter>  
  20.     <!-- FilterDispatcher用來初始化Struts2並且處理所有的WEB請求。 -->  
  21.     <filter-mapping>  
  22.         <filter-name>struts2</filter-name>  
  23.         <url-pattern>/*</url-pattern>  
  24.     </filter-mapping>  
  25. </web-app>  

8.login.jsp源代碼:

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>登錄頁面</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!-- 
  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.     -->  
  21.   </head>  
  22.     
  23.   <body>  
  24.     <form action="login.action" method="post">  
  25.     <table align="center">  
  26.     <caption><h3>用戶登錄</h3></caption>  
  27.         <tr>  
  28.             <td>用戶名:<input type="text" name="username"/></td>  
  29.         </tr>  
  30.         <tr>  
  31.             <td>密  碼:<input type="text" name="password"/></td>  
  32.         </tr>  
  33.         <tr align="center">  
  34.             <td colspan="2"><input type="submit" value="登錄"/><input type="reset" value="重填" /></td>  
  35.         </tr>  
  36.     </table>  
  37. </form>  
  38.   </body>  
  39. </html>  

9.error.jsp源代碼:

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>錯誤頁面</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!-- 
  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.     -->  
  21.   </head>  
  22.     
  23.   <body>  
  24.     登錄失敗!  
  25.   </body>  
  26. </html>  

10.welcome.jsp源代碼:

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>成功頁面</title>  
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   </head>  
  23.     
  24.   <body>  
  25.     您已經登錄!<br/>  
  26.         <s:property value="tip"/>  
  27.   </body>  
  28. </html>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章