zoukankan      html  css  js  c++  java
  • struts2(二)——Action 与接收参数

    Action方法

    Method访问

    • 使用方法

    action 标签的 method 属性

    • 弊端

    要写多个action标签

    通配符

    • 要求

    访问路径当中要包含执行的方法,使用通配符调用方法时,内部会验证是否允许访问该方法,所以要在 Action 中加上

    <allowed-methods>delete,update,insert,select</allowed-methods>
    

    使用方法

    goods_*        method="{1}"    其中1为第一个*的内容
    *_*    class="com.myxq.struts.{1}"  method={2}
    

    动态方法访问

    • 要求
      默认是关闭状态,要想使用的话,必须得要通过修改常量来打开,使用通配符调用方法时,内部会验证是否允许访问该方法,所以要在Action中加上
    <allowed-methods>delete,update,insert,select</allowed-methods>
    

    使用方法

    goods!add.action // action.name == goods
    

    Struts中API介绍

    如何获得到servlet中的API

    开发当中不会使用这几种方式接收参数

    完全解耦合的方式

    使用ActionContext来进行获取

    • 注意

    此方法只能从域中取数据和保存数据,不能获取其它的方法

    • 使用步骤
    ActionContext context = ActionContext.getContext();
    HttpParameters parameters = context.getParameters();
    String username = parameters.get("username").getValue();
    String[] hobbies = parameters.get("hobby").getMultipleValues();
    System.out.println(username);
    System.out.println(Arrays.toString(hobbies));
    
    // to req
    context.put("reqName","reqValue");
    // to session
    context.getSession().put("sessionName","sessionValue");
    //to application
    context.getApplication().put("applicationName","applicationValue");
    
    • 示例表单
    <form action="${pageContext.request.contextPath}/from.action>
        用户名:<input type="text" placeholder="请输入用户名..." name="username"><br/>
        昵称:<input type="text" placeholder="请输入用户名..." name="nick"><br/>
        爱  好: <input type="checkbox" value="足球" name="hobby">足球
                <input type="checkbox" value="篮球" name="hobby">篮球
                <input type="checkbox" value="乒乓球" name="hobby">乒乓球<br/>
        <input type="submit" value="提交">
    </form>
    

    使用Servlet的API的原生方式

    • servletActionContext
    • 这种方式可以操作域对象的数据,同时也可以获得对象的方法。
    • 使用步骤
    HttpServletRequest request = ServletActionContext.getRequest();
    request.setCharacterEncoding("gbk");
    String username = request.getParameter("username");
    String[] hobbies = request.getParameterValues("hobby");
    System.out.println(username);
    System.out.println(Arrays.toString(hobbies));
    
    request.setAttribute("","");
    request.getSession().setAttribute("","");
    ServletActionContext.getServletContext().setAttribute("","");
    

    接口注入的方式

    • 让Action实现一些接口,让接口提供的一些方法,设置一些具体的值

    结果页配置

    局部结果页

    在 action 中配置,优先查找,私有的

    <action name="from" class="work.douzi.struts.FromAction">
        <result name="success">/res.jsp</result>
    </action>
    

    全局结果页

    在 package 中配置,公用的

    <global-results>
        <result name="login">/login.jsp</result>
    </global-results>
    

    接收参数

    提供属性set方法的方式

    在Action当中提供对应属性的set方法,会自动接收参数,并且会自动做类型转换

    开发当中用得少,当只接收少量数据,不用封装成对象的时候用

    使用方式

    <h2>提供属性set方法的方式</h2>
      <form action="${pageContext.request.contextPath}/from3.action" method="post">
        用户名:<input type="text" placeholder="请输入用户名..." name="username"><br/>
        年龄:<input type="text" placeholder="请输入年龄..." name="age"><br/>
        爱  好: <input type="checkbox" value="足球" name="hobby">足球
        <input type="checkbox" value="篮球" name="hobby">篮球
        <input type="checkbox" value="乒乓" name="hobby">乒乓球<br/>
        <input type="submit" value="提交">
      </form>
    
    private String username;
    private Integer age;
    private List hobby;
    

    缺点

    接收少量数据可以,如果大量数据,还得要自己手动封装成对象

    页面中提供表达式方式

    • 创建一个接收参数的domain模型

    • 在Action当中提供一个对象属性,并要提供该对象属性的get/set方法

    • 在jsp页面当中发送参数时要带上Action当中的对象属性名称
      使用方式

    采用模型驱动方式

    • 创建一个接收参数的domain模型

    • 在Action当中实现ModelDriven接口

    • 实现接口方法getModel()

    • 创建一个模型对象,在接口方法中返回

    使用方式

    private User user = new User();
    @Override
    public User getModel() {
        return user;
    }
    @Override
    public String execute() throws UnsupportedEncodingException {
        System.out.println("user===="+user);
        return null;
    }
    

    接收参数错误处理

    只要任何一个拦截器在执行的过程当中,都会向错误信息中去添加错误消息。最后workflow会检查错误信息当中是否有错误的消息,如果没有,就直接到目标action;如果有,就会跳转到input逻辑视图

    在开发时,可以配置一个input逻辑视图,错误时,可以在跳转到自己指定的页面, 在页面中可以显示错误信息

    接收复杂类型封装到List集合

    实际上就是页面表达式接收参数,只不过换成 List

    • 创建jsp页面

    • 创建页面对象模型

    • 创建对应Action

    • 在jsp当中添加表达式

    接收复杂类型封装到Map集合

    类似于 List 方式,key值为自己取的一个值

  • 相关阅读:
    Sql Server Profiler使用
    用PowerDesign反向生成数据库Sql语句问题
    使用PowerDesign15反向生成数据库
    离线安装Sharepoint工具
    开发Windows服务
    Windows Server 2008 R2 安装域
    Entity Framework执行Sql语句返回DataTable
    GO 学习资源收集
    Ubuntu常用命令大全
    MVC MVC3中 ViewBag、ViewData和TempData的使用和区别 【转】
  • 原文地址:https://www.cnblogs.com/mdz3201/p/12636261.html
Copyright © 2011-2022 走看看