Struts2以WebWork優秀的設計思想為核心,吸收了Struts1的部分優點,建立了一個基於WebWork和Struts1的MVC框架。
2.1、通過官網下載最新版:http://struts.apache.org/download.cgi
建議下載struts-xx.all.zip包,壓縮包中不僅包含struts2的jar還包含了示例、源碼以及幫助文檔。
2.2、在項目中導入Struts2需要的jar包
2.3、修改web.xml文件
在web.xml文件的<web-app>節點下添加StrutsPrepareAndFilter核心過濾器,主要負責攔截用戶的請求,交給Struts2的框架進行處理。
2.4、添加struts.xml配置文件。
struts.xml是Struts2的核心配置文件,該文件通常放在src目錄下,編譯部署以後,他就到了應用程序的WEB-INF\classes目錄下。
3.1、新建web項目,並導入struts的jar包

3.2、添加Action類
實現Action可以有三種方法:
1.使用普通的Java類,編寫public String execute()方法
2.實現Action接口,實現execute()方法
3.繼承ActionSupport類,重寫execute()方法。
package com.able.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author 夏中偉
* 實現Action可以有三種方法:
* 1.使用普通的Java類,編寫public String execute()方法
* 2.實現Action接口,實現execute()方法
* 3.繼承ActionSupport類,重寫execute()方法。
* userAction這裡繼承ActionSupport
*/
public class UserAction extends ActionSupport {
private String name;
private String message;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
/**
* 這個方法名可以隨便寫
* @return
*/
public String execute(){
System.out.println(name+"-----------------");
message="Hello World"+name;
return "abc";
}
}
3.3、修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Struts2_Demo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.4、在src目錄下添加struts.xml,並添加相應的配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!-- 修改了Struts.xml需要重啟web服務配置才能生效,為了避免多次重啟web服務設置以下常量 -->
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/err.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<!-- 執行的是代理類 -->
<action name="userAction" class="com.able.action.UserAction" method="execute">
<result name="abc" >index.jsp</result> <!--type="forword"-->
</action>
<action name="login" class="com.able.action.LoginFieldAction">
<result name="success">index.jsp</result>
</action>
</package>
<!-- <include file="exemple.xml"></include> -->
<!-- Add packages here -->
</struts>
3.5、修改index.jps文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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="userAction">
用戶名:<input name="name"/><br/>
<input type="submit" value="提交"/>
</form>
<div>
<s:property value="message"/>
</div>
<div id="通過屬性傳值,以及modelDriven的接收">
剛剛輸入的用戶名:
<s:property value="username"/><br/>
<s:property value="password"/>
</div>
**********************分割線*************************
<div>
這是通過javaBean方式獲取的用戶名和密碼
<s:property value="user.username"/><br/>
<s:property value="user.password"/>
</div>
</body>
</html>
3.6、將項目添加到tomcat中啟動項目,在浏覽器中輸入:localhost:8080/Struts2_Demo(自己的項目名)/userAction
就可以看到頁面跳轉到index.jsp頁面,然後在文本框中輸入xiazhongwei提交後,在下邊輸出message,

4.1、在浏覽器中請求userAction時會經過Struts的核心過濾器“StrutsPrepareAndExecuteFilter”,
4.2、核心過濾器會根據請求在struts.xml中匹配name相同的action,然後會跳轉到相應action處理類userAction類,
4.3、會在userAction尋找與struts.xml中配置的<action method=“exectue”>標簽中屬性method相同值的方法,默認執行行方法名是execute()方法,如果在標簽中定義method="add",就回在userAction方法中尋求add的方法。
4.4、執行完execute()方法後根據返回值String,尋找匹配struts.xml中<action></action>標簽中的<resutl>值相同的result
4.5、根據類型跳轉相應的請求中,上示例定義跳轉到index.jsp中,所以會在頁面中看到userAction類中execute方法中返回的message值“Hello world ”+name,name是頁面提交表單中的值