繁体   English   中英

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

[英]How to add Object in using model.addAttributes in Spring MVC

我正在尝试从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}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM