【问题标题】:Spring MVC: Cannot mapping JSP files under subfoldersSpring MVC:无法映射子文件夹下的 JSP 文件
【发布时间】:2017-09-30 16:05:16
【问题描述】:

我知道有很多主题有相同的问题,但经过 3 天的研究,我无法解决我的问题。

如果有人在其他地方发现了答案,请随时将其标记为重复并引导我到那里。

我正在尝试在我的控制器中映射一个请求,然后重定向到我的默认视图文件夹下的子文件夹。但是,我收到 404 错误。

我的 web.xml:

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

  <display-name>SIGCOM</display-name>

  <servlet>
    <servlet-name>springController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springController</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/springController.xml</param-value>
  </context-param>

  <filter>
    <filter-name>SpringFilter</filter-name>
    <filter-class>br.com.sigcom.filter.SpringFilter</filter-class>
    <init-param>
      <param-name>requestEncoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>SpringFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- 
  <listener>
    <listener-class>br.com.sigcom.listener.SpringListener</listener-class>
  </listener>
  -->

  <error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/WebContent/WEB-INF/views/errorHandling/errorPage.jsp</location>
  </error-page>

  <error-page>
    <!-- Missing resource -->
    <error-code>404</error-code>
    <location>/WebContent/WEB-INF/views/errorHandling/error404.jsp</location>
  </error-page>
</web-app>

我的 servlet XML:

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

    <context:component-scan base-package="br.com.sigcom.controller" />

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

    <mvc:annotation-driven />

    <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926" />
  </beans>

如您所见,我的默认视图文件夹是 WEB-INF/views。到目前为止,一切都很好。我做了一个这样的子文件夹:

WEB-INF/
  |-views/
    |-admin/
      |-default.jsp

在我的控制器中,我无法访问 WEB-INF/views/admin/default.jsp,因为我收到了这个 404 错误。

我的控制器:

package br.com.sigcom.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class SpringController { 
  @RequestMapping(value = {"", "/", "home", "default", "index"})
  public String redirectDefaultPage() {
    return "default";
  }

  @RequestMapping("admin")
  public String redirectAdminPage() {
    return "admin/default";
  }
}

我尝试了很多,搜索了很多,但是没有成功。我知道这一定是一个简单的细节,但我已经筋疲力尽了。提前谢谢!

PS.:我的控制台:

mai 02, 2017 9:36:23 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
ADVERTÊNCIA: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SIGCOM' did not find a matching property.
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Server version:        Apache Tomcat/8.0.43
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Server built:          Mar 28 2017 14:42:59 UTC
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Server number:         8.0.43.0
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: OS Name:               Windows 8.1
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: OS Version:            6.3
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Architecture:          amd64
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Java Home:             C:\Program Files (x86)\Java
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: JVM Version:           1.8.0_131-b11
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: JVM Vendor:            Oracle Corporation
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: CATALINA_BASE:         C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: CATALINA_HOME:         C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Dcatalina.base=C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Dcatalina.home=C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Dwtp.deploy=C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\wtpwebapps
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Djava.endorsed.dirs=C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\endorsed
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Dfile.encoding=Cp1252
mai 02, 2017 9:36:23 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFORMAÇÕES: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files (x86)/Java/bin/server;C:/Program Files (x86)/Java/bin;C:/Program Files (x86)/Java/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared Files\cpp\bin\Intel64;C:\Program Files (x86)\Borland\Delphi7\Bin;C:\Program Files (x86)\Borland\Delphi7\Projects\Bpl\;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\MySQL\MySQL Utilities 1.6\;C:\Arquivos de programas\Borland\Delphi7\Projects\Bpl;C:\morais_san\Development\Delphi\Repository\Components\JVCL3.33\jcl;C:\morais_san\Development\Java\Maven\apache-maven-3.5.0\bin;C:\morais_san\Development\Eclipse\eclipse-jee-neon-R-win32-x86_64\eclipse;;.
mai 02, 2017 9:36:23 PM org.apache.coyote.AbstractProtocol init
INFORMAÇÕES: Initializing ProtocolHandler ["http-nio-8080"]
mai 02, 2017 9:36:23 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMAÇÕES: Using a shared selector for servlet write/read
mai 02, 2017 9:36:23 PM org.apache.coyote.AbstractProtocol init
INFORMAÇÕES: Initializing ProtocolHandler ["ajp-nio-8009"]
mai 02, 2017 9:36:23 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMAÇÕES: Using a shared selector for servlet write/read
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.Catalina load
INFORMAÇÕES: Initialization processed in 982 ms
mai 02, 2017 9:36:23 PM org.apache.catalina.core.StandardService startInternal
INFORMAÇÕES: Starting service Catalina
mai 02, 2017 9:36:23 PM org.apache.catalina.core.StandardEngine startInternal
INFORMAÇÕES: Starting Servlet Engine: Apache Tomcat/8.0.43
mai 02, 2017 9:36:25 PM org.apache.jasper.servlet.TldScanner scanJars
INFORMAÇÕES: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
mai 02, 2017 9:36:26 PM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: No Spring WebApplicationInitializer types detected on classpath
mai 02, 2017 9:36:26 PM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: Initializing Spring FrameworkServlet 'springController'
mai 02, 2017 9:36:26 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFORMAÇÕES: FrameworkServlet 'springController': initialization started
mai 02, 2017 9:36:26 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFORMAÇÕES: Refreshing WebApplicationContext for namespace 'springController-servlet': startup date [Tue May 02 21:36:26 BRT 2017]; root of context hierarchy
mai 02, 2017 9:36:26 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFORMAÇÕES: Loading XML bean definitions from ServletContext resource [/WEB-INF/springController-servlet.xml]
mai 02, 2017 9:36:28 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFORMAÇÕES: Mapped "{[/search],methods=[GET]}" onto public java.lang.String br.com.sigcom.controller.SearchController.redirectSearchPage()
mai 02, 2017 9:36:28 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFORMAÇÕES: Mapped "{[/admin]}" onto public java.lang.String br.com.sigcom.controller.SpringController.redirectAdminPage()
mai 02, 2017 9:36:28 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFORMAÇÕES: Mapped "{[ || / || /home || /default || /index]}" onto public java.lang.String br.com.sigcom.controller.SpringController.redirectDefaultPage()
mai 02, 2017 9:36:28 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFORMAÇÕES: Looking for @ControllerAdvice: WebApplicationContext for namespace 'springController-servlet': startup date [Tue May 02 21:36:26 BRT 2017]; root of context hierarchy
mai 02, 2017 9:36:28 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFORMAÇÕES: Looking for @ControllerAdvice: WebApplicationContext for namespace 'springController-servlet': startup date [Tue May 02 21:36:26 BRT 2017]; root of context hierarchy
mai 02, 2017 9:36:28 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler
INFORMAÇÕES: Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
mai 02, 2017 9:36:28 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFORMAÇÕES: FrameworkServlet 'springController': initialization completed in 2250 ms
mai 02, 2017 9:36:28 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deploying web application directory C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\webapps\docs
mai 02, 2017 9:36:28 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deployment of web application directory C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\webapps\docs has finished in 35 ms
mai 02, 2017 9:36:28 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deploying web application directory C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\webapps\examples
mai 02, 2017 9:36:29 PM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: ContextListener: contextInitialized()
mai 02, 2017 9:36:29 PM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: SessionListener: contextInitialized()
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deployment of web application directory C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\webapps\examples has finished in 456 ms
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deploying web application directory C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\webapps\host-manager
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deployment of web application directory C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\webapps\host-manager has finished in 45 ms
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deploying web application directory C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\webapps\manager
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deployment of web application directory C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\webapps\manager has finished in 44 ms
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deploying web application directory C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\webapps\ROOT
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deployment of web application directory C:\morais_san\Development\Tomcat\apache-tomcat-8.0.43\webapps\ROOT has finished in 32 ms
mai 02, 2017 9:36:29 PM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler ["http-nio-8080"]
mai 02, 2017 9:36:29 PM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler ["ajp-nio-8009"]
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.Catalina start
INFORMAÇÕES: Server startup in 5763 ms
mai 02, 2017 9:59:25 PM org.apache.catalina.core.StandardContext reload
INFORMAÇÕES: Reloading Context with name [/SIGCOM] has started
mai 02, 2017 9:59:25 PM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: Destroying Spring FrameworkServlet 'springController'
mai 02, 2017 9:59:25 PM org.springframework.web.context.support.XmlWebApplicationContext doClose
INFORMAÇÕES: Closing WebApplicationContext for namespace 'springController-servlet': startup date [Tue May 02 21:36:26 BRT 2017]; root of context hierarchy
mai 02, 2017 9:59:27 PM org.apache.jasper.servlet.TldScanner scanJars
INFORMAÇÕES: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
mai 02, 2017 9:59:27 PM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: No Spring WebApplicationInitializer types detected on classpath
mai 02, 2017 9:59:27 PM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: Initializing Spring FrameworkServlet 'springController'

2017 年 5 月 2 日晚上 9:59:27 org.springframework.web.servlet.DispatcherServlet initServletBean INFORMAÇÕES: FrameworkServlet 'springController': 初始化开始 2017 年 5 月 2 日晚上 9:59:27 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh INFORMAÇÕES:为命名空间“springController-servlet”刷新 WebApplicationContext:启动日期 [Tue May 02 21:59:27 BRT 2017];上下文层次的根 2017 年 5 月 2 日晚上 9:59:27 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFORMAÇÕES:从 ServletContext 资源 [/WEB-INF/springController-servlet.xml] 加载 XML bean 定义 2017 年 5 月 2 日晚上 9:59:28 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 寄存器 INFORMAÇÕES:将“{[/search],methods=[GET]}”映射到公共 java.lang.String br.com.sigcom.controller.SearchController.redirectSearchPage() 2017 年 5 月 2 日晚上 9:59:28 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 寄存器 INFORMAÇÕES:将“{[/admin]}”映射到公共 java.lang.String br.com.sigcom.controller.SpringController.redirectAdminPage() 2017 年 5 月 2 日晚上 9:59:28 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 寄存器 INFORMAÇÕES:将“{[ || / || /home || /default || /index]}”映射到公共 java.lang.String br.com.sigcom.controller.SpringController.redirectDefaultPage() 2017 年 5 月 2 日晚上 9:59:28 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache INFORMAÇÕES:寻找@ControllerAdvice:命名空间'springController-servlet'的WebApplicationContext:启动日期[Tue May 02 21:59:27 BRT 2017];上下文层次的根 2017 年 5 月 2 日晚上 9:59:28 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache INFORMAÇÕES:寻找@ControllerAdvice:命名空间'springController-servlet'的WebApplicationContext:启动日期[Tue May 02 21:59:27 BRT 2017];上下文层次的根 2017 年 5 月 2 日晚上 9:59:28 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler INFORMAÇÕES:将 URL 路径 [/resources/**] 映射到处理程序 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0' 2017 年 5 月 2 日晚上 9:59:28 org.springframework.web.servlet.DispatcherServlet initServletBean 信息:FrameworkServlet 'springController':初始化在 1456 毫秒内完成 2017 年 5 月 2 日晚上 9:59:28 org.apache.catalina.core.StandardContext 重新加载 INFORMAÇÕES:重新加载名称为 [/SIGCOM] 的上下文已完成

【问题讨论】:

    标签: java spring jsp servlets subdirectory


    【解决方案1】:

    我尝试使用与您相同的设置设置一个项目,只是 web.xml 中没有过滤器、过滤器映射、错误页面、侦听器,并且我能够在没有 404 的情况下访问 URL。

    控制器也能够解析模板。 您是否也使用上下文路径正确输入了您的 URL? 例如http://localhost:8080/SIGCOM/admin

    【讨论】:

    • 您的其他配置设置一定有问题,因为在这里创建相同的项目有效。或许,你可以试试把最小的包上传到git,让我们看看?
    • 已解决,@Chris。这是我的过滤器!
    【解决方案2】:

    我已经使用您的代码创建了一个新项目。但最后,我得到了正确的东西。也许你可以像一楼说的那样检查你的网址。

    【讨论】:

    • 再次检查...没有。
    【解决方案3】:

    已解决。当你们没有过滤器运行时,我已经评论了我的并工作了。现在我知道该怎么做了。感谢您的帮助,伙计们!

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 1970-01-01
      • 2015-05-28
      • 2015-12-05
      • 1970-01-01
      • 2012-06-11
      • 2022-01-02
      • 2016-11-11
      • 2014-09-18
      相关资源
      最近更新 更多