/user/login可以顺利执行!
/user/get总是报错,错误为
org.springframework.web.HttpSessionRequiredException: Expected session attribute 'Username'
org.springframework.web.method.annotation.ModelFactory.initModel(ModelFactory.java:103)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:726)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import yangzhou.model.Message;
import yangzhou.service.UserServiceI;
/**
* Created by SunBo on 2017/6/10.
*/
@Controller
@RequestMapping("/user")
@Scope("prototype")
@SessionAttributes({"Username"})
public class UserController {
@Autowired
private UserServiceI userServiceI;
@ResponseBody
@RequestMapping("/login")
public Message login(@Param("username")String username,@Param("password")String password, ModelMap modelMap){
boolean loginType = userServiceI.login(username,password);
if(loginType){
modelMap.addAttribute("Username",username);
return new Message(true,"登陆成功");
}
else {
return new Message(false,"登陆失败");
}
}
@ResponseBody
@RequestMapping("/check")
public Message get(@ModelAttribute("Username") String username,ModelMap modelMap){
System.out.println("username:"+ username);
return new Message(false,"登陆失败");
}
}
希望大神可以指点一下!我已经找了很多网上的资料,还是找不出问题所在.谢谢啦!
3 个解决方案
#1
@Param("username")String username
@ModelAttribute("Username") String username
这两个username是一个值吗?一个大写一个小写的。。
@ModelAttribute("Username") String username
这两个username是一个值吗?一个大写一个小写的。。
#2
@SessionAttributes({"Username"})
modelMap.addAttribute("Username",username);
@ModelAttribute("Username")
这三个都是大写就行了吧.
@Param("username")String username这个大小写只跟URL有关吧.求大神赐教
modelMap.addAttribute("Username",username);
@ModelAttribute("Username")
这三个都是大写就行了吧.
@Param("username")String username这个大小写只跟URL有关吧.求大神赐教
#3
@ResponseBody
@RequestMapping("/check")
public Message get(@ModelAttribute("Username") String username,ModelMap modelMap){
System.out.println("username:"+ username);
return new Message(false,"登陆失败");
}
@ModelAttribute("Username") String username 参数的username改成首字母大写,@ModelAttribute注解里的字符串必要要和参数里一致,不然肯定找不到
@RequestMapping("/check")
public Message get(@ModelAttribute("Username") String username,ModelMap modelMap){
System.out.println("username:"+ username);
return new Message(false,"登陆失败");
}
@ModelAttribute("Username") String username 参数的username改成首字母大写,@ModelAttribute注解里的字符串必要要和参数里一致,不然肯定找不到
#1
@Param("username")String username
@ModelAttribute("Username") String username
这两个username是一个值吗?一个大写一个小写的。。
@ModelAttribute("Username") String username
这两个username是一个值吗?一个大写一个小写的。。
#2
@SessionAttributes({"Username"})
modelMap.addAttribute("Username",username);
@ModelAttribute("Username")
这三个都是大写就行了吧.
@Param("username")String username这个大小写只跟URL有关吧.求大神赐教
modelMap.addAttribute("Username",username);
@ModelAttribute("Username")
这三个都是大写就行了吧.
@Param("username")String username这个大小写只跟URL有关吧.求大神赐教
#3
@ResponseBody
@RequestMapping("/check")
public Message get(@ModelAttribute("Username") String username,ModelMap modelMap){
System.out.println("username:"+ username);
return new Message(false,"登陆失败");
}
@ModelAttribute("Username") String username 参数的username改成首字母大写,@ModelAttribute注解里的字符串必要要和参数里一致,不然肯定找不到
@RequestMapping("/check")
public Message get(@ModelAttribute("Username") String username,ModelMap modelMap){
System.out.println("username:"+ username);
return new Message(false,"登陆失败");
}
@ModelAttribute("Username") String username 参数的username改成首字母大写,@ModelAttribute注解里的字符串必要要和参数里一致,不然肯定找不到