struts2

1.Struts2基礎使用方法  MVC框架  調度請求
2.開發步驟
  2.1 引入依賴
  2.2 配置web.xml  核心過濾器 StrutsPrepareAndExecuteFilter
      <filter-mapping>
  2.3 UI
  2.4 定義Action
      public FirstAction implements Action{
         public String  execute() throws Exception{
  return "邏輯視圖名";
}
      }
  2.5 建立strus.xml
      文件的根節點 <struts>
<package name="default" namespace="/" extends="struts-default">
  <action name="請求路徑" class="Action類的全路徑">
     <result name="success">/index.jsp</result>
  </action>
</package>
                   </struts>
  2.6部署運行
3.自動裝配
  零散屬性:Action類中兩個成員變量的名稱和頁面上表單元素name屬性值保持一致。
      規則:約定優於配置。
  領域模型:兩種配置
    public class FirstAction implements Action{
private UserInfo userinfo;
    }
      配置一:
      userinfo.username
      userinfo.password
      配置二:ModelDriven
     public class FirstAction implements ModelDriven<UserInfo>,Action{
  private UserInfo userinfo=new UserInfo();
public UserInfo getModel(){
   return userinfo;
}
     }
3.ServletAPI 操做 session request, application
  解耦
    方式一:
    public FirstAction implements Action{
         public String  execute() throws Exception{
 Map<String,Object> map= ActionContext.getContext().getSession();
 map.put(k,v);
  return "邏輯視圖名";
}
      }

    方式二:
    public FirstAction implements SessionAware,Action{
         Map<String,Object> map ;
         public String  execute() throws Exception{
  map.put(k,v);
  return "邏輯視圖名";
}
public void setSessionAware(Map<String,Object> map){
this.map=map;
}
      }
  耦合
    方式一:
     public FirstAction implements Action{
         
         public String  execute() throws Exception{
  HttpSession session=ServletActionContext.getRequest().getSession();
  session.setAttribute(k,v);
  return "邏輯視圖名";
}
      }
    方式二:
   public class FirstAction implements Action,ServletRequestAware {
    HttpServletRequest request;
    public String execute() throws Exception {
        HttpSession session = request.getSession();
        return null;
    }
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        request=httpServletRequest;
    }
  }
4.Action------->UI  數據共享  
        Map<String,Object> map;
map.put("uname","張三");
ValueStack vs=ActionContext.getContext().getValueStack();
ValueStack vs=(ValueStack)ServletActionContext.getRequest.getAttributes("struts.valueStack");
vs.push(map);
  方式一:不加#
          <s:property value="uname">
 ¥{uname}  
  方式二:得加#
      <s:property value="#session.uname">
1.Struts2執行  基本架構
GPR原則:登陸
    給用戶看一眼   get    
用戶填寫登陸信息,點擊登陸 post
頁面跳轉歡迎界面 redirect
*能夠將核心過濾器當作是Action類的中央調度器。
2.Struts.xml 節點詳細講解
   namspace做用:隔離不一樣的命名空間下的邏輯同名的Action
  幾個文件的執行順序
  1.default.property
  2.struts-default.xml
  3.struts-plugin.xml
  4.struts.xml
  5.struts.property
  6.web.xml

3.常量配置
    <!--修改Struts.xml,不用重啓容器-->
    <constant name="struts.devMode" value="true" />
4.動態方法調用(DMI Dynamic Mehtod Invocation) RMI Remote Method Invocation 遠程方法調用
  當一個Action中有多個業務方法的時候,咱們要向讓客戶端請求能匹配到對應的方法,咱們如今的作法
  定義多個Action節點,咱們又想到了,若是Action節點不少,維護起來很不方便。
   1.開啓動態方法調用
   2.配置
   經過!訪問方法      actionName!list   actionName!del(Action類中的真實方法)  可能會注入
缺點:
5.通配符 *_*  萬象歸終  四兩撥千斤
使用一個節點 實現整個軟件全部的Action  best prectice  約定因爲配置
6.ResultType  結果類型
  dispatcher  普通轉發到視圖頁面 (默認值) ->轉發
  redirect   重定向到視圖頁面
  redirectAction 重定向到Action
  chain    轉發到Action
  plainText
  轉發到Action :type="chain"
   <result name="list" type="chain">
                <param name="actionName">userActionResultAdd</param>
                <param name="namespace">/</param>
            </result>java

     pom.xmlweb

 

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.3</version>
    <scope>test</scope>
</dependency>


<!--獲取ServletAPI-->
<dependency>
    <groupId>javaee</groupId>
    <artifactId>javaee-api</artifactId>
    <version>5</version>
</dependency>

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.3.4.1</version>
</dependency>

<dependency>
    <groupId>org.apache.struts.xwork</groupId>
    <artifactId>xwork-core</artifactId>
    <version>2.3.4.1</version>
</dependency>

<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>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>

    <!--建立一個default包,繼承自Struts2的struts-default包-->
    <!--<package name="default" namespace="/" extends="struts-default">
        &lt;!&ndash;接受處理用戶的/helloword.action請求,並根據返回結果,完成跳轉&ndash;&gt;
        <action name="helloword"
                class="cn.struts.one.HelloWordAction">
            &lt;!&ndash;結果爲」helloword時,跳轉至helloword.jsp頁面&ndash;&gt;
            <result name="ss">helloword.jsp</result>
        </action>
    </package>-->
<package name="default" namespace="/" extends="struts-default">
    <action name="useraction" class="cn.struts.UsesrAction">
        <result name="success">/success.jsp</result>
        <result name="input">1.jsp</result>
    </action>
</package>

<include file="struts-day02.xml"></include>
<include file="struts-day03.xml"></include>
<include file="struts-day04.xml"></include>
<include file="struts-day05.xml"></include>
</struts>
filtFirst類  
public class filtFirst extends ActionSupport {

    public String  list(){
        System.out.println("list");
        return "list";
    }

    public String  del(){
        System.out.println("del");
        return "del";
    }
}
struts-day02.xml
<constant name="struts.devMode" value="true"></constant>
<package name="cn.struts.day02" namespace="/" extends="struts-default">
 <!--list-->
<action name="firstlist" class="cn.struts.day02.filtFirst" method="list">
      <result name="list">/day02/list.jsp</result>
  </action>

    <!--del-->
<action name="firstDel" class="cn.struts.day02.filtFirst" method="del">
        <result name="del">/day02/del.jsp</result>
    </action>
</package>
------------------------------------------------------------
twodell類
public class twodell extends ActionSupport {
    public String  one(){

        return "one";
    }
    public String two(){

        return "two";
    }
}
struts-day03.xml
<constant name="struts.devMode" value="true"></constant>
<package name="cn.struts.day03" namespace="/" extends="struts-default">
 <!--list-->
<action name="*_*" class="cn.struts.day03.{1}" method="{2}">
      <result name="{2}">/day03/{2}.jsp</result>
  </action>
</package>
-----------------------------------------------------
 
filtFirst類
public class filtFirst extends ActionSupport {

    public String  list(){
        System.out.println("list===================");
        return "list";
    }

    public String  del(){
        System.out.println("del====================");
        return "del";
    }
}
struts-day04.xml
<struts>
    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <package name="cn.struts.day04" namespace="/" extends="struts-default">
     <!--list-->
<action name="firstFirst" class="cn.struts.day04.filtFirst">
          <result name="list">/day02/list.jsp</result>
          <result name="del">/day02/del.jsp</result>
      </action>
    </package>
</struts>
UserAction類
public class UserAction extends ActionSupport {
    public String del(){
        return "del";
    }

    public String list(){
        Map<String,Object> request= (Map<String, Object>) ActionContext.getContext().get("request");
        request.put("uname","啦啦");
        return "list";
    }
    public String add(){
        Map<String,Object> request= (Map<String, Object>) ActionContext.getContext().get("request");
        Object uname=request.get("uname");
        request.put("info","數據");
        return "add";
    }
}
struts-day05.xml
<struts>
    <constant name="struts.devMode" value="true"></constant>
    <package name="day05" namespace="/" extends="struts-default">
        <action name="userActionResult" class="cn.struts.day05.UserAction" method="list">
            <result name="list" type="chain">
                <param name="actionName">userActionAdd</param>
                <param name="namespace">/</param>
            </result>
        </action>

        <action name="userActionAdd" class="cn.struts.day05.UserAction" method="add">
            <result name="add">/day05/add.jsp</result>
        </action>
    </package>
</struts>
add.jsp
<body>
<s:debug/>
${info}${uname}<h2>Day05 Add</h2>
</body>
相關文章
相關標籤/搜索