文檔版本 | 開發工具 | 測試平臺 | 工程名字 | 日期 | 作者 | 備註 |
---|---|---|---|---|---|---|
V1.0 | 2016.06.12 | lutianfei | none |
struts2框架
什麼是框架,框架有什麼用?
- 框架 是 實現部分功能的代碼 (半成品),使用框架簡化企業級軟件開發 ,提高開發效率。
- 學習框架 ,清楚的知道框架能做什麼? 還有哪些工作需要自己編碼實現 ?
什麼是struts2框架,它有什麼用?
- Struts 2是在 struts 1和WebWork的技術基礎上進行了合併的全新的Struts 2框架。
- 其全新的Struts 2的體系結構與Struts 1的體系結構差別巨大。Struts 2以WebWork爲核心
- struts2=struts1+webwork;
- struts2框架是apache產品。
- struts2是一個標準的mvc框架。
- javaweb中的model2模式就是一個mvc模式。 model2=servlet+jsp+javaBean
- struts2框架只能在
javaweb
開發中使用的。 - 使用struts2框架,可以簡化我們的web開發,並且降低程序的耦合度。
XWork—它是webwork核心,提供了很多核心功能:
- 前端攔截機(interceptor)
- 運行時表單屬性驗證
- 類型轉換
- 強大的表達式語言(OGNL – the Object Graph Navigation Language)
- IoC(Inversion of Control反轉控制)容器等
類似於struts2框架的產品 :
- struts1 webwork jsf springmvc
- ssh—struts2 spring hibernate
- ssi—springmvc spring ibatis
Strust2 核心功能
- 允許POJO(Plain Old Java Objects)對象 作爲Action
- Action的execute 方法不再與Servlet API耦合,更易測試
- 支持更多視圖技術(JSP、FreeMarker、Velocity)
- 基於Spring AOP思想的攔截器機制,更易擴展
- 更強大、更易用輸入校驗功能
- 整合Ajax支持
struts2快速入門
- index.jsp——>HelloServlet——–>hello.jsp web開發流程.
index.jsp——>HelloAction———>hello.jsp struts2流程
Struts2的下載和安裝
- http://struts.apache.org/download.cgi 下載Struts2 最新版
struts2的目錄結構:
- apps: 該文件夾包含了基於struts2 的示例應用,這些示例應用對於學習者是非常有用的;例子程序
war
後綴表示web壓縮文件 - docs : 該文件夾下包含了struts2 相關文檔,包括struts2 快速入門、struts2的文檔以及API文檔等
- lib : 該文件夾下包含了Struts2框架和核心類庫,以及struts2第三方插件類庫
- 開發時沒必要將lib目錄下jar文件全部複製到項目中
- src : 該文件夾下包含了Struts2框架的全部源代碼
- core 它是struts2的源代碼
- xwork-core struts2底層使用了xwork,xwork的源代碼
- apps: 該文件夾包含了基於struts2 的示例應用,這些示例應用對於學習者是非常有用的;例子程序
1.導入jar包
- 下載struts2的jar包 struts-2.3.15.1-all 版本.
- Struts運行必要jar包
- struts2-core-2.3.1.1.jar:Struts 2框架的核心類庫
- xwork-core-2.3.1.1.jar:Command模式框架,WebWork和Struts2都基於xwork
- ognl-3.0.3.jar:對象圖導航語言(Object Graph Navigation Language),struts2框架通過其讀寫對象的屬性
- freemarker-2.3.18.jar:Struts 2的UI標籤的模板使用FreeMarker編寫
- commons-logging-1.1.x.jar:ASF出品的日誌包,Struts 2框架使用這個日誌,包來支持Log4J和JDK 1.4+的日誌記錄。
- commons-fileupload-1.2.2.jar: 文件上傳組件,2.1.6版本後需要加入此文件
- commons-io-2.0.1.jar:傳文件依賴的jar包
- commons-lang-2.5.jar:對java.lang包的增強
- 開發中爲了方便導入,可以使用app/struts2-blank.war 攜帶jar包
注意:在struts2開發,一般情況下最少導入的jar包,去
apps
下的struts2-blank
示例程序中copy。將war
後綴改爲rar
後解壓。2.創建index.jsp,hello.jsp頁面
- 在index.jsp (發起請求頁面)
<a href="${pageContext.request.contextPath}/hello">第一次使用struts2</a>
- hello.jsp (結果頁面)
<h1>你好,Struts2<h1>
- 結果頁面顯示 struts2框架訪問成功
- 在index.jsp (發起請求頁面)
3.對struts2框架進行配置
- 1.web.xml文件中配置前端控制器(核心控制器)—–就是一個Filter
- 目的:是爲了讓struts2框架可以運行。
- 過濾器配置
/*
, 但是struts2 默認處理.action結尾請求,分發到相應Action類
- 1.web.xml文件中配置前端控制器(核心控制器)—–就是一個Filter
<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>
* 2.創建一個struts.xml配置文件 ,這個是struts2框架配置文件。
* 目的:是爲了struts2框架流程可以執行。
* 名稱:struts.xml
* 位置:src下(classes下)
- 4.創建一個HelloAction類
- 要求,在HelloAction類中創建一個返回值是String類型的方法,注意,無參數。
public class HelloAction {
public String say(){
System.out.println("hello world");
return "good"; // 結果頁面命名
}
}
struts2 的Action類似以前編寫的Servlet程序,可以處理用戶提交請求,但是Struts2的Action可以POJO對象
5.在struts.xml文件中配置HelloAction
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="cn.itcast.action.HelloAction"
method="say">
<result name="good">/hello.jsp</result>
</action>
</package>
- 6.在index.jsp中添加連接,測試
<a href="${pageContext.request.contextPath}/hello">第一次使用struts2</a>
- 在地址欄中輸入:http://localhost/struts2_day01/index.jsp 訪問連接,就可以看到
- HelloAction類中的say方法執行了,也跳轉到了hello.jsp.
Struts2 處理流程
- 對入門程序進行流程分析
模仿struts2流程完成入門程序
- index.jsp
- hello.jsp
- HelloAction
struts.xml
1.創建一個Filter—-StrutsFilter
- 2.在web.xml文件中配置StrutsFilter
<filter>
<filter-name>struts</filter-name>
<filter-class>cn.itcast.filter.StrutsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 3.在StrutsFilter中完成攔截操作,並訪問Action中的方法,跳轉到hello.jsp頁面操作.
// 2.1 得到請求資源路徑
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
String path = uri.substring(contextPath.length() + 1);
// System.out.println(path); // hello
// 2.2 使用path去struts.xml文件中查找某一個<action name=path>這個標籤
SAXReader reader = new SAXReader();
// 得到struts.xml文件的document對象。
Document document = reader.read(new File(this.getClass()
.getResource("/struts.xml").getPath()));
Element actionElement = (Element) document
.selectSingleNode("//action[@name='" + path + "']"); // 查找<action name='hello'>這樣的標籤
if (actionElement != null) {
// 得到<action>標籤上的class屬性以及method屬性
String className = actionElement.attributeValue("class"); // 得到了action類的名稱
String methodName = actionElement.attributeValue("method");// 得到action類中的方法名稱。
// 2.3通過反射,得到Class對象,得到Method對象
Class actionClass = Class.forName(className);
Method method = actionClass.getDeclaredMethod(methodName);
// 2.4 讓method執行.
String returnValue = (String) method.invoke(actionClass
.newInstance()); // 是讓action類中的方法執行,並獲取方法的返回值。
// 2.5
// 使用returnValue去action下查找其子元素result的name屬性值,與returnValue做對比。
Element resultElement = actionElement.element("result");
String nameValue = resultElement.attributeValue("name");
if (returnValue.equals(nameValue)) {
// 2.6得到了要跳轉的路徑。
String skipPath = resultElement.getText();
// System.out.println(skipPath);
request.getRequestDispatcher(skipPath).forward(request,
response);
return;
}
}
struts2的流程分析以及工具配置
1.流程分析
- 請求 –> StrutsPrepareAndExecuteFilter 核心控制器 –> Interceptors 攔截器(實現代碼功能 ) –> Action 的execute –> 結果頁面 Result
- 攔截器 在
struts-default.xml
定義 - 執行攔截器 是 defaultStack 中引用攔截器
2.關於手動配置struts.xml文件中提示操作
- 如果安裝Aptana編輯器 ,請不要用Aptana自帶xml編輯器 編寫struts2配置文件
- struts.xml提示來自於 DTD約束,
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
- 如果可以上網,自動緩存dtd,提供提示功能
- 如果不能上網,也可以配置本地DTD提示
- 提示配置說明
- dtd文件的名稱空間:http://struts.apache.org/dtds/struts-2.3.dtd
- 提示文件的路徑:\struts-2.3.15.1-all\struts-2.3.15.1\src\core\src\main\resources\struts-2.3.dtd
3.關聯struts2源文件
- 如果是com.opensymphony.xxx : 在xwork-core下
- 如果是org.apache.struts2 : 在core下
4.使用插件
struts2-config-browser-plugin-2.3.15.1
- 提供在瀏覽器中查看 struts2 配置加載情況
- 將解壓struts2/lib/struts2-config-browser-plugin-2.3.7.jar 複製WEB-INF/lib下
- 訪問 http://localhost/struts2_day01/config-browser/index.action 查看 struts2配置加載情況
struts2配置(重點)
1.struts2配置文件加載順序
- struts2框架要能執行,必須先加載
StrutsPrepareAndExecuteFilter
- 在StrutsPrepareAndExecuteFilter的init方法中對Dispatcher進行了初始化.
- 在Dispatcher類中定義的init方法內就描述了struts2配置文件加載的順序
init_DefaultProperties(); // [1] ---------- org/apache/struts2/default.properties
init_TraditionalXmlConfigurations(); // [2] --- struts-default.xml,struts-plugin.xml,struts.xml
init_LegacyStrutsProperties(); // [3] --- 自定義struts.properties
init_CustomConfigurationProviders(); // [5] ----- 自定義配置提供
init_FilterInitParameters() ; // [6] ----- web.xml
init_AliasStandardObjects() ; // [7] ---- Bean加載
1.default.properties文件
- 作用:定義了struts2框架中所有常量
- 位置: org/apache/struts2/default.properties
2.struts-default.xml
- 作用:配置了bean,interceptor,result等。
- 位置:在struts的core核心jar包.
- struts-plugin.xml
- 它是struts2框架中所使用的插件的配置文件。
struts.xml
- 我們使struts2所使用的配置文件。
3.自定義的struts.properties
- 就是可以自定義常量。
4.web.xml
- 可以理解爲由Struts2框架加載的。
在開發中,後加載文件中的配置會將先加載文件中的配置覆蓋。
- default.properties
- struts-default.xml
- struts.xml
2.關於Action的配置
1.
<package>
作用:是用於聲明一個包。用於管理action。- 1.name 它用於聲明一個包名,包名不能重複,也就是它是唯一的。
- 2.namespace 它與action標籤的name屬性合併確定了一個唯一訪問action的路徑。
- 3.extends 它代表繼承的包名。
- 4.abstrace 它可以取值爲true/false,如果爲true,代表這個包是用於被繼承的。
2
<action>
用於聲明一個action- 1.name 就是action的一個名稱,它是唯一的(在同包內) 它與package中的namespace確定了訪問action的路徑。
- 2.class Action類的全名
- 3.method 要訪問的Action類中的方法的名稱,方法無參數 ,返回值爲String.如果不寫,默認跳轉到
execute
函數。
3.
<result>
用於確定返回結果類型- 1.name 它與action中的method方法返回值做對比,確定跳轉路徑。
關於action配置其它細節
1.關於默認值問題
<package namespace="默認值">
namespace的默認值是<action class="默認值" method="默認值">
- class的默認值是 : com.opensymphony.xwork2.ActionSupport
- method的默認值是 : execute
<result name="默認值">
name的默認值是 “success”
2.關於訪問action的路徑問題
- 當action的配置是:
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="cn.itcast.action.DefaultAction">
<result>/hello.jsp</result>
</action>
</package>
* 此時輸入: http://localhost/struts2_day01_2/a/b/c/hello 也訪問到了action。
* 原因:struts2中的action被訪問時,它會首先查找
* 1.namespace="/a/b/c" action的name=hello 沒有.
* 2.namespace="/a/b action的name=hello 沒有
* 3.namespace="/a" action的name=hello 沒有
* 4.namespace="/" action的name=hello 查找到了.
* 如果最後也查找不到,會報404錯誤.
* 3.默認的action。
* 作用:處理其它action處理不了的路徑。
* `<default-action-ref name="action的名稱" />`
* 當訪問的路徑,其它的action處理不了時,就會執行name指定的名稱的action。
* 4.action的默認處理類
* 在action配置時,如果class不寫。默認情況下是 com.opensymphony.xwork2.ActionSupport
* `<default-class-ref class="cn.itcast.action.DefaultAction"/>`
* 如上設置,在當前包下,默認處理action請求的處理類就爲class指定的類。即:當`<action name="xxx" class="">`中class省略時,按照default-class-ref中的class設置認定對應的類。
關於常量配置
default.properties 它聲明瞭struts中的常量。
問題:人爲設置常量,可以在哪些位置設置 ?
- 1.struts.xml(應用最多)
<constant name="常量名稱" value="常量值"></constant>
- 2.struts.properties(基本不使用)
- 3.web.xml(瞭解)
- 配置常量,是使用
StrutsPrepareAndExecuteFilter的初始化參數
來配置的.
- 配置常量,是使用
- 1.struts.xml(應用最多)
<init-param>
<param-name>struts.action.extension</param-name>
<param-value>do,,</param-value>
</init-param>
常用常量(struts.xml)
struts.action.extension=action,,
: 這個常量用於指定strus2框架默認攔截的後綴名.<constant name="struts.i18n.encoding" value="UTF-8"/>
: 相當於request.setCharacterEncoding(“UTF-8”); 解決post請求亂碼<constant name="struts.serve.static.browserCache" value="false"/>
: false不緩存,true瀏覽器會緩存靜態內容,產品環境設置true、開發環境設置false<constant name="struts.devMode" value="true" />
: 提供詳細報錯頁面,修改struts.xml後不需要重啓服務器 (要求)
struts.xml文件的分離:
- 目的:就是爲了閱讀方便。可以讓一個模塊一個配置文件,在struts.xml文件中通過
<include file="test.xml"/>
導入其它的配置文件。
- 目的:就是爲了閱讀方便。可以讓一個模塊一個配置文件,在struts.xml文件中通過
Action
關於Action類的創建方式介紹
有三種方式
1.創建一個POJO類.
- 簡單的Java對象(Plain Old Java Objects):指的是沒有實現任何接口,沒有繼承任何父類(除了Object)
- 優點:無耦合。
- 缺點:所以工作都要自己實現。
在struts2框架底層是通過反射來操作
- struts2框架 讀取
struts.xml
獲得 完整Action類名 - obj = Class.forName(“完整類名”).newInstance();
- Method m = Class.forName(“完整類名”).getMethod(“execute”);
- m.invoke(obj); 通過反射 執行 execute方法
- struts2框架 讀取
2.創建一個類,實現Action接口
com.opensymphony.xwork2.Action
- 優點:耦合低。提供了五種結果視圖,定義了一個行爲方法。
- 缺點:所有工作都要自己實現。
- 爲了讓用戶開發的Action更加規範struts2提供了一個Action接口
- public static final String SUCCESS = “success”; // 數據處理成功 (成功頁面)
- public static final String NONE = “none”; // 頁面不跳轉 return null; 效果一樣
- public static final String ERROR = “error”; // 數據處理發送錯誤 (錯誤頁面)
- public static final String INPUT = “input”; // 用戶輸入數據有誤,通常用於表單數據校驗 (輸入頁面)
- public static final String LOGIN = “login”; // 主要權限認證 (登陸頁面)
3.創建一個類,繼承自
ActionSupport類
。com.opensymphony.xwork2.ActionSupport
- ActionSupport類實現了Action接口。
- 優點 : 表單校驗、錯誤信息設置、讀取國際化信息 三個功能都支持.
- 缺點 : 耦合度高。
- 在開發中,第三種會使用的比較多.
關於action的訪問方式
1.通過設置method的值,來確定訪問action類中的哪一個方法.
- 當訪問的是book_add,這時就會調用BookAction類中的add方法。
<action name="book_add" class="cn.itcast.action.BookAction" method="add"></action>
- 當訪問的是book_update,這時就會調用BookAction類中的update方法。
<action name="book_update" class="cn.itcast.action.BookAction" method="update"></action>
- 當訪問的是book_add,這時就會調用BookAction類中的add方法。
2.使用通配符來簡化配置
- 1.在struts.xml文件中
<action name="*_*" class="cn.itcast.action.{1}Action" method="{2}"></action>
- 2.在jsp頁面上
- book.jsp
- 1.在struts.xml文件中
<a href="${pageContext.request.contextPath}/Book_add">book add</a><br>
<a href="${pageContext.request.contextPath}/Book_update">book update</a><br>
<a href="${pageContext.request.contextPath}/Book_delete">book delete</a><br>
<a href="${pageContext.request.contextPath}/Book_search">book search</a><br>
* 當訪問book add時,這時的路徑是 Book_add,那麼對於struts.xml文件中.
* `*_*`代表匹配兩個字符串
* {1} 匹配UserAction 用於執行class
* {2} 匹配login用於指定method執行方法 和結果頁面
* 第一個星就是 Book
* 第二個星就是 add
* 對於{1}Action---->BookAction
* 對於method={2}--->method=add
* 使用通配符來配置注意事項:
* 1.必須定義一個統一的命名規範。
* 2.不建議使用過多的通配符,閱讀不方便。
- 3.動態方法調用 (瞭解)
- 通過url動態指定調用Action哪個方法而無需配置
<action>
的method屬性
- 通過
!方法名
指定調用Action哪個方法
- 通過url動態指定調用Action哪個方法而無需配置
struts.xml沒有指定method屬性,但
product!add.action
就會執行ProductAction的add方法eg:在struts.xml文件中
<action name="book" class="cn.itcast.action.BookAction"></action>
* 訪問時路徑: http://localhost/struts2_day01_2/book!add
* 就訪問到了BookAction類中的add方法。
* 對於`book!add` 這就是動態方法調用。
* 注意:struts2框架支持動態方法調用,是因爲在`default.properties`配置文件中設置了動態方法調用爲**true**.
* 第108行 `struts.enable.DynamicMethodInvocation = true`
在struts2框架中獲取servlet API
- 對於struts2框架,不建議直接使用servlet api;
在struts2中獲取servlet api有三種方式
1.通過ActionContext來獲取
- 1.獲取一個ActionContext對象。
ActionContext context=ActionContext.getContext();
:返回ActionContext實例對象
- 2.獲取servlet api
- 注意:通過ActionContext獲取的不是真正的Servlet api,而是一個Map集合。
- 1.context.getApplication() : 返回一個Map對象,存取ServletContext屬性
- 2.context.getSession() : 返回一個Map對象,存取HttpSession屬性
- 3.context.getParameter() : 得到的就相當於request.getParameterMap()
- 4.context.put(String,Object) : 相當於request.setAttribute(String,String);
- 5.context.get(key) 相當於 HttpServletRequest的getAttribute(String name)方法
- 6.context.setApplication(Map) 將該Map實例裏key-value保存爲ServletContext的屬性名、屬性值
- 7.setSession(Map) 將該Map實例裏key-value保持爲HttpSession的屬性名、屬性值
- 1.獲取一個ActionContext對象。
2.注入方式獲取(這種方式是真正的獲取到了servlet api)
1.要求action類必須實現指定接口。
- ServletContextAware : 注入ServletContext對象
- ServletRequestAware :注入 request對象
- ServletResponseAware : 注入response對象
2.重定接口中的方法。
- private HttpServletRequest request;
- 3.聲明一個web對象,使用接口中的方法的參數對聲明的web對象賦值.
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
- 擴展:分析其實現:
- 是使用struts2中的一個interceptor完成的.
<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
if (action instanceof ServletRequestAware) { //判斷action是否實現了ServletRequestAware接口
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); //得到request對象.
((ServletRequestAware) action).setServletRequest(request);//將request對象通過action中重寫的方法注入。
}
- 3.通過ServletActionContext獲取.
- 該方案可避免Action類實現XxxAware接口,但Action依然與Servlet API直接耦合
- 開發中優先使用ActionContext 這樣可以避免耦合
- 在ServletActionContext中方法都是static。
- ServletActionContext.getRequest() : 獲得request對象 (session)
- ServletActionContext.getResponse() : 獲得response 對象
- ServletActionContext.getServletContext() : 獲得ServletContext對象
- 靜態方法沒有線程問題,ThreadLocal
Result結果類型
Action處理請求後, 返回字符串(邏輯視圖名), Struts2 根據邏輯視圖名,決定響應哪個結果,需要在struts.xml 提供
<result>
元素定義結果頁面<result>
標籤屬性- 1.name 與action中的method的返回值匹配,進行跳轉.
- 2.type 作用:是用於定義跳轉方式
- 對於type屬性它的值有以下幾種:
- 在struts-default.xml文件中定義了type可以取的值
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
必會: chain dispatcher redirect redirectAction stream
dispatcher
:它代表的是請求轉發,也是默認值。它一般用於從action跳轉到頁面。該結果類型有一個 location 參數, 它是一個默認參數
- dispatcher 結果類型將把控制權轉發給應用程序裏的某個資源.
- dispatcher 結果類型不能把控制權轉發給一個外部資源. 若需要把控制權重定向到一個外部資源, 應該使用 redirect 結果類型
chain
:它也相當於請求轉發。它一般情況下用於從一個action跳轉到另一個action。redirect
:它代表的是重定向 它一般用於從action跳轉到頁面- redirect 結果類型接受下面這些參數:
- location: 用來給出重定向的目的地
- param: 用來表明是否把 location 參數的值視爲一個 OGNL 表達式來解釋. 默認值爲 true
- redirect 結果類型可以把響應重定向到一個外部資源
redirectAction
: 它代表的是重定向 它一般用於從action跳轉另一個action。
- actionName: 指定 “目的地” 動作的名字. 它是默認屬性
- namespace: 用來指定 “目的地” 動作的命名空間. 如果沒有配置該參數, Struts 會把當前 Action 所在的命名空間作爲 “目的地” 的命名空間
stream
:代表的是服務器端返回的是一個流,一般用於下載。
瞭解: freemarker velocity
- 局部結果頁面與全局結果頁面
<action name="result" class="cn.itcast.struts2.demo6.ResultAction">
<!-- 局部結果 當前Action使用 -->
<result name="success">/demo6/result.jsp</result>
</action>
<global-results>
<!-- 全局結果 當前包中 所有Action都可以用-->
<result name="success">/demo6/result.jsp</result>
</global-results>
作業
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>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="cn.itcast.action.LoginAction">
<result name="failer">/login.jsp</result>
<result type="redirect">/success.jsp</result>
</action>
</package>
</struts>
- LoginAction.java
<?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>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="cn.itcast.action.LoginAction">
<result name="failer">/login.jsp</result>
<result type="redirect">/success.jsp</result>
</action>
<action name="login1" class="cn.itcast.action.Login1Action">
<result name="failer">/login1.jsp</result>
<result type="redirect">/success.jsp</result>
</action>
<action name="login2" class="cn.itcast.action.Login2Action">
<result name="failer">/login2.jsp</result>
<result type="redirect">/success.jsp</result>
</action>
<action name="login3" class="cn.itcast.action.Login3Action">
<result name="failer">/login3.jsp</result>
<result type="redirect">/success.jsp</result>
</action>
<action name="list" class="cn.itcast.action.ListAction">
</action>
<action name="map" class="cn.itcast.action.MapAction">
</action>
</package>
</struts>
- login.jsp & success.jsp
//login.jsp
<html>
<head>
</head>
<body>
${requestScope["login.message"] }<br>
<form action="${pageContext.request.contextPath}/login" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="登錄">
</form>
</body>
</html>
//success.jsp
<html>
<head>
</head>
<body>
${username}
</body>
</html>