Struts2获取request的几种方式汇总
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"/>
/本篇文章来源于Java秀,原文出处:http://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");%>--%>
Struts2获取request的几种方式汇总的更多相关文章
- 【Struts2】Struts2获取session的三种方式
1.Map<String,Object> map = ActionContext.getContext().getSession(); 2.HttpSession session = S ...
- Struts2获取参数的几种方式
Struts2由于是一个贴心的框架,所以获取参数这种体力活,就无需再通过原生的request来getParameter了,有如下几种方式进行获取 1.Action中属性驱动,必须提供与form表单na ...
- Struts2获取Session的三种方式
1.Map<String,Object> session = ActionContext.getContext().getSession(); session.put("cod ...
- struts2获取request、session、application的四种方式
struts2获取request.session.application的四种方式 //获取map类型的request.session.application public class LoginAc ...
- Struts2获取request三种方法
Struts2获取request三种方法 struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象. 在Actio ...
- 【深入Struts2】获取ServletAPI的三种方式
一:获取servletAPI的三种方法 在传统的Web开发中,经常会用到Servlet API中的HttpServletRequest.HttpSession和ServletContext.Strut ...
- Struts2(四.注册时检查用户名是否存在及Action获取数据的三种方式)
一.功能 1.用户注册页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- java:struts框架2(方法的动态和静态调用,获取Servlet API三种方式(推荐IOC(控制反转)),拦截器,静态代理和动态代理(Spring AOP))
1.方法的静态和动态调用: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...
- java动态获取WebService的两种方式(复杂参数类型)
java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_Fligh ...
随机推荐
- Postgresql的导表
背景 前面已经介绍了常用的备份与恢复了,接下来介绍一下导表. 正文 很多情况,会有把数据导出的需求,轻重缓急总会有特别紧急的情况,但是又不是专业干db的人,还是记录下来,以防不时之需. 针对于导表,个 ...
- PID<->Port[转]
//Netstat -anb #include <Windows.h> #include "Psapi.h" #include <Iprtrmib.h> # ...
- Apache Commons Lang之日期时间工具类
码农不识Apache,码尽一生也枉然. FastDateFormat FastDateFormat是一个快速且线程安全的时间操作类,它完全可以替代SimpleDateFromat.因为是线程安全的,所 ...
- 计算KS值的标准代码
计算KS值的标准代码 from scipy.stats import ks_2samp get_ks = lambda y_pred,y_true: ks_2samp(y_pred[y_true==1 ...
- Codeforces Round #556(Div.1)
A 容易发现i,i+1至少有一个数出现,于是可以让尽量多的2和奇数出现 #include<bits/stdc++.h> using namespace std; int n,s1,s2; ...
- Linux保护机制
RELRO(RELocation Read Only) 在Linux中有两种RELRO模式:"Partial RELRO" 和 "Full RELRO".Lin ...
- poj-3659 Cell Phone Network(最小支配集+贪心)
http://poj.org/problem?id=3659 Description Farmer John has decided to give each of his cows a cell p ...
- 三十四、www服务apache进阶
9.虚拟主机:部署多个站点,每个站点希望用不同的站点域名和站点目录,或者是不同的端口和不同的IP,则需要虚拟主机,简单理解就是一个http服务要配置多个站点,就要虚拟主机. apache虚拟主机分为三 ...
- python_8_集合
1.集合:可变集合set,不可变集合frozenset,集合是无序不重复的 set('hello') set9[1,2,3,4]) set((1,2,3)) 2.添加元素 > add:将元素整体 ...
- Survey sampling
Survey sampling \(\bullet\)What is survey sampling?(c.f.census survey)(c.f.:参考,查看,来源于拉丁语) \(\bullet\ ...