Spring整合Struts2简单示例
1.SpringIntegrateStruts2Demo项目结构:
2.LoginAction.java源代码:
package com.xqh.action;import com.opensymphony.xwork2.ActionSupport;import com.xqh.service.MyService;public class LoginAction extends ActionSupport{//下面是用于封装用户请求参数的两个属性private String username;private String password;//用于封装处理结果的属性private String tip;//系统所用的业务逻辑组件private MyService ms;//设置注入业务逻辑组件所必需的setter方法public void setMs(MyService ms){this.ms = ms;}//username属性的setter和getter方法public void setUsername(String username){this.username = username;}public String getUsername(){return this.username;}//password属性所必需的setter和getter方法public void setPassword(String password){this.password = password;}public String getPassword(){return this.password;}//tip属性的getter和setter方法public void setTip(String tip){this.tip = tip;}public String getTip(){return this.tip;}//处理用户请求的execute方法public String execute() throws Exception{//调用业务逻辑组件的valid方法来//验证用户输入的用户名和密码是否正确if (ms.valid(getUsername(), getPassword())){setTip("Spring与Struts2整合成功!");return SUCCESS;}else{return ERROR;}}}
3.MyService.java源代码:
package com.xqh.service;public interface MyService {public boolean valid(String username, String pass);}
4.MyServiceImpl.java源代码:
package com.xqh.service;public class MyServiceImpl implements MyService{@Overridepublic boolean valid(String username, String pass) {if (username.equals("xqh") && pass.equals("123"))return true;return false;}}
5.struts.xml配置文件:
<?xml version="1.0" encoding="GBK"?><!-- 指定Struts2配置文件的DTD信息 --><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><!-- Struts2配置文件的根元素 --><struts><!-- 配置了系列常量 --><constant name="struts.i18n.encoding" value="GBK"/><package name="login" extends="struts-default"><!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类, 而是Spring容器中的Bean实例--><action name="login" class="loginAction"><!-- 为两个逻辑视图配置视图页面 --><result name="error">/error.jsp</result><result name="success">/welcome.jsp</result></action><!-- 让用户直接访问该应用时列出所有视图页面 --><action name=""><result>.</result></action></package></struts>
6.applicationContext.xml配置文件:
<?xml version="1.0" encoding="GBK"?><!-- 指定Spring配置文件的DTD信息 --><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN""http://www.springframework.org/dtd/spring-beans-2.0.dtd"><!-- Spring配置文件的根元素 --><beans><!-- 定义一个业务逻辑组件,实现类为com.xqh.service.MyServiceImpl --><bean id="myService" class="com.xqh.service.MyServiceImpl"/><!-- 让Spring管理的Action实例 --><bean id="loginAction" class="com.xqh.action.LoginAction"scope="prototype"><!-- 依赖注入业务逻辑组件 --><property name="ms" ref="myService"/></bean></beans>
注意:当Spring容器管理Struts2的Action时,由于每个Action对应一次用户请求,且封装了该请求的请求百状态信息,所以不应该将Action配置成单例模式,因些必须指定scope属性,该属性值可指定为prototype和request两种
7.web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- 使用ContextLoaderListener初始化Spring容器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 定义Struts2的FilterDispathcer的Filter --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><!-- FilterDispatcher用来初始化Struts2并且处理所有的WEB请求。 --><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
8.login.jsp源代码:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>登录页面</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> <form action="login.action" method="post"> <table align="center"> <caption><h3>用户登录</h3></caption> <tr> <td>用户名:<input type="text" name="username"/></td> </tr> <tr> <td>密 码:<input type="text" name="password"/></td> </tr> <tr align="center"> <td colspan="2"><input type="submit" value="登录"/><input type="reset" value="重填" /></td> </tr> </table></form> </body></html>
9.error.jsp源代码:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>错误页面</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> 登录失败! </body></html>
10.welcome.jsp源代码:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%@ taglib prefix="s" uri="/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>成功页面</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> 您已经登录!<br/><s:property value="tip"/> </body></html>