2016-03-21 48 views
0

我在创建springmvc项目面临的问题上,如下预期我JSP页面不会被渲染为代码弹簧模型对象不是撕心裂肺的jsp页面

我控制器

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> 

下面是输出

enter image description here

回答

3

你几乎得到它,但你在这里有一个错字:

<h2> {$msg}</h2> 

Spring使用${nameOfAttr}指豆/模型属性,所以......你必须写:

<h2> ${msg}</h2> 

并且您的消息将按预期显示。欲了解更多信息请check this link

+0

谢谢你,我知道它的愚蠢的错误 –

+1

很高兴听到... :)发生所有的时间,新鲜的眼睛通常帮助超过伟大的知识 –

+1

哈哈很好的报价 –