java - Spring : Unable to resolve view name in Spring MVC

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

我确实经历了一些解决方案,但没有一个对我的情况有效。 在运行这个简单的 spring mvc 示例时,我在浏览器上收到“未找到”错误。我是 Spring 框架的新手。任何帮助将不胜感激。

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

WARN [org.springframework.web.servlet.PageNotFound] (default task-4) No mapping found for HTTP request with URI [/Demo1/] in DispatcherServlet with name 'SpringDispatcher' WARN [org.springframework.web.servlet.PageNotFound] (default task-5) No mapping found for HTTP request with URI [/Demo1/HelloPage] in DispatcherServlet with name '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>

spring-dispatcher-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;

    }
}

主页.jsp

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

    <h2>${msg}</h2>

</body>
</html>

这是我的目录结构:

Directory Structure Image Link

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

谢谢!

最佳答案

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

关于java - Spring : Unable to resolve view name in Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44921924/

相关文章:

java - Spring MVC ModelAttribute 作为接口(interface)

java - 使用 Hibernate 将 MySQL latin1_bin 转换为 utf-8

java - 如何检查 netbeans 中的编译器和运行时?

java - 如何在 Spring 中为包(包括子包)中的每个 bean id 添加常量字符串前缀?

java - Spring Maven 清理错误 - 请求的配置文件 "pom.xml"无法激活,因为它不存在

java - 尽管保留了所有内容,但无法使用 log4j 生成日志文件

Spring-MVC:什么是 "context"和 "namespace"?

java - 验证,如果无效则通知,返回循环

java - WebDriver 中的错误监听器和测试执行之间的同步

java - 分页 MySQL 数据库结果