2013-10-22 39 views
1

我想實現使用Spring MVC的Hello世界的例子,但它沒有給出所需的結果。MVC使用春天你好世界的例子

這是我的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>Print: ${message} world</h2> 
</body> 
</html> 

HelloController.java

package com.sbv.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping("/hello") 
public class HelloController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String printHello(ModelMap model) 
    { 
     model.addAttribute("message", "Hello"); 
     return "index"; 
    } 
} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>LoginSpringMVC</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
    <servlet-name>Hello</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>Hello</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

HelloServlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

    <context:component-scan base-package="com.sbv.controller" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

這我得到的輸出是

打印:世界

的$ {消息}是沒有得到印刷

有人請幫我這

在此先感謝

+0

只是要確保你提供正確的觀點:你能否一些文本添加到您的JSP文件並驗證它顯示? –

回答

3

message屬性添加到MODELprintHello方法:

@Controller 
@RequestMapping("/hello") 
public class HelloController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String printHello(ModelMap model) 
    { 
     model.addAttribute("message", "Hello"); 
     return "index"; 
    } 
} 

,當你做出一個/hello (http://host:port/appContext/hello)要求GET將得到執行。但是,你必須在web.xml文件下面的條目:

<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file> 

和Web容器將使用此文件以追加爲/的請求,以顯示index.jsp給用戶,因爲你沒有任何處理方法映射到/ URL。因此,爲了顯示信息給用戶,從web.xml刪除<welcome-file-list>進入和改變HelloController這樣:

@Controller 
@RequestMapping("/") 
public class HelloController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String printHello(ModelMap model) 
    { 
     model.addAttribute("message", "Hello"); 
     return "index"; 
    } 
} 
1

擺脫

<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file> 

,讓你的要求

http://yourhost:yourport/YourApp/hello 

,以配合您@Controller映射。

如果你讓你的要求

http://yourhost:yourport/YourApp 

和任何<welcome-file>項的存在,你的Servlet被擊中之前那些將被選擇。