1. 程式人生 > >Struts2學習第一天——struts2基本流程與配置

Struts2學習第一天——struts2基本流程與配置

文件版本 開發工具 測試平臺 工程名字 日期 作者 備註
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的目錄結構:

    • apps: 該資料夾包含了基於struts2 的示例應用,這些示例應用對於學習者是非常有用的;例子程式war字尾表示web壓縮檔案
    • docs : 該資料夾下包含了struts2 相關文件,包括struts2 快速入門、struts2的文件以及API文件等
    • lib : 該資料夾下包含了Struts2框架和核心類庫,以及struts2第三方外掛類庫
      • 開發時沒必要將lib目錄下jar檔案全部複製到專案中
    • src : 該資料夾下包含了Struts2框架的全部原始碼
      • core 它是struts2的原始碼
      • xwork-core struts2底層使用了xwork,xwork的原始碼
  • 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框架訪問成功
  • 3.對struts2框架進行配置

    • 1.web.xml檔案中配置前端控制器(核心控制器)—–就是一個Filter
      • 目的:是為了讓struts2框架可以執行。
      • 過濾器配置/* , 但是struts2 預設處理.action結尾請求,分發到相應Action類
        <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>
    • 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提示
  • 提示配置說明
    • 提示檔案的路徑:\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下

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的初始化引數來配置的.
        <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"/>匯入其它的配置檔案。

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方法
  • 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>
  • 2.使用萬用字元來簡化配置

    • 1.在struts.xml檔案中
      • <action name="*_*" class="cn.itcast.action.{1}Action" method="{2}"></action>
    • 2.在jsp頁面上
      • book.jsp
    <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哪個方法

  • 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的屬性名、屬性值
  • 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>