Pavel Petrashov
Pavel Petrashov

Reputation: 139

Please help me with Spring MVC

I'm trying to deal with Spring MVC. I made a simple example

@Controller
@RequestMapping("/")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        model.addAttribute("name", "Pavel!");
        return "hello";
    }
}

and

<html>
<body>
    <h1>${message}</h1>
    <h1>${name}</h1>
</body>
</html>

and all the cool running. but then I wanted to add a method

@RequestMapping(method = RequestMethod.GET)
public String printUser(User user) {
    user.setName("Pavel");
    user.setSname("Petrashov");
    user.setAge(24);
    return "user";
}

and now I do not know how to get the data in user.jsp

I have just two hours ago, began to study and still poorly guided. please help

Upvotes: 0

Views: 55

Answers (2)

Ascalonian
Ascalonian

Reputation: 15174

Add ModelMap model as a parameter in your printUser, then do model.addAttribute("user", user);

Then in your JSP, you just do: ${user.name} like <h1>${user.name}</h1>

Upvotes: 1

Neeraj Jain
Neeraj Jain

Reputation: 7730

Model Map is there to help you out

 @RequestMapping(method = RequestMethod.GET)
    public String printUser(User user,ModelMap model){
        user.setName("Pavel");
        user.setSname("Petrashov");
        user.setSname("Petrashov");
        user.setAge(24);

        model.addAttribute("user",user);
        return "user"; 
    }

Then just use Expression language in jsp like @Ascalonian Suggested

${user.name}
${user.age}

Upvotes: 1

Related Questions