Struts2 入門 1

Struts是什麼

概念

Struts2是一個基於MVC設計模式的Web應用框架,它本質上至關於一個servlet,在MVC設計模式中,Struts2做爲控制器(Controller)來創建模型與視圖的數據交互。Struts 2是Struts的下一代產品,是在 struts 1和WebWork的技術基礎上進行了合併的全新的Struts 2框架。其全新的Struts 2的體系結構與Struts 1的體系結構差異巨大。Struts 2以WebWork爲核心,採用攔截器的機制來處理用戶的請求,這樣的設計也使得業務邏輯控制器可以與ServletAPI徹底脫離開,因此Struts 2能夠理解爲WebWork的更新產品。雖然從Struts 1到Struts 2有着太大的變化,可是相對於WebWork,Struts 2的變化很小。java

clipboard.png

優點

  • 自動封裝參數
  • 參數校驗
  • 結果的處理(轉發|重定向)
  • 國際化
  • 顯示等待頁面
  • 表單的防止重複提交

搭建Struts2框架

1.導包
在Struts2的zip包下的apps中找到struts2-blank.war,用解壓軟件打開,lib就是所須要的jar包web

clipboard.png

2.書寫Action類apache

package cn.zhli13.a_hello;

public class HelloAction {
    
    public String hello () {
        System.out.println("hello_word");
        return "success";
    }
}

3.書寫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="hello" namespace="/hello" extends="struts-default">
        <action name="HelloAction" class="cn.zhli13.a_hello.HelloAction" method="hello">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>

4.將Struts核心過濾器配置到web.xmlapi

<?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" 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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>strus2_demo</display-name>
  <!-- Struts核心過濾器 -->
  <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>

5.測試結果架構

clipboard.png

6.流程圖app

clipboard.png

struts2訪問流程&struts2架構

clipboard.png

配置詳解

struts.xml配置框架

<!-- i18n:國際化. 解決post提交亂碼 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 指定訪問action時的後綴名 
    http://localhost:8080/struts2_day01/hello/HelloAction.do
-->
<constant name="struts.action.extension" value="action"></constant>
<!-- 指定struts2是否以開發模式運行
        1.熱加載主配置.(不須要重啓便可生效)
        2.提供更多錯誤信息輸出,方便開發時的調試
 -->
<constant name="struts.devMode" value="true"></constant>
<!-- package:將Action配置封裝.就是能夠在Package中配置不少action.
        name屬性: 給包起個名字,起到標識做用.隨便起.不能其餘包名重複.
        namespace屬性:給action的訪問路徑中定義一個命名空間
        extends屬性: 繼承一個 指定包
        abstract屬性:包是否爲抽象的; 標識性屬性.標識該包不能獨立運行.專門被繼承
  -->
<package name="hello" namespace="/hello" extends="struts-default" >
    <!-- action元素:配置action類
            name屬性: 決定了Action訪問資源名.
            class屬性: action的完整類名
            method屬性: 指定調用Action中的哪一個方法來處理請求
     -->
    <action name="HelloAction" class="cn.itheima.a_hello.HelloAction" method="hello" >
        <!-- result元素:結果配置 
                name屬性: 標識結果處理的名稱.與action方法的返回值對應.
                type屬性: 指定調用哪個result類來處理結果,默認使用轉發.
                標籤體:填寫頁面的相對路徑
        -->
        <result name="success" type="dispatcher" >/hello.jsp</result>
    </action>
</package>
<!-- 引入其餘struts配置文件 -->
<include file="cn/zhli13/b_dynamic/struts.xml"></include>

2.struts常量配置jsp

struts2默認常量配置位置
clipboard.pngide

修改struts2常量配置(方式前後也是加載順序)

方式1:src/struts.xml(主要)
<!-- i18n:國際化. 解決post提交亂碼 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
方式2:在src下建立struts.properties
struts.i18n.encoding=UTF8
方式3:在項目的web.xml中
<context-param>
  <param-name>struts.i18n.encoding</param-name>
  <param-value>UTF-8</param-value>
</context-param>

3.struts2配置的進階
動態方法調用

方式1:

<!-- 配置動態方法調用是否開啓常量
        默認是關閉的,須要開啓
        訪問時方法名前要添加!符號
 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
方式2:
   <package name="dynamic" namespace="/dynamic" extends="struts-default" >
    <!-- 動態方法調用方式2:通配符方式
         使用{1} 取出第一個星號通配的內容
      -->
    <action name="Demo1Action_*" class="cn.zhli13.b_dynamic.Demo1Action" method="{1}" >
        <result name="success" >/hello.jsp</result>
    </action>
</package>

struts2中的默認配置

<package name="default" namespace="/default" extends="struts-default" >
        <!-- 找不到包下的action,會使用Demo2Action做爲默認action處理請求 -->
        <default-action-ref name="Demo2Action"></default-action-ref>
        <!-- method屬性:execute  -->
        <!-- result的name屬性:success  -->
        <!-- result的type屬性:dispatcher 轉發  -->
        <!-- class屬性:com.opensymphony.xwork2.ActionSupport -->
        <action name="Demo2Action"   >
            <result  >/hello.jsp</result>
        </action>
    </package>

Action類詳解

Action類的書寫方式

方式1
package cn..d_api;
//方式1: 建立一個類.能夠是POJO
//POJO:不用繼承任何父類.也不須要實現任何接口.
//使struts2框架的代碼侵入性更低.
public class Demo3Action {

}

//方式2: 實現一個接口Action
// 裏面有execute方法,提供action方法的規範.
// Action接口預置了一些字符串.能夠在返回結果時使用.爲了方便
import com.opensymphony.xwork2.Action;

public class Demo4Action implements Action {

    @Override
    public String execute() throws Exception {
        return null;
    }

}


//方式3: 繼承一個類.ActionSupport
// 幫咱們實現了 Validateable, ValidationAware, TextProvider, LocaleProvider .
//若是咱們須要用到這些接口的實現時,不須要本身來實現了.
import com.opensymphony.xwork2.ActionSupport;

public class Demo5Action  extends ActionSupport{

}
相關文章
相關標籤/搜索