Spring MVC - 在Tomcat上获得常量404错误

时间:2014-11-21 00:51:48

标签: java spring tomcat

我已经使用Maven创建了一个基本的Spring MVC Web应用程序。但是,当我访问/登录URL时,我一直在努力获取常数404错误。 / login URL应该映射到AuthController servlet和createLoginForm()方法,但不幸的是它失败了,我最终得到了404错误。

我试图弄清楚导致404错误的问题是什么,以及为什么/ login URL无法映射到servlet。

问题是否位于web.xml或spring-dispatcher-servlet.xml文件的错误配置中,导致无法映射URL?

当我访问/ URL时,会映射index.jsp文件并且它正常工作 login.jsp文件放在WEB-INF目录之外,在webapp目录和index.jsp文件中。

提前致谢。
AuthController.java

package com.github.wjoz.springmvcreview.auth;

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

@Controller
public class AuthController {

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public ModelAndView createLoginForm() {
        ModelAndView model = new ModelAndView("login");
        return model;
    }

}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-   app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>springmvcreview</display-name>
     <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
     <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

弹簧调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<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/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">

<mvc:default-servlet-handler/>
<context:component-scan base-package="main.java.com.github.wjoz.springmvcreview.auth" />
<mvc:annotation-driven />
 <!-- Tells the location of the view in the project -->
 <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
        <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
        <value>.jsp</value>
        </property>
 </bean>

</beans>

的login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head>
    <title>Welcome to our application. Sign in.</title>
</head>
<body>

  <form action="/springmvcreview/login" method="post">
    <div>
     <label for="username">Username</label>
     <input type="text" name="username" id="username">
     <label for="password">Password</label>
     <input type="password" name="password" id="password">
      <button type="submit">Sign in</button>
    </div>
  </form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

这是Shazin's answer

的补充

首先,将您的JSP文件放在WEB-INF中。没有理由让它们在它之外,因为你故意破坏你的视图渲染功能。

第二,

<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

表示网址www.somedomain.com/将加载索引页面,所有其他网址将以此开头。但是,你的表格

<form action="/springmvcreview/login" method="post">

正在点击网址www.somedomain.com/springmvcreview/login。这与控制器URL映射

不匹配
@RequestMapping(value="/login", method=RequestMethod.GET)
public ModelAndView createLoginForm() {

不包含表单操作的映射。因此,要么从表单操作中删除/springmvcreview,要么将@RequstMapping值修改为/springmvcreview/login

答案 1 :(得分:0)

您没有DispatcherServlet的contextConfigLocation。您需要告诉context:component-scan扫描com.github.wjoz.springmvcreview.auth包并加载AuthController

<servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value><PATH_TO>/spring-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

我又看到了一个问题。您不需要以下main.java部分;

<context:component-scan base-package="com.github.wjoz.springmvcreview.auth" />