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");
在中获取其中的值
<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()方法被调用前执行
在jsp页面中获取其中的值
<s:property value="#request.task.tname"/>
/本篇文章来源于java秀,原文出处:https://www.java.sh/article/jsp/1353.html
struts2超链接传值: <s:a href="info.action?id=%{#list.id}"> <s:property value="#list.title"/></s:a>
第一种方式,非ioc(spring中的控制反转)方式:
java代码
/**
* file name:baseaction.java * version: * date:2010-1-27 * copyright belongs to musoon corporation 2010
*/
package com.action;
import java.util.map;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts2.servletactioncontext;
import com.opensymphony.xwork2.actioncontext;
import com.opensymphony.xwork2.actionsupport;
/**
* project name:zhiming ** class name:baseaction
* author:musoon ** created time:2010-1-27 下午06:45:35
* changed by:musoon ** changed time:2010-1-27 下午06:45:35
* changed memo:
* @version
* class description:
*/
public class baseaction extends actionsupport {
private static final long serialversionuid = 7620009925942346125l;
actioncontext context = actioncontext.getcontext();
httpservletrequest request = (httpservletrequest) context.get(servletactioncontext.http_request);
httpservletresponse response = (httpservletresponse) context.get(servletactioncontext.http_response);
map session = context.getsession();
//sessionmap session = (sessionmap) context.get(actioncontext.session);
}
/**
* file name:baseaction.java * version: * date:2010-1-27 * copyright belongs to musoon corporation 2010
*/
package com.action;
import java.util.map;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts2.servletactioncontext;
import com.opensymphony.xwork2.actioncontext;
import com.opensymphony.xwork2.actionsupport;
/**
* project name:zhiming ** class name:baseaction
* author:musoon ** created time:2010-1-27 下午06:45:35
* changed by:musoon ** changed time:2010-1-27 下午06:45:35
* changed memo:
* @version
* class description:
*/
public class baseaction extends actionsupport {
private static final long serialversionuid = 7620009925942346125l;
actioncontext context = actioncontext.getcontext();
httpservletrequest request = (httpservletrequest) context.get(servletactioncontext.http_request);
httpservletresponse response = (httpservletresponse) context.get(servletactioncontext.http_response);
map session = context.getsession();
//sessionmap session = (sessionmap) context.get(actioncontext.session);
}
我们平常所说的session一般是httpsession,但在struts2中被封装成了map类型。
第二种方式,ioc方式:
java代码
/**
* file name:baseaction.java * version: * date:2010-1-27 * copyright belongs to musoon corporation 2010
*/
package com.action;
import java.util.map;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts2.dispatcher.sessionmap;
import org.apache.struts2.interceptor.servletrequestaware;
import org.apache.struts2.interceptor.servletresponseaware;
import org.apache.struts2.interceptor.sessionaware;
import com.opensymphony.xwork2.actioncontext;
import com.opensymphony.xwork2.actionsupport;
/**
* project name:zhiming ** class name:baseaction
* author:musoon ** created time:2010-1-27 下午06:45:35
* changed by:musoon ** changed time:2010-1-27 下午06:45:35
* changed memo:
* @version
* class description:
*/
public class baseaction extends actionsupport implements sessionaware, servletrequestaware, servletresponseaware {
private static final long serialversionuid = 7620009925942346125l;
actioncontext context = actioncontext.getcontext();
httpservletrequest request;
httpservletresponse response;
sessionmap session;
//获取request,response,session方式一,非ioc方式,不用实现sessionaware, servletrequestaware, servletresponseaware
//actioncontext context = actioncontext.getcontext();
//httpservletrequest request = (httpservletrequest) context.get(servletactioncontext.http_request);
//httpservletresponse response = (httpservletresponse) context.get(servletactioncontext.http_response);
//map session = context.getsession();
//sessionmap session = (sessionmap) context.get(actioncontext.session);
//获取request,response,session方式一,ioc方式,必须实现sessionaware, servletrequestaware, servletresponseaware
public void setsession(map map) {
this.session = (sessionmap) map;
}
public void setservletrequest(httpservletrequest request) {
this.request = request;
}
public void setservletresponse(httpservletresponse response) {
this.response = response;
}
}
/**
* file name:baseaction.java * version: * date:2010-1-27 * copyright belongs to musoon corporation 2010
*/
package com.action;
import java.util.map;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts2.dispatcher.sessionmap;
import org.apache.struts2.interceptor.servletrequestaware;
import org.apache.struts2.interceptor.servletresponseaware;
import org.apache.struts2.interceptor.sessionaware;
import com.opensymphony.xwork2.actioncontext;
import com.opensymphony.xwork2.actionsupport;
/**
* project name:zhiming ** class name:baseaction
* author:musoon ** created time:2010-1-27 下午06:45:35
* changed by:musoon ** changed time:2010-1-27 下午06:45:35
* changed memo:
* @version
* class description:
*/
public class baseaction extends actionsupport implements sessionaware, servletrequestaware, servletresponseaware {
private static final long serialversionuid = 7620009925942346125l;
actioncontext context = actioncontext.getcontext();
httpservletrequest request;
httpservletresponse response;
sessionmap session;
//获取request,response,session方式一,非ioc方式,不用实现sessionaware, servletrequestaware, servletresponseaware
//actioncontext context = actioncontext.getcontext();
//httpservletrequest request = (httpservletrequest) context.get(servletactioncontext.http_request);
//httpservletresponse response = (httpservletresponse) context.get(servletactioncontext.http_response);
//map session = context.getsession();
//sessionmap session = (sessionmap) context.get(actioncontext.session);
//获取request,response,session方式一,ioc方式,必须实现sessionaware, servletrequestaware, servletresponseaware
public void setsession(map map) {
this.session = (sessionmap) map;
}
public void setservletrequest(httpservletrequest request) {
this.request = request;
}
public void setservletresponse(httpservletresponse response) {
this.response = response;
}
}
这样我们在写action时直接继承这个baseaction,那些request、response、session之类就可以正常地用了,good。等我下午有时间反编译一下别人封装好的baseaction,看看是不是这样搞的,哈哈。
ps:
平时我们在action中要把值设进session然后在jsp页面去的话,一般是这样(struts2不行):
java代码
public string findallusers() throws exception {
list<user> userlist = userservice.findallusers();
httpsession se = request.getsession();
se.setattribute("userlist", userlist);
session.put("userlist", userlist);
//session.put("aaa", "bbb");
//session.put(key, value);
//request.setattribute("userlist", userlist);
return success;
}
public string findallusers() throws exception {
list<user> userlist = userservice.findallusers();
httpsession se = request.getsession();
se.setattribute("userlist", userlist);
session.put("userlist", userlist);
//session.put("aaa", "bbb");
//session.put(key, value);
//request.setattribute("userlist", userlist);
return success;
}
在struts2中,设进session的话则应该变成这样了,因为session是一个map类型:
java代码
public string findallusers() throws exception {
list<user> userlist = userservice.findallusers();
session.put("userlist", userlist);
//request.setattribute("userlist", userlist);
return success;
}
public string findallusers() throws exception {
list<user> userlist = userservice.findallusers();
session.put("userlist", userlist);
//request.setattribute("userlist", userlist);
return success;
}
据说,如果直接到jsp页面的话,一般推荐用request而不用session,多人单机同时操作的话保险一点,虽然一个同一时间只有一个session。
在jsp页面取值的话:
html代码
<table class="table_report">
<tr>
<th>用户id</th>
<th>用户名称</th>
<th>用户性别</th>
<th>用户年龄</th>
<th>用户地址</th>
<th>用户电话</th>
<th>用户邮箱</th>
</tr>
<!-- struts2最正规的取值方式 -->
<%-- <s:iterator id="user" value="%{#session.userlist}">--%>
<s:iterator id="user" value="#session.userlist">
<%-- <s:iterator id="user" value="#request.userlist">--%>
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.sex}</td>
<td>${user.age}</td>
<td>${user.address}</td>
<td>${user.phone}</td>
<td>${user.email}</td>
</tr>
</s:iterator>
</table>
<%-- 用完要清空 --%>
<%request.removeattribute("userlist");%>
<%--<%session.removeattribute("userlist");%>--%>
上一篇: PHP将session保存到memcache服务器
下一篇: .Net程序间的通讯与控制