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}或者

   

②使用一些Servlet API进行值的存取操作:HttpServletRequest、HttpSession和ServletContext。Struts2对这个三个对象用Map进行了封装,我们就可以使用Map对象来存取数据了。
  
    1)在Action中存值
ActionContext actionContext = ActionContext.getContext(); //get HttpServletRequest
Map request = (Map) actionContext.get("request");
request.put("a", "a is in request");
//get HttpSession
//Map session = (Map) actionContext.get("session");
Map session = actionContext.getSession();
session.put("b", "b is in session");

//get ServletContext
//Map application  = (Map) actionContext.get("application");
Map 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}或获得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)在页面上通过标签遍历list的每条值显示在table上。



               
                   
                       
                       
                       
                       
                       
                       
                       
                   
               

               
                
                   

                       
                       
                       
                       
                   
                
               

           
编号 影片类型 影片名称 发布人 发布时间 审核状态 操作
${o.id} ${o.name } ${o.type} ${o.updateMember.memberName }






2,从前台向后台传递参数
 
  ①通过表单传递参数

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


 
       
           
               
               
               
           
       
    

                   
               

                   
               

                   
               


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

HttpServletRequest httpReq = ServletActionContext.getRequest();
String s = httpReq.getParameter("activityName");
    另外,在后台也可以通过extends ActionSupport 并构建get/set方法在后台获得其值
        private String activityName;

        public String getActivityName() {
              return activityName;
         }

        public void setActivityName(String activityName) {
               this.activityName = activityName;
        }
       
        public String list(){
                 System.out.println(activityName);
        }



②通过超链接传递参数

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


                                    src="${pageContext.request.contextPath}/images/info.png"
                                    title="查看" />

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

HttpServletRequest httpReq = ServletActionContext.getRequest();
Long id= Long.parseLong(httpReq.getParameter("Id"));String s = httpReq.getParameter("infot"); 
 
Long pageNum = Long.parseLong(httpReq.getParameter("pageNum"));

你可能感兴趣的:(JAVA前端与后端参数传递方法小结)