2017-06-15 185 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文件夹中。这就是调度员班没有找到的原因。我检查了日志并解决了问题。感谢你所做的一切。

相关问题