MVC使用Spring hello world示例

时间:2013-10-22 08:55:44

标签: spring jsp spring-mvc

我正在尝试使用Spring MVC实现hello world示例,但它没有提供所需的结果。

这是我的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>

我得到的输出是

打印:世界

$ {message}没有打印

有人请帮我解决这个问题

提前致谢

2 个答案:

答案 0 :(得分:3)

message方法中MODEL属性已添加到printHello

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

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

将在您GET发出/hello (http://host:port/appContext/hello)请求时执行。但是您在web.xml文件中有以下条目:

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

并且Web容器将使用此文件追加/向用户显示index.jsp的请求,因为您没有将任何处理程序方法映射到/ URL 。因此,为了向用户显示消息,请从<welcome-file-list>中删除web.xml条目,然后将HelloController更改为:

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

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

答案 1 :(得分:1)

摆脱

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

并提出要求

http://yourhost:yourport/YourApp/hello

以匹配您的@Controller映射。

如果您提出要求

http://yourhost:yourport/YourApp

并且存在任何<welcome-file>条目,这些条目将在Servlet被点击之前被选中。