2016-03-21 45 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

哈哈很好的報價 –