2017-06-15 167 views
0

我已經開始在基於Maven的項目中使用Spring MVC。我的項目目錄結構 enter image description hereSpring MVC 404 Not Found

web.xml中的內容

<?xml version="1.0" encoding="UTF-8"?> 

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0"> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <module-name>Admin Application</module-name> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcherServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/servletContext.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
     <async-supported>true</async-supported> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcherServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

servetContext的內容是

<?xml version="1.0" encoding="UTF-8"?> 
<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" 
     xmlns="http://www.springframework.org/schema/beans" 
     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"> 

    <context:component-scan base-package="com.web.controller"/> 
    <bean name="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
     <property name="locations"> 
      <list> 
       <value>/WEB-INF/config.properties</value> 
      </list> 
     </property> 
    </bean> 
    <mvc:resources mapping="/css/**" location="/css/"/> 
    <mvc:resources mapping="/js/**" location="/js/"/> 
    <mvc:annotation-driven/> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

</beans> 

在我已經定義了一個方法,將處理最初的LoginController中的HttpRequest

@Controller 
public class LoginController { 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String signIn() { 
     return "sign-in"; 
    } 
} 

當我請求登錄頁面使用http://localhost:8080/AdminApplication/

它給了我404找不到錯誤。你能告訴我在整個設置中我在這裏做了什麼錯誤嗎?

+0

也許您需要驗證您的應用的確切contextPath是什麼,例如http:// localhost:8080 /可以,因爲contextPath爲「/」 –

回答

0

對於每個訪問應用程序時加載的第一個頁面的每個Web應用程序,您應該有一個索引頁面,直接放置在webapp文件夾下。

這包括在web.xml

<welcome-file-list> 
    <welcome-file>redirect.jsp</welcome-file> 
</welcome-file-list> 

重定向頁面(JSP)可以像:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
    <% response.sendRedirect("signin"); %> 

然後,你可以 「登入」 映射到春天的索引控制器。這將有你的「登錄」頁面作爲視圖

0

我犯了一個錯誤,在父POM.xml中,我已經定義了作爲spring-mvc提供的範圍,以便它不打包在lib文件夾中。這就是調度員班沒有找到的原因。我檢查了日誌並解決了問題。感謝你所做的一切。