如果是一个普通的POJO类中如何获取request对象的引用
你可以在 POJO 类中写一个方法把 request对象作为参数传过去。
你可以在 POJO 类中写一个方法把 request对象作为参数传过去。
解决方案 »
- struts2中使用servlet关于web.xml的配置
- 关于Applet JTextField的问题
- 大家好,这时关于j2ee里struts和spring模式匹配的问题
- hibernate 一对多插入数据时 NullPointerException 求救
- j2sdk1.4.2 和 tomcat6.0不兼容,请问有没有patch包可以解决这个问题?
- 关于协议传输的问题!
- 那位大哥能提供个开源的论坛啊 小弟谢了
- 有关j2ee的困惑
- TOMCAT的配置问题
- 在jb8中建cmp时要一个一个字段地添加吗?并且“是不是主键”这些确定的信息都要自己来添加吗?能不能自动建立?
- 用struts2时在控制台打印的警告信息。谁知道怎么回事啊??
- linux中启动weblogic的命令是什么?
因为你没有在里面存放任何东西,当然是空啦
这是一个办法,但是我主要想弄清楚ServletActionContext.getRequest()为什么不行,它到顶怎么用?为什么在action中是有效的?
我取的request对象,不是放在request对象中的obj,再说现在练request对象都不能引用到!(用ServletActionContext.getRequest())
User user = new User();
user.setName("aaa");
ActionContext ctx = ActionContext.getContext();
ctx.put("user",user);在你的JSP中
可以用EL表达式${user.name}来得到
谢谢!你的回答没有错!不过我的问题是ServletActionContext的用法,用这个类如何获得request对象?这么问吧!假设我有一个自定义的ServletUitl类public class ServletUtil
{
public static void formate(String key)
{
HttpServletRequest request = ServletActionContext.getRequest();
String value = (String)request.getAttribute(key);
value = value + "经过格式化过程了!";
request.setAttribute(key,value);
}
}jsp页面
<%
request.setAttribute("key","hello world!");
ServletUtil.formate("key");
String value = (String)request.getAttribute("key");
out.println(value);
%>但是页面上不会出现hello world!经过格式化过程了!再强调一下,我的目的是想知道ServletActionContext.getRequest()为什么是null,
或者如何在一个普通的POJO类中获得request对象的引用?
ServletActionContext.getRequest(); 可以拿到请求是应该 在 你框架的 action中 耦合进来来的一个方法。
但普通的jsp和 java类同你的 框架没有任何关系. 自然就得不到你的框架的支持.
public static void setRequest(HttpServletRequest request) {
ActionContext.getContext().put(HTTP_REQUEST, request);
}意思可能就是容器通过加载ServletActionContext将request对象设置进入了ServletActionContext,当我们调用的时候,
由于struts包是由容器加载的,所以我们调用的ServletActionContext就是已经存在request对象的那个类,
这样才能得到一个request对象;