Spring MVC - 执行的视图无效(违反指定的请求映射)

时间:2017-05-28 22:19:09

标签: java spring spring-mvc intellij-idea

我一直试图调试它几个小时甚至试图一次又一次地创建同一个项目。由于IntelliJ IDEA或其他原因,我不知道这是否是问题。我试图谷歌,但无法找到任何解决方案。我在intelliJ中制作了一个简单的spring mvc演示应用程序。一个默认结构由intelliJ和一个视图" index.jsp"提供。在网络目录中。

project structure

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     id="WebApp_ID" version="3.1">

<display-name>spring-mvc-demo</display-name>

<!-- Spring MVC Configs -->

<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


</web-app>

弹簧MVC-演示servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />

<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Step 5: Define Spring MVC view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>
</beans>

DebugController.java

package com.luv2code.springdemo;

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

/**
 * Created by gaurav.ahirwar on 29/05/17.
 */

@Controller

public class DebugController {

    @RequestMapping(value="/", method = RequestMethod.GET)
    public String debug() {
        return "hello";
    }
}

问题在于每当我运行项目并点击localhost:8080 /然后执行index.jsp但是根据我的配置和映射,应该加载hello.jsp。我可能会犯一些愚蠢的错误,但我尽可能多地尝试,现在真的很沮丧。请帮帮我。感谢。

本地主机:8080

index.jsp loaded at root

1 个答案:

答案 0 :(得分:0)

尝试此映射:

@RequestMapping(value = {"", "/", "index.jsp"}, method = RequestMethod.GET)

我猜localhost:8080等于""路径,但不是/