1

我正在尝试使用 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} 没有被打印出来

有人请帮我解决这个问题

提前致谢

4

2 回答 2

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";
    }
}
于 2013-10-22T12:54:12.213 回答
1

摆脱

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

并提出您的要求

http://yourhost:yourport/YourApp/hello

以匹配您的@Controller映射。

如果您提出要求

http://yourhost:yourport/YourApp

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

于 2013-10-22T12:27:49.777 回答