自己在做項目的時候,在jsp頁面通。過${xxx}獲取不到值,困擾了自己好久。
自己jsp頁面如下:
<input id="uuid" type="hidden" value="${user.uId}" name="userId"/>
controller如下:
@RequestMapping(value = "/checkLogin",method = RequestMethod.POST)
public String checkLogin(@RequestParam(value = "userName") String userName, @RequestParam (value = "password") String password,HttpSession session){
String passwordMd5=DigestUtils.md5DigestAsHex(password.getBytes());//Spring自帶的Md5加密
User user = userService.checkLogin(userName, passwordMd5);
System.out.println(user.toString());
if(user!=null){
/*
兩種方式向session域中存數據
1、通過HttpSession。
httpSession.setAttribute(key,value)
2、通過ModelMap。
modelMap.addAttribute(key,value)
*/
session.setAttribute("user",user);
return "redirect:HomePage1";
}
return "user_jsp/user_login";
}
但是頁面就是不顯示session中的值。
- 首先,我忘記在jsp中忘了加 <%page isELIgnored="false"%>,isELIgnored的默認值爲true,導致了在jsp頁面會把${xxx}解析成正常的文本。
- 即使加了<%page isELIgnored="false"%>,我在另一個頁面又碰到相同的問題,解決如下:在使用Servlet規範的時候,要使用新的規範Servlet3.1,不要使用默認的,可以將web.xml文件修改如下,就能解決問題。
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
</web-app>