2014-11-21 103 views
1

我已經用Maven創建了一個基本的Spring MVC Web應用程序。然而,當我訪問/ login URL時,我一直在努力處理404錯誤。 應該將/ login URL映射到AuthController servlet和createLoginForm()方法,但不幸的是它失敗了,我最終遇到了404錯誤。Spring MVC - 在Tomcat上獲得不斷的404錯誤

我想弄清楚是什麼問題導致404錯誤,爲什麼/登錄URL不能映射到servlet。

該問題位於web.xml或spring-dispatcher-servlet.xml文件的錯誤配置中,導致該URL無法映射?

當我訪問/ URL時,index.jsp文件被映射,並且工作正常 login.jsp文件放在WEB-INF目錄之外,在webapp之一以及索引.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> 
+0

_登錄。jsp文件放在WEB-INF directory_之外,那麼你的'InternalResourceViewResolver'就沒用了。 – 2014-11-21 00:53:46

+0

感謝您的回覆。但是,當我將login.jsp文件移到WEB-INF目錄中時,我仍然收到404錯誤。 – Wjoz 2014-11-21 00:56:08

+0

你打算使用'default-servlet-handler'做什麼?如果沒有,請將其刪除。 – 2014-11-21 00:57:13

回答

1

這是除了Shazin's answer

首先,把你的JSP文件中WEB-INF。沒有理由讓它們超出它,因爲你故意破壞了你的視圖渲染功能。

其次,

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

意味着該URL www.somedomain.com/將加載索引頁,和所有其他的URL將與啓動。但是,您的表單

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

正在擊中URL www.somedomain.com/springmvcreview/login。這與控制器URL映射不匹配

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

不包含表單操作的映射。所以要麼從表格動作中刪除/springmvcreview,要麼將@RequstMapping的值修改爲/springmvcreview/login

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" />