如何在Spring MVC中使用model.addAttributes添加Object

我正在尝试从DB检索用户信息并使用Spring MVC在前端(JSP)中显示。

在控制器内部,目前我正在添加以下代码,

ModelMap model; model.addAttribute("email", user.getEmailAddress()); model.addAttribute("name", user.getuserName()); model.addAttribute("birthday", user.getBirthday()); model.addAttribute("street",user.getUserAddress().getStreet_address()); model.addAttribute("state", user.getUserAddress().getState()); model.addAttribute("city", user.getUserAddress().getCity()); model.addAttribute("zip", user.getUserAddress().getZip()); model.addAttribute("country", user.getUserAddress().getCountry()); 

在前端JSP中,我使用$ {email},$ {name},$ {birthday}等显示它们。 但是我想做这样的事情,

ModelMap模型; model.addAttribute( “用户”,用户);

在前端,显示为$ {user.getName()}。 但是这不起作用。 如果还有其他方法可以告诉我吗?

在控制器中添加如下

  ModelMap model = new ModelMap(); model.put("user", user); 

在jsp中使用这样的

  ${user.name} 

或者还有另一种选择 – 像这样使用@ModelAttribute: http ://krams915.blogspot.com/2010/12/spring-3-mvc-using-modelattribute-in.html(包含Model和ModelAttribute示例)。

不要忘记JSP选项isELIgnored="false" ,如下所示:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" isELIgnored="false" pageEncoding="ISO-8859-1"%> 
 **In Controller do IT** @RequestMapping(value = "/loginStep.do", method = RequestMethod.GET) public String loginStep(ModelMap model,HttpServletRequest request, HttpServletResponse response,HttpSession session) { model.addAttribute("YearList", yearList); return "uploadPDFPage"; } **In uploadPDFPage JSP Do it** ${YearList}