Transfer data from Controller to Jsp addAttribute method in Spring MVC

734 views Asked by At

I create Spring MVC project like this. This is Controller:

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public String doHello(Model model) {
        model.addAttribute("message", "Hello Spring MVC");
        return "helloworld";
    }
}

And this is content of file "helloworld.jsp".

<body> <h1>${message }</h1> </body> // I don't know why I can't post full code

When i run URL "http://localhost:8080/HelloSpringMVC/hello", the result should be Hello Spring MVC but I got ${message }, what's going on?

3

There are 3 answers

1
Sainik Kumar Singhal On

Make sure you have javax expression language dependency in your project.

0
Thanh Dat Dao On

add <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> to jsp file.

  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
<h1>
    Hello ${message }
</h1>

</body>
</html>
0
Cam On

try this:

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView doHello() {
        ModelAndView model = new ModelAndView("helloworld");  
        model.addObject("message", "Hello Spring MVC");
        return model;
    }
}