4
回答
关于Spring mvc中session值赋值和获取问题
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("/Test2")
@SessionAttributes("first")
public class Test2Controller {

 @RequestMapping("/firstMethod")
 public String firstMethod (ModelMap map){
  System.out.println("执行firstMethod");
  map.addAttribute("first", "ok");
  return "search";
 }
 
 @RequestMapping("/secondMethod")
 public String secondMethod(@ModelAttribute("first") String first){
  System.out.println("执行了secondMethod");
  System.out.println("+++++"+first);
  return "search1";
 }
}

上面是我写的小例子,先是执行http://localhost:8080/@mvc/Test2/firstMethod.do,可以执行成功,但在执行http://localhost:8080/@mvc/Test2/secondMethod.do时就报错了,

报错内容如下:

2013-3-5 15:50:13 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet mvc threw exception
org.springframework.web.HttpSessionRequiredException: Expected session attribute 'first'
 at org.springframework.web.method.annotation.ModelFactory.initModel(ModelFactory.java:103)
 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:614)
 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
 at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
 at java.lang.Thread.run(Thread.java:619)

哪位高手指点一下,我这个是错在哪了,还是配置文件没配什么

举报
magelite10
发帖于5年前 4回/11K+阅
共有4个答案 最后回答: 5年前

@SessionAttribute 必须绑定一个已经存在的对象,如果不存在,那么需要先定义

在你的代码中加入如下方法试试

@ModelAttribute("first")
   public String populateForm() {
       return ""; // populates form for the first time if its null
   }


引用来自“震秦”的答案

http://localhost:8080/@mvc/Test2/secondMethod.do?first=first就能访问了. 你在方法上写了@ModelAttribute("first"), 是让接收参数的.

不过你url中带了@竟然能访问我还是第一回见. 

@Controller
@RequestMapping("/Test2")
@SessionAttributes("first")
public class Test2Controller {

 @RequestMapping("/firstMethod")
 public String firstMethod(ModelMap map) {
  System.out.println("执行firstMethod");
  map.addAttribute("first", "ok");
  return "search";
 }

 @RequestMapping("/secondMethod")
 public String secondMethod(@ModelAttribute("first")String first) {
  System.out.println("执行了secondMethod");
  System.out.println("+++++" + first);
  return "search1";
 }
 
 @ModelAttribute("first") 
 public String populateForm() { 
  return ""; // populates form for the first time if its null 
 }

}
我把代码改成了这样,在执行secondMethod时不会报错了,但是取不到我在firstMethod中给first存的值(值为ok)。此外,如果我将populateForm的返回值给为return "fail";那么我在secondMethod时,取到的first的值是fail。

--- 共有 1 条评论 ---
magelite10问题解决了,我之前那样写就可以了,是项目名字不对,去掉那个@就可以了,谢谢您啦! 5年前 回复
顶部