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{
@Override
public 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>