我在创建 spring
mvc
项目时遇到问题,我的 JSP
页面未按预期呈现,下面是代码
我的 Controller 是
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/welcome")
public ModelAndView helloWorld(){
ModelAndView model=new ModelAndView("HelloPage");
model.addObject("msg","hello page");
return model;
}
}
jsp页面是
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2> {$msg}</h2>
</body>
</html>
下面是输出
最佳答案
你几乎明白了,但这里有一个拼写错误:
<h2> {$msg}</h2>
Spring 使用 ${nameOfAttr}
来引用 bean/model 属性,因此...您必须编写:
<h2> ${msg}</h2>
您的消息将按预期显示。欲了解更多信息请check this link
关于java - spring 模型对象未在 jsp 页面上呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36135552/