我创建了这样的Spring MVC项目。这是控制器:
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public String doHello(Model model) {
model.addAttribute("message", "Hello Spring MVC");
return "helloworld";
}
}
这是文件"helloworld.jsp"的内容
<body> <h1>${message }</h1> </body> // I don't know why I can't post full code
当我运行URL时"http://localhost:8080/HelloSpringMVC/hello",结果应该是Hello Spring MVC
,但我得到了${message }
,发生了什么?
确保您的项目中有javax表达式语言依赖项。
添加<%@taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%>到jsp文件。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello ${message }
</h1>
</body>
</html>
试试这个:
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView doHello() {
ModelAndView model = new ModelAndView("helloworld");
model.addObject("message", "Hello Spring MVC");
return model;
}
}