zoukankan      html  css  js  c++  java
  • 转----JAVA前端与后端参数传递方法小结

    1,从Action中传值到JSP页面的方法

         ①在Action中定义一个成员变量,然后对这个成员变量提供get/set方法,在JSP页面就可以取到这个变量的值了。

      1)在Action中定义成员变量

     //定义一个成员变量
    private String message;
    	
    //提供get/set方法
    public String getMessage() {
    	return message;
    }
    public void setMessage(String message) {
    	this.message = message;
    }
    

      2)在JSP页面中取值

    ${message}
    或者
    <s:property value="message"/>
    

         ②使用一些Servlet API进行值的存取操作:HttpServletRequest、HttpSession和ServletContext。Struts2对这个三个对象用Map进行了封装,我们就可以使用Map对象来存取数据了。

      1)在Action中存值

    ActionContext actionContext = ActionContext.getContext();
    		
    //get HttpServletRequest
    Map<String,Object> request = (Map) actionContext.get("request");
    request.put("a", "a is in request");
    		
    //get HttpSession
    //Map<String,Object> session = (Map) actionContext.get("session");
    Map<String,Object> session = actionContext.getSession();
    session.put("b", "b is in session");
    		
    //get ServletContext
    //Map<String,Object> application  = (Map) actionContext.get("application");
    Map<String,Object> application  = actionContext.getApplication();
    application.put("c", "c is in application");
    
    //get ServletActionContext.request
    HttpServletRequest request=ServletActionContext.getRequest()
    request.setAttribute("c2", "test5");
    request.put("c3","test6")
    //或者直接放入上下文中
    ActionContext.getContext().put("d","d is in application"); 
    
    
    

      2)在JSP页面上取值(使用EL表达式)

    ${a}
    ${b}
    ${c}
    ${d}
    ${c2}
    ${c3}
     or				
    ${requestScope.a}
    ${sessionScope.b}
    ${applicationScope.c}
    ${actionContext.d}
    ${requestScope.c2}
    ${requestScope.c3}
    or<%=request.getAttribute("d")%>  //使用ActionContext.getContext().put("d","d is an application")的情况
    

    附加:ServletContext,ActionContext,ServletActionContext的区别   

    在ActionContext.getContext().put("a", "   test3")后页面上用${a}或<s:property value="#a"/>获得test3的值

    #相当于ActionContext. getContext() ,#session.b表达式相当于ActionContext.getContext().getSession(). getAttribute(”b”)

    ③对于传递list的值,可以使用 actionContext的valueStack来传递值栈

        1) 在后台使用Hibernate查询 ,EntityManager 通过createQuery()来getResultList()获得List,将list放入valueStack中

    ActionContext.getContext().getValueStack().push(model);
    

        2)在页面上通过<s:iterator>标签遍历list的每条值显示在table上。

    [html] view plain copy
     
    1. <table class="tablelist" >  
    2.                 <thead>  
    3.                     <tr>  
    4.                         <th width="100px;">编号</th>  
    5.                         <th>影片类型</th>  
    6.                         <th>影片名称</th>  
    7.                         <th>发布人</th>  
    8.                         <th>发布时间</th>  
    9.                         <th>审核状态</th>  
    10.                         <th>操作</th>  
    11.                     </tr>  
    12.                 </thead>  
    13.   
    14.                 <tbody>  
    15.                  <s:iterator value="recordList" var="o" status="i">  
    16.                     <tr>  
    17.                         <td>${o.id}</td>  
    18.                         <td>${o.name }</td>  
    19.                         <td>${o.type}</td>  
    20.                         <td>${o.updateMember.memberName }</td>  
    21.                     </tr>  
    22.                  </s:iterator>  
    23.                 </tbody>  
    24.   
    25.             </table>  


    2,从前台向后台传递参数

       ①通过表单传递参数

         1)在前端jsp页面设置form表单,确定需要传递的参数name让用户在input中输入,通过点击按钮后submit()提交到后台

    [html] view plain copy
     
    1. <s:form method="post" action="ActivityAction_toUI.action">  
    2.   <table class="serTable">  
    3.         <tbody>  
    4.             <tr>  
    5.                 <td>  
    6.                     <label>活动名称</label>  
    7.                 </td>  
    8.                 <td>  
    9.                     <input type="text" class="serput" name="activityName" placeholder="输入文本"/>  
    10.                 </td>  
    11.                 <td>  
    12.                     <s:submit cssClass="btn1" value="搜索" onclick="submit();" />  
    13.                 </td>  
    14.             </tr>  
    15.         </tbody>  
    16.      </table>  
    17.  </s:form>  

        2)点击搜索后activityName会放到HttpServletRequest 中

    [java] view plain copy
     
    1. HttpServletRequest httpReq = ServletActionContext.getRequest();  
    2. String s = httpReq.getParameter("activityName");  

        另外,在后台也可以通过extends ActionSupport 并构建get/set方法在后台获得其值

    [java] view plain copy
     
    1.         private String activityName;  
    2.       
    3.         public String getActivityName() {  
    4.               return activityName;  
    5.          }  
    6.   
    7.         public void setActivityName(String activityName) {  
    8.                this.activityName = activityName;  
    9.         }  
    10.           
    11.         public String list(){  
    12.                  System.out.println(activityName);  
    13.         }  



    ②通过超链接传递参数

         1)前台通过超链接跳转时将参数加到方法的后面

    <s:a cssClass="acolor" target="mainBody" theme="simple"  action="ActivityAction_info.action?Id=%{Id}&pageNum=%{pageNum}&infot="mylittlepony"" ><img
                                        src="${pageContext.request.contextPath}/images/info.png"
                                        title="查看" /></s:a>
    

         2)后台通过HttpServletRequest 获得超链接后面参数所对应的值

    [java] view plain copy
     
    1. HttpServletRequest httpReq = ServletActionContext.getRequest();  
    2. <pre name="code" class="java">Long id= Long.parseLong(httpReq.getParameter("Id"));  

    String s = httpReq.getParameter("infot");
    Long pageNum = Long.parseLong(httpReq.getParameter("pageNum"));

    
    

     

  • 相关阅读:
    Linux系统自带服务罗列
    几个有用的shell命令
    Zabbix
    RaspberryPi3安装CentOS7教程
    grafana简介
    负载均衡之Haproxy配置详解(及httpd配置)
    CentOS7版本的新特性
    文件系统目录结构
    openstack部署dashboard
    openstack核心组件--cinder存储服务(6)
  • 原文地址:https://www.cnblogs.com/lyf906522290/p/8571624.html
Copyright © 2011-2022 走看看