1. 程式人生 > >Struts 2 入門

Struts 2 入門

  1. 請說明為啥報找不到result name=input

相當於自定mvc中的action層。
com.zking.framework

配置檔案引入dtd不生效,無提示

  1. 環境搭建
    1.1 jar
    1.2 web.xml
    1.3 struts.xml
    struts.xml(核心配置檔案)

  2. 開發
    2.1 Action
    2.1.1 不需要指定父類(ActionSupport)
    2.1.2 業務方法的定義
    public String xxx();//execute
    2.1.3 Action是多例模式(注:在spring中的配置中一定要注意)
    Action用來接收引數

2.2 引數賦值
2.2.1 Action中定義屬性,並提供get/set方法
userName, getUserName/setUserName

2.2.2 ModelDriven
      返回實體,不能為null,不需要提供get/set方法
  
2.2.3 ModelDriven返回實體和Action中屬性重名,ModelDriven中優先順序更高
      注:ognl,ActionContext學完就知道了

2.3 與J2EE容器互動
2.3.1 非注入
2.3.1.2 耦合
ServletActionContext

  2.3.1.2 解耦(建立使用解耦模式)
          ActionContext

2.3.2 注入
2.3.2.1 耦合
作業:找出struts2中其它的幾個注入介面(XxxAware)

  2.3.2.2 解耦

–src
–struts.xml(核心配置檔案)
–struts.properties(全域性屬性檔案)
3. 核心檔案配置
3.1 include 包含檔案
file

3.2 package 包
name 包名
extends 繼承
namespace 虛擬路徑
abstract 通常用來被繼承

3.3 action 子控制器
name:helloAction,helloAction_*
class 全限定名
method:execute,{1}

  注1:動態方法呼叫,新版本中已禁用,可自行開啟或關閉

  注2:子控制器的訪問路徑:名稱空間+"/"+子控制器名字_xxx+".action"

3.4 result
name
type:dispatcher|redirect

新版本中struts2提供了一個外掛可檢視所有的Action

  1. 新增依賴

    org.apache.struts
    struts2-config-browser-plugin
    2.5.13

  2. 訪問
    http://localhost:8080/應用名字/config-browser/actionNames.action