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()方法被调用前执行
在jsp页面中获取其中的值
<s:property value="#request.task.tname"/>
struts2超链接传值: <s:a href="info.action?id=%{#list.id}"> <s:property value="#list.title"/></s:a>
第一种方式,非IoC(Spring中的控制反转)方式:
Java代码
public class BaseAction extends ActionSupport {
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);
}
public class BaseAction extends ActionSupport {
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代码
public class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
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;
}
}
public class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
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。
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 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 userList = userService.findAllUsers();
session.put(“userList”, userList);
//request.setAttribute(“userList”, userList);
return SUCCESS;
}
据说,如果直接到jsp页面的话,一般推荐用request而不用session,多人单机同时操作的话保险一点,虽然一个浏览器同一时间只有一个session。
在jsp页面取值的话:
Html代码
<%– –%>
用户ID | 用户名称 | 用户性别 | 用户年龄 | 用户地址 | 用户电话 | 用户邮箱 |
---|
<%– –%>
user.id
{user.name}
user.sex
{user.age}
user.address
{user.phone}
${user.email}
<%– 用完要清空 –%>
<%request.removeAttribute(“userList”);%>
<%–<%session.removeAttribute(“userList”);%>–%>