开发者社区> 问答> 正文

关于Spring mvc中session值赋值和获取问题?报错

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)

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

展开
收起
爱吃鱼的程序员 2020-06-22 21:32:34 629 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    这个配置应该是没有问题的,你把项目名@mvc改下看我把项目名那个@去掉就没问题了,谢谢您。

    <spanstyle="font-family:Arial,'LiberationSans','DejaVuSans',sans-serif;font-size:14px;line-height:18px;">@SessionAttribute必须绑定一个已经存在的对象,如果不存在,那么需要先定义<spanstyle="background-color:#FFFFFF;font-family:Arial,'LiberationSans','DejaVuSans',sans-serif;font-size:14px;line-height:18px;">

    <spanstyle="background-color:#FFFFFF;font-family:Arial,'LiberationSans','DejaVuSans',sans-serif;font-size:14px;line-height:18px;">在你的代码中加入如下方法试试

    <spanstyle="background-color:#FFFFFF;font-family:Arial,'LiberationSans','DejaVuSans',sans-serif;font-size:14px;line-height:18px;">

    <preclass="brush:java;toolbar:true;auto-links:false;">@ModelAttribute("first")publicStringpopulateForm(){return"";//populatesformforthefirsttimeifitsnull}

    <spanstyle="font-family:Arial,'LiberationSans','DejaVuSans',sans-serif;font-size:14px;line-height:18px;background-color:#FFFFFF;">

    <arel="nofollow">http://localhost:8080/@mvc/Test2/secondMethod.do?first=first就能访问了.你在方法上写了<spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14.44444465637207px;line-height:21.99652862548828px;background-color:#F9F9F9;">@ModelAttribute("first"),是让接收参数的.

    <spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14.44444465637207px;line-height:21.99652862548828px;background-color:#F9F9F9;">不过你url中带了@竟然能访问我还是第一回见. 

    <divclass="ref">

    引用来自“震秦”的答案

    <divclass=ref_body>

    <arel="nofollow">http://localhost:8080/@mvc/Test2/secondMethod.do?first=first就能访问了.你在方法上写了<spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14.44444465637207px;line-height:21.99652862548828px;background-color:#F9F9F9;">@ModelAttribute("first"),是让接收参数的.

    <spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14.44444465637207px;line-height:21.99652862548828px;background-color:#F9F9F9;">不过你url中带了@竟然能访问我还是第一回见. 

    问题解决了,我之前那样写就可以了,是项目名字不对,去掉那个@就可以了,谢谢您啦!

    2020-06-22 21:32:51
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
workshop专场-微服务专场-开发者动手实践营-微服务-Spring Cloud Alibaba 微服务全家桶体验 立即下载
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载

相关实验场景

更多