zoukankan      html  css  js  c++  java
  • Struts2获取request三种方法

    Struts2获取request三种方法
     
    struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象。
    在Action中获取request方法一:
     
    在Action中的代码:
    Map request = (Map)ActionContext.getContext().get("request");
    List<Task> tasks = taskManager.findAll();
    request.put("tasks", tasks);
     
    在JSP页面中获取其中的值:
    <s:iterator id="task" value="#request.tasks">
           <tr class="table_header">
            <td><s:property value="#task.tname"/></td>
            <td><s:property value="#task.tuid"/></td>
            <td><s:property value="#task.tstartTime"/></td>
            <td><s:property value="#task.tendTime"/></td>
            <td><s:property value="#task.tstate"/></td>
            <td><input type="radio" id="choose" name="choose" onclick="getId(this.value)" value="<s:property value='#task.tid'/>"/></td> 
           </tr>
    </s:iterator>
    --------------------------------------------------------------------------------------------
    方法二:通过ServletActionContext类来获取,使用struts2经验如果处理get传参是中文,只能使用该方法进行处理乱码问题
     
    Action中代码:
    HttpServletRequest request = ServletActionContext.getRequest();
    request.setAttribute("username", "zhangsan");
     
    在jsp中获取其中的值
         <s:property value="#request.username">或者${requestScope.req}
    -------------------------------------------------------------------------------------------- 
    方法三:通过ServletRequestAware接口通过IOC机制注入Request对象
    Action中的代码:
    Action实现ServletRequestAware接口,实现接口中的方法
         private HttpServletRequest request;
         //实现接口中的方法
         public void setServletRequest(HttpServletRequest request){
          this.request = request;
         }
         //然后在execute()方法中就可以使用了
         public String execute(){
          request.setAttribute("username", "zhangsan");
          request.getSession().getServletContext().getApplication(); //得到Application
         }
         该方法必须要实现,而且该方法是自动被调用
         这个方法在被调用的过程中,会将创建好的request对象通过参数的方式传递给你,你可以用来赋给你本类中的变量,然后request就可以使用了
         注意:setServletRequest()方法一定会再execute()方法被调用前执行
     
  • 相关阅读:
    java web项目打包.war格式
    version 1.4.2-04 of the jvm is not suitable for thi
    Sugarcrm Email Integration
    sharepoint 2010 masterpage中必须的Content PlaceHolder
    微信开放平台
    Plan for caching and performance in SharePoint Server 2013
    使用自定义任务审批字段创建 SharePoint 顺序工作流
    Technical diagrams for SharePoint 2013
    To get TaskID's Integer ID value from the GUID in SharePoint workflow
    how to get sharepoint lookup value
  • 原文地址:https://www.cnblogs.com/Mr-Rocker/p/4249489.html
Copyright © 2011-2022 走看看