S2SH整合时遇到一个Action中取值的问题,疑惑中100分求详细解释。

SADSDSDDASDASD 2013-01-26 10:44:30
上学的时候 ,也没学什么 ,年底了 工作不忙 就开始自己研究S2SH了,刚能整合到一起

就遇到了一个问题,先上代码:
package com.myclass.test.action;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.myclass.test.dto.UserDTO;
import com.myclass.test.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller("UserAction")
public class UserAction extends ActionSupport {
@Resource
private UserService userService;
private String userName;
@Override
public String execute() throws Exception {

//System.out.println("---"+userName);
userService.addUser(new UserDTO(userName));

ActionContext ac=ActionContext.getContext();
HttpServletRequest request=(HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
System.out.println("@@"+request.getParameter("userName"));

ActionContext.getContext().put("message", userName);
return SUCCESS;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}

}



我现在想问的时,userName 这个属性,是Struts2可以自己根据页面(JSP)传来的表单
给赋值进去的,那么我用
ActionContext.getContext().get("userName")

为什么取不到值呢,得到一个null

但是
ActionContext ac=ActionContext.getContext();  
HttpServletRequest request=(HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
System.out.println("@@"+request.getParameter("userName"));


这样就可以呢?

而且这样也是可以的
ActionContext ac=ActionContext.getContext();  
HttpServletRequest request=(HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
System.out.println("@@"+request.getAttribute("userName"));

那么

ActionContext.getContext().get("userName")


这样实在哪里取得的值呢?
...全文
815 34 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
hupei945 2013-10-25
  • 打赏
  • 举报
回复
大神,问你个问题, 我通过getHibernateTemplate()方法里面哪个方法可以 实现通过表内某个字段查询
hupei945 2013-10-23
  • 打赏
  • 举报
回复
大神,我是来看你的,谢谢你帮我解决了我的问题
GoldenDragon 2013-02-23
  • 打赏
  • 举报
回复
ActionContext context = ActionContext.getContext();得到的是一个线程安全容器。 它的内部结构是是一个OGNL值栈,本质上是一系列的MAP,所以楼主直接取一个键值对的值是取不到的。 对于页面请求所传的参数,Struts2会调用params拦截器压入值栈中的params map中,然后通过反射调用action里的set方法给属性赋值。所以想要通过get(key)的形式获取值,必须先获取params,即"getParameters()"。 另外,ActionContext的实现主要是为了与底层web解耦,所以不应该与底层web对象httprequest打交道。如果楼主实在是想实现类似于request.getParameter("userName"))的用法,可以使用ServletActionContext对象,它可以与servlet api打交道,可以通过ServletActionContext. getRequest()获取HttpRequest对象,实现楼主的用法。
qinqi0517 2013-02-22
  • 打赏
  • 举报
回复
你的action中没有传值肯定取不到啊!
飘飘哥 2013-02-20
  • 打赏
  • 举报
回复
put<key,value> 我亲爱的楼主
飘飘哥 2013-02-20
  • 打赏
  • 举报
回复
引用 5 楼 dracularking 的回复:
是我看错了还是你写错了 put时key是message对吧 ActionContext.getContext().put("message", userName); get时key变成了userName?
你看看
长笛党希望 2013-02-20
  • 打赏
  • 举报
回复
ActionContext.getContext().put("xxx", xxVALUE) 相当于 request.setAttribute("xxx",xxVALUE) 相比楼主传过来参数的形式是?xxx=xxVALUE吧。。。 不能用get方法去取
channel321 2013-02-18
  • 打赏
  • 举报
回复
action的开头设置个断点。一目了然啊。。2楼的比喻很正确了。但是可能后面那个楼主不明白的put问题没搞清,之所以PUT可以取到,是因为他会Put在两个地方,包括根对象和request对象。。记得当时看过书好像是这样的。如果你不信我的讲法,你也可以按照楼上的,在页面写个debug来就能清晰的查出这个Put是放在什么位置。。
lvzg_005 2013-02-04
  • 打赏
  • 举报
回复
非常欣赏lz善于发现问题的能力。 ActionContext.getContext.get()方法可获取当前ActionContext中key对应的value。同时该方法可以获取HttpServletRequest中的属性。 ActionContext.getContext.put()方法将key-value存放在ActionContext中,该方法可用于向HttpServletRequest存入属性。 HttpServletRequest中的getAttribute(String s)和getParameter(String s)方法本质是不一样,不然为啥写两个方法呢。 一个是获取request范围内属性值,一个是获取请求参数值。 上述说明和LZ的实例验证的结果有出入,就是调用get方法可以拿到值,而调用request.getAttribute却拿到值了。因此本人同样 编写了简单的demo如下测试

 ActionContext ac = ActionContext.getContext();
		HttpServletRequest request = 
		(HttpServletRequest)ac.get(ServletActionContext.HTTP_REQUEST);
		
		String para = (String)request.getAttribute("para");
		logger.info("attribute[para]=" + para);
		String par2 = (String)request.getParameter("para");
		logger.info("parameter[para]=" + par2);
		String par3 = (String)ac.get("para");
		logger.info("context.get[para]=" + par3);
		ac.put("mes", "putValue");
		String mes = (String)request.getAttribute("mes");
		logger.info("attribute[mes]=" + mes);
输出结果如下: attribute[para]=null //requst属性中没有这个值。 parameter[para]=test //请求参数中的值 context.get[para]=null //同1 attribute[mes]=putValue //自行设置的属性值,可以获取 结果和最开始介绍的get,set方法保持一致,就是get,set方法其实是操作的HttpServletRequest中的属性。 lz的例子中通过getAttribute能获取到值,是不是在别的地方调用setAttribute方法了?
coooliang 2013-02-03
  • 打赏
  • 举报
回复
撸主头像很牛B也。
  • 打赏
  • 举报
回复
http://blog.csdn.net/smcfy/article/details/5693481 参考学习。
fx_xf 2013-01-31
  • 打赏
  • 举报
回复
同学习! 首先非常赞同2楼回答,例子也很形象。 我觉得ActionContext.getContext().put("xxx", yyy)是put进了context最下层,而request是在中间层,这样用request.getAttribute能取出put进去的值。 相同的是ActionContext.getContext().get首先会去request中间这一层找,发现找不到username所以取不出,而用ActionContext.getContext().request.get就到最下层去找到username自然就取出来啦.
xwqfudimo 2013-01-31
  • 打赏
  • 举报
回复
Action.getContext().get("xxx")会到CompoundRoot中去取值,而request.getParameter("xxx")到ValueStack中去取,如果userName是action的一个属性,用Action.getContext().get("xxx")就能取到
王奕然 2013-01-30
  • 打赏
  • 举报
回复
我觉得这个没必要深究他,ActionContext.getContext().put("message", userName);是放到context里 你用<s:debug/>看就行了
SADSDSDDASDASD 2013-01-29
  • 打赏
  • 举报
回复
我就是想知道 put的时候 是放在了哪个值栈里 get的时候默认是在哪个取得的
SADSDSDDASDASD 2013-01-29
  • 打赏
  • 举报
回复
引用 15 楼 AA5279AA 的回复:
引用 13 楼 xiaofeifei123 的回复:引用 11 楼 AA5279AA 的回复: 引用 8 楼 xiaofeifei123 的回复:引用 5 楼 dracularking 的回复: 是我看错了还是你写错了 put时key是message对吧 ActionContext.getContext().put("message", userName); ……
也在ActionContext 这里里面 但是不能直接取到
snow-is-my-Love 2013-01-29
  • 打赏
  • 举报
回复
lz 还是看看ActionContext.getContext();的对象 都封装了什么吧
失落夏天 2013-01-28
  • 打赏
  • 举报
回复
引用 13 楼 xiaofeifei123 的回复:
引用 11 楼 AA5279AA 的回复: 引用 8 楼 xiaofeifei123 的回复:引用 5 楼 dracularking 的回复: 是我看错了还是你写错了 put时key是message对吧 ActionContext.getContext().put("message", userName); get时key变成了userName? 兄弟 你没买看明白吧 我没……
哦,那样确实没问题,但是你ActionContext里面的对象userName是什么时候放进去的呢? struts2自动放进去的userName好像并不在ActionContext这个大的对象里面
tangwei070 2013-01-28
  • 打赏
  • 举报
回复
2楼说的就是对的。 如果要通过action上下文来获取页面参数,应该通过的事下面的方法来获取。 ActionContext context = ActionContext.getContext(); Map params = context.getParameters(); String username = (String) params.get(“username”);
SADSDSDDASDASD 2013-01-28
  • 打赏
  • 举报
回复
引用 11 楼 AA5279AA 的回复:
引用 8 楼 xiaofeifei123 的回复:引用 5 楼 dracularking 的回复: 是我看错了还是你写错了 put时key是message对吧 ActionContext.getContext().put("message", userName); get时key变成了userName? 兄弟 你没买看明白吧 我没有那么二 user……
ActionContext.getContext().put("message", userName); 这句话但我没写 好吗
加载更多回复(12)

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧