2013-10-22 35 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被击中之前那些将被选择。