簡體   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