Spring MVC:找不到带URI的HTTP请求的映射[/hello.jsp]

时间:2016-08-16 20:55:38

标签: java spring spring-mvc

我正在尝试使用Spring MVC& amp; Tomcat,但我似乎无法创建单个处理程序来呈现简约页面。

已创建以下文件:

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="springapp" version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <servlet>
        <servlet-name>springapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springapp-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springapp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
    </servlet-mapping>
    <display-name>Spring MVC Framework Test</display-name>
</web-app>

springapp-servlet.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" xmlns:mvc="http://www.springframework.org/schema/cache"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!--<mvc:annotation-driven />-->
    <context:component-scan base-package="springapp.web.*"/>

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

    <bean name="/hello.jsp" class="springapp.web.HelloController"/>
</beans>

的hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JSP Title</title>
</head>
<body>

${message}

</body>
</html>

HelloController.java

package springapp.web;

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
public class HelloController {
    @RequestMapping(value="/hello", method=RequestMethod.GET)
    public String printHello(ModelMap model) {
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "hello";
    }
}

文件夹结构:

enter image description here

我尝试找到处理程序:

16-Aug-2016 16:31:18.867 WARNING [http-nio-8080-exec-8] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/springapp/hello.jsp] in DispatcherServlet with name 'springapp'
16-Aug-2016 16:31:22.991 WARNING [http-nio-8080-exec-9] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/springapp/web/hello.jsp] in DispatcherServlet with name 'springapp'
16-Aug-2016 16:31:23.023 WARNING [http-nio-8080-exec-10] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/springapp/web/hello.jsp] in DispatcherServlet with name 'springapp'

有人可以告诉我我做错了吗?

2 个答案:

答案 0 :(得分:2)

您的代码应该是这样的: 您还应该使用 / springapp / hello 而不是 /springapp/hello.jsp

运行它

控制器:

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

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

       @RequestMapping(method = RequestMethod.GET)
       public String printHello(ModelMap model) {
          model.addAttribute("message", "Hello Spring MVC Framework!");

          return "hello";
       }

    }

的web.xml

<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Spring MVC Application</display-name>

   <servlet>
      <servlet-name>springapp</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>springapp</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

的HelloWeb-servlet.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.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="springapp.web" />

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

</beans>

你的jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

答案 1 :(得分:1)

正如我可以看到您的文件夹结构和springapp-servlet.xml,我认为您必须将hello.jsp文件放在WEB-INF文件夹中,因为这可能是找不到页面的原因(也许显示这种类型的错误404)。根据springapp-servlet.xml,您指示您的jsp文件应该在WEB-INF文件夹中。