一、Struts2下載
登錄http://struts.apache.org/download.cgi 下載Struts2最新版
建議下載Full Distribution完整版,該選項包括Struts2的示例應用(apps文件夾)、空示例應用、核心庫(lib文件夾)、源代碼和文檔(docs文件夾)等,src文件夾包含Struts2框架的全部源代碼。
二、Struts2安裝
將這幾個必須類庫複製到Web應用的WEB-INF/lib路徑下。如果需要在Web應用中使用Struts2的更多特性,則需要將相應的JAR文件複製到Web應用的WEB-INF/lib路徑下。
三、開發流程
1.在web.xml文件中定義核心Filter來攔截用戶請求。
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 定義Struts2的核心Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 讓Struts2的核心Filter攔截所有請求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.如果需要以POST方式提交請求,則定義包含表單數據的JSP頁面。如果僅僅只是以GET方式發送請求,則無須經過這一步。
3.定義處理用戶請求的Action類。比如我們做一個登錄demo需要一個LoginAction
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
//定義封裝請求參數的username和password屬性
private String username;
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
//定義處理用戶請求的execute方法
public String execute() throws Exception
{
//當username爲crazyit.org,password爲leegang時即登錄成功
if (getUsername().equals("struts")
&& getPassword().equals("struts") )
{
ActionContext.getContext().getSession()
.put("user" , getUsername());
return SUCCESS;
}
else
{
return ERROR;
}
}
}
4.配置Action以及處理結果與物理視圖資源之間的對應關係。
<struts>
<!-- 指定全局國際化資源文件 -->
<constant name="struts.custom.i18n.resources" value="mess"/>
<!-- 指定國際化編碼所使用的字符集 -->
<constant name="struts.i18n.encoding" value="GBK"/>
<!-- 所有的Action定義都應該放在package下 -->
<package name="lee" extends="struts-default">
<action name="login" class="org.crazyit.app.action.LoginAction">
<!-- 定義三個邏輯視圖和物理資源之間的映射 -->
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
5.編寫視圖資源
login.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login">
<s:textfield name="username" key="user"/>
<s:textfield name="password" key="pass"/>
<s:submit key="login"/>
</s:form>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><s:text name="errorPage"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
<s:text name="failTip"/>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><s:text name="succPage"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
<s:text name="succTip">
<s:param>${sessionScope.user}</s:param>
</s:text><br/>
</body>
</html>
6.編寫國際化資源文件
使用native2ascii轉換編碼
loginPage=\u767b\u5f55\u9875\u9762
errorPage=\u9519\u8bef\u9875\u9762
succPage=\u6210\u529f\u9875\u9762
failTip=\u5bf9\u4e0d\u8d77\uff0c\u60a8\u4e0d\u80fd\u767b\u5f55\uff01
succTip=\u6b22\u8fce\uff0c{0},\u60a8\u5df2\u7ecf\u767b\u5f55\uff01
user=\u7528\u6237\u540d
pass=\u5bc6 \u7801
login=\u767b\u5f55
即
loginPage=登錄頁面
errorPage=錯誤頁面
succPage=成功頁面
failTip=對不起,您不能登錄!
succTip=歡迎,{0},您已經登錄!
user=用戶名
pass=密 碼
login=登錄
四、運行結果