Spring MVC 404错误

dan*_*nik 9 java spring jsp servlets spring-mvc

我疯了,无法理解问题所在:

我有以下结构:

SpringMVC
  +WebContent
      -web-inf
        -web.xml
        -springMVC-servlet.xml
      -index.jsp
      -security
           -login.jsp 
Run Code Online (Sandbox Code Playgroud)

web.xml中

<display-name>springMVC</display-name> 
<servlet> 
  <servlet-name>springMVC</servlet-name> 
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping> 
  <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>*.do</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
  <servlet-name>springMVC</servlet-name> 
  <url-pattern>/index.html</url-pattern>
</servlet-mapping>
<welcome-file-list> 
  <welcome-file>index.html</welcome-file> 
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)

用SpringMVC-servlet.xml中

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

    <context:annotation-config/> 
    <context:component-scan base-package="com.vanilla.springMVC"/> 

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

</beans>
Run Code Online (Sandbox Code Playgroud)

我的控制器:

package com.vanilla.springMVC;

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

@Controller
public class DefaultController {

    @RequestMapping(value="/index.html", method=RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }

    @RequestMapping(value="/login.html", method=RequestMethod.GET)
    public ModelAndView loginPage(){
        ModelAndView mv = new ModelAndView("/security/login");
        return mv;
    }
}
Run Code Online (Sandbox Code Playgroud)

导航到/index.html没有问题

http:// localhost:8080/SpringMVC/index.html 完美无缺.

但是当我导航到 http:// localhost:8080/SpringMVC/login.html时, 我有404错误.

HTTP Status 404 - /SpringMVC/login.jsp    
type Status report
message /SpringMVC/login.jsp
description The requested resource (/SpringMVC/login.jsp) is not available.
Run Code Online (Sandbox Code Playgroud)

我不想将login.jsp移动到与index.jsp相同的级别,但为什么我有这个问题?

Nic*_*aly 5

我只是在这里添加它,因为它解决了我的404问题.原来我的问题是url-mapping价值web.xml.使用Spring WebMVC版本org.springframework:spring-webmvc:4.1.6.RELEASE

我使用以下(不起作用):

<url-pattern>/rest</url-pattern>
Run Code Online (Sandbox Code Playgroud)

我应该使用以下值(有效):

<url-pattern>/rest/*</url-pattern>
Run Code Online (Sandbox Code Playgroud)

要么

<url-pattern>/</url-pattern>
Run Code Online (Sandbox Code Playgroud)

参考:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html


Ral*_*lph 4

HTTP 404 表示未找到资源。

  • 这意味着找不到控制器或直接访问的资源(如 CSS 文件)
  • 这并不意味着控制器中引用的 JSP 未找到(这将是 500 内部服务器错误)

HTTP 状态 404 - /SpringMVC/login.jsp

看起来您发送了一个 HTTP 请求,/SpringMVC/login.jsp但您的控制器方法绑定到.html,因此您需要将 HTTP 请求更改为/SpringMVC/login.html

由于名称(登录)可能您的 Spring Security 配置不正确。