如何在Spring MVC中使用model.addAttributes添加对象 [英] How to add Object in using model.addAttributes in Spring MVC

查看:1519
本文介绍了如何在Spring MVC中使用model.addAttributes添加对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Spring MVC从数据库中检索用户信息并显示在前端(JSP)中.

I'm trying to retrieve the user information from DB and display in the front end (JSP) using Spring MVC.

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

Inside the controller, currently I'm adding the following code,

                     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}等显示它们.但是我想做这样的事情,

In the front-end JSP, I display them using ${email} ,${name} ,${birthday} etc . However I would like to do something like this,

ModelMap模型; model.addAttribute("user",user);

ModelMap model; model.addAttribute("user",user);

并在前端显示为$ {user.getName()}.但是,这是行不通的.你能告诉我是否还有其他方法可以做到这一点?

and in front end , display as ${user.getName()}. However this is not working . Can you let me know if there are any other ways to do this ?

推荐答案

在控制器中添加如下内容

In controller add as below

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

在jsp中像这样使用

    ${user.name}

这篇关于如何在Spring MVC中使用model.addAttributes添加对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆