4

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());

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

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

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 ?

0

4 Answers 4

9

In controller add as below

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

In jsp use like this

    ${user.name}
Sign up to request clarification or add additional context in comments.

2 Comments

Variable modelMap should be model here.
@Prabhakaran can we insert space or line break in model attribute...bcoz my model returns an entire string to jsp page...but I need to have space b/w each news.....model.addAttribute("news",news+"\n");
0

Or there is another option - to use @ModelAttribute like this: http://krams915.blogspot.com/2010/12/spring-3-mvc-using-modelattribute-in.html (contains Model and ModelAttribute examples).

1 Comment

That was a great blog. This was the one I was searching for long time
0

Don't forget the JSP option isELIgnored="false" as below:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" isELIgnored="false"
pageEncoding="ISO-8859-1"%>

1 Comment

@LawrenceAiello I agree. The author's suggestion to disable the expression language does not in any way help the OP in adding properties to a model object, retrievable from a JSP by referencing the object's name and property. It seems to intentionally try cause more problems by preventing them from being able to parse any EL in the page it's applied to. see docs.oracle.com/cd/E19316-01/819-3669/bnaic/index.html
-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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.