Spring整合Struts2简单示例(转)

来源:互联网 发布:ws2812灯软件 编辑:程序博客网 时间:2024/05/19 13:27
 

Spring整合Struts2简单示例

1.SpringIntegrateStruts2Demo项目结构:


2.LoginAction.java源代码:

view plaincopy to clipboardprint?
  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源代码:

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

4.MyServiceImpl.java源代码:

view plaincopy to clipboardprint?
  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配置文件:

view plaincopy to clipboardprint?
  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配置文件:

view plaincopy to clipboardprint?
  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配置文件:

view plaincopy to clipboardprint?
  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源代码:

view plaincopy to clipboardprint?
  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源代码:

view plaincopy to clipboardprint?
  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源代码:

view plaincopy to clipboardprint?
  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>