日期:2014-05-20  浏览次数:20877 次

spring mvc 关于在controller中获取request
页面:request.setAttribute("prodcut","1234142143123");
<a href="<%=basePath %>/thisisademo">


java:@RequestMapping("/thisisademo")
public String addProduct(HttpServletRequest request, HttpServletResponse response){
  System.out.println(request.getAttribute("product"));
  }

为什么控制台打出来为null值呢。
web.xml文件中加入了。
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>

请帮忙看看,谢谢了。

------解决方案--------------------
request.getSession().getAttribute("product");
------解决方案--------------------
用session试试呢?有可能是request被重新定义。清空了里面的值。而且页面向controller传数据,也可以放到url里面或action里。直接request.getParameter("prodcut");就能取到
------解决方案--------------------
request.setAttribute("prodcut","1234142143123");
改成
request.getSession().setAttribute("prodcut","1234142143123");
还有你的页面
页面:request.setAttribute("prodcut","1234142143123");
<a href="<%=basePath %>/thisisademo">
这里的request.setAttribute("prodcut","1234142143123");来添加一个属性,那么你<a href="<%=basePath %>/thisisademo">能把这个属性传过去吗??反正我觉得是传不过去的
------解决方案--------------------
你前台是用什么方式跳转到后台的?没见过前台request.setAttribute后台能接收的
反过来可以
前台到后台最好走url后面接或者隐藏表单都可以
------解决方案--------------------
<a href="<%=basePath %>/thisisademo?product=123412341234">
------解决方案--------------------
第一个:要是想用session取值。那在页面也要把值放到session中。
在页面里request.getSession().setAttribute("prodcut","1111");
然后在controller里request.getSession().getAttribute("prodcut");
第二个:要是想用request.getParameter("prodcut");取值。那楼主要确认一下参数是否在url或action里。如:
<form metod="post" action=".....? prodcut="+ prodcut+" >类似这种的。
------解决方案--------------------
在前台向后台传数据一般是不用setAttribute()这种方式,一般都是在action里传一个参数。在后台用request.getParameter()取。要是实在想用setAttribute()应该也是能实现的。但最好用session传值。因为request跳转界面时,会被重置。贴的代码有点少,不知道那儿出问题了。