JAVA前端与后端参数传递方法小结

时间:2022-09-05 21:10:56

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上。

<table class="tablelist" >
<thead>
<tr>
<th width="100px;">编号</th>
<th>影片类型</th>
<th>影片名称</th>
<th>发布人</th>
<th>发布时间</th>
<th>审核状态</th>
<th>操作</th>
</tr>
</thead>

<tbody>
<s:iterator value="recordList" var="o" status="i">
<tr>
<td>${o.id}</td>
<td>${o.name }</td>
<td>${o.type}</td>
<td>${o.updateMember.memberName }</td>
</tr>
                 </s:iterator>
                </tbody>

            </table>

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

   ①通过表单传递参数

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

<s:form method="post" action="ActivityAction_toUI.action">
<table class="serTable">
        <tbody>
            <tr>
                <td>
                    <label>活动名称</label>
                </td>
                <td>
                    <input type="text" class="serput" name="activityName" placeholder="输入文本"/>
                </td>
                <td>
                    <s:submit cssClass="btn1" value="搜索" onclick="submit();" />
                </td>
            </tr>
        </tbody>
    </table>
 </s:form>
    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)前台通过超链接跳转时将参数加到方法的后面

<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 获得超链接后面参数所对应的值
HttpServletRequest httpReq = ServletActionContext.getRequest();
<pre name="code" class="java">Long id= Long.parseLong(httpReq.getParameter("Id"));
String s = httpReq.getParameter("infot");
Long pageNum = Long.parseLong(httpReq.getParameter("pageNum"));