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>

     pom.xml

 

<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>

posted on 2017-10-25 12:25  溜氓  阅读(159)  评论(0编辑  收藏  举报

导航