Spring:无法解析Spring MVC中的视图名称

时间:2017-07-05 09:16:09

标签: java spring spring-mvc http-status-code-404 spring-annotations

我确实经历过一些解决方案,没有一个能够解决我的问题。 我一直在找" Not Found"运行这个简单的spring mvc示例时浏览器出错。我是Spring框架的新手。任何帮助都将受到高度赞赏。

以下是我在运行程序时在控制台上遇到的错误:

  

WARN [org.springframework.web.servlet.PageNotFound](默认任务-4)在DispatcherServlet中找不到带有URI [/ Demo1 /]的HTTP请求的映射,名称为' SpringDispatcher'   WARN [org.springframework.web.servlet.PageNotFound](默认任务-5)在DispatcherServlet中找不到带有URI [/ Demo1 / HelloPage]的HTTP请求的映射,名称为' SpringDispatcher'

这是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>Demo1</display-name>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>SpringDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.kurshit.springmvc.Demo1</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

弹簧调度-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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd"> 

 <mvc:default-servlet-handler />
 <mvc:annotation-driven/>  
<context:component-scan base-package = "com.kurshit.springmvc.Demo1.controller" />

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

</beans>

HomeController.java

package com.kurshit.springmvc.Demo1.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {

    @RequestMapping(value="/welcome.html")
    public ModelAndView test(HttpServletResponse response) throws IOException{
        // TODO Auto-generated method stub
        ModelAndView mav = new ModelAndView("HelloPage","msg","Welcome To Spring");


        return mav;

    }
}

HomePage.jsp

<html>
<body>
    <h1>First Spring Example with Deployment Descriptor</h1>

    <h2>${msg}</h2>

</body>
</html>

以下是我的目录结构的样子:

Directory Structure Image Link

我正在使用Maven工具来解决依赖性:

谢谢!

1 个答案:

答案 0 :(得分:2)

您的jsp名为HomePage.jsp,但您在ModelAndView中返回HelloPage。这些名称需要匹配。 (尽管在ModelAndView中不需要.jsp扩展名)