struts2概述

1.Struts2基础使用方法  MVC框架  调度请求
2.开发步骤
  2.1 引入依赖
  2.2 配置web.xml  核心过滤器 StrutsPrepareAndExecuteFilter
     
  2.3 UI
  2.4 定义Action
      public FirstAction implements Action{
         public String  execute() throws Exception{
  return "逻辑视图名";
}
      }
  2.5 创建strus.xml
      文件的根节点

 
     /index.jsp
 


                   

  2.6部署运行
3.自动装配
  零散属性:Action类中两个成员变量的名称和页面上表单元素name属性值保持一致。
      规则:约定优于配置。
  领域模型:两种配置
    public class FirstAction implements Action{
private UserInfo userinfo;
    }
      配置一:
      userinfo.username
      userinfo.password
      配置二:ModelDriven
     public class FirstAction implements ModelDriven,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 map= ActionContext.getContext().getSession();
 map.put(k,v);
  return "逻辑视图名";
}
      }

    方式二:
    public FirstAction implements SessionAware,Action{
         Map map ;
         public String  execute() throws Exception{
  map.put(k,v);
  return "逻辑视图名";
}
public void setSessionAware(Map 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 map;
map.put("uname","张三");
ValueStack vs=ActionContext.getContext().getValueStack();
ValueStack vs=(ValueStack)ServletActionContext.getRequest.getAttributes("struts.valueStack");
vs.push(map);
  方式一:不加#
         
 ¥{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.常量配置
   
   
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"
   
                userActionResultAdd
                /
           

     pom.xml

<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.3version>
    <scope>testscope>
dependency>



<dependency>
    <groupId>javaeegroupId>
    <artifactId>javaee-apiartifactId>
    <version>5version>
dependency>

<dependency>
    <groupId>org.apache.strutsgroupId>
    <artifactId>struts2-coreartifactId>
    <version>2.3.4.1version>
dependency>

<dependency>
    <groupId>org.apache.struts.xworkgroupId>
    <artifactId>xwork-coreartifactId>
    <version>2.3.4.1version>
dependency>

<filter>
  <filter-name>struts2filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
  <filter-name>struts2filter-name>
  <url-pattern>/*url-pattern>
filter-mapping>
struts.xml
xml version="1.0" encoding="UTF-8"?>
 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="useraction" class="cn.struts.UsesrAction">
        <result name="success">/success.jspresult>
        <result name="input">1.jspresult>
    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">
 
  <action name="firstlist" class="cn.struts.day02.filtFirst" method="list">
      <result name="list">/day02/list.jspresult>
  action>

    
    <action name="firstDel" class="cn.struts.day02.filtFirst" method="del">
        <result name="del">/day02/del.jspresult>
    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">
 
  <action name="*_*" class="cn.struts.day03.{1}" method="{2}">
      <result name="{2}">/day03/{2}.jspresult>
  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">
     
      <action name="firstFirst" class="cn.struts.day04.filtFirst">
          <result name="list">/day02/list.jspresult>
          <result name="del">/day02/del.jspresult>
      action>
    package>
struts>
UserAction类
 
  
public class UserAction extends ActionSupport {
    public String del(){
        return "del";
    }

    public String list(){
        Map request= (Map) ActionContext.getContext().get("request");
        request.put("uname","啦啦");
        return "list";
    }
    public String add(){
        Map request= (Map) 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">userActionAddparam>
                <param name="namespace">/param>
            result>
        action>

        <action name="userActionAdd" class="cn.struts.day05.UserAction" method="add">
            <result name="add">/day05/add.jspresult>
        action>
    package>
struts>
add.jsp
<body>
<s:debug/>
${info}
${uname}
<h2>Day05 Addh2>
body>

你可能感兴趣的:(struts2概述)