我知道有很多问题都有相同的问题,但是经过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_sanDevelopmentTomcatapache-tomcat-8.0.43
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: CATALINA_HOME: C:morais_sanDevelopmentTomcatapache-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_sanDevelopmentTomcatapache-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_sanDevelopmentTomcatapache-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_sanDevelopmentTomcatapache-tomcat-8.0.43wtpwebapps
mai 02, 2017 9:36:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Djava.endorsed.dirs=C:morais_sanDevelopmentTomcatapache-tomcat-8.0.43endorsed
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)Javabin;C:WindowsSunJavabin;C:Windowssystem32;C:Windows;C:/Program Files (x86)/Java/bin/server;C:/Program Files (x86)/Java/bin;C:/Program Files (x86)/Java/lib/amd64;C:ProgramDataOracleJavajavapath;C:Program Files (x86)Common FilesIntelShared FilescppbinIntel64;C:Program Files (x86)BorlandDelphi7Bin;C:Program Files (x86)BorlandDelphi7ProjectsBpl;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;c:Program Files (x86)ATI TechnologiesATI.ACECore-Static;C:Program FilesIntelWiFibin;C:Program FilesCommon FilesIntelWirelessCommon;C:Program Files (x86)SkypePhone;C:Program Files (x86)QuickTimeQTSystem;C:Program Files (x86)MySQLMySQL Utilities 1.6;C:Arquivos de programasBorlandDelphi7ProjectsBpl;C:morais_sanDevelopmentDelphiRepositoryComponentsJVCL3.33jcl;C:morais_sanDevelopmentJavaMavenapache-maven-3.5.0bin;C:morais_sanDevelopmentEclipseeclipse-jee-neon-R-win32-x86_64eclipse;;.
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_sanDevelopmentTomcatapache-tomcat-8.0.43webappsdocs
mai 02, 2017 9:36:28 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deployment of web application directory C:morais_sanDevelopmentTomcatapache-tomcat-8.0.43webappsdocs 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_sanDevelopmentTomcatapache-tomcat-8.0.43webappsexamples
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_sanDevelopmentTomcatapache-tomcat-8.0.43webappsexamples 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_sanDevelopmentTomcatapache-tomcat-8.0.43webappshost-manager
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deployment of web application directory C:morais_sanDevelopmentTomcatapache-tomcat-8.0.43webappshost-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_sanDevelopmentTomcatapache-tomcat-8.0.43webappsmanager
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deployment of web application directory C:morais_sanDevelopmentTomcatapache-tomcat-8.0.43webappsmanager 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_sanDevelopmentTomcatapache-tomcat-8.0.43webappsROOT
mai 02, 2017 9:36:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFORMAÇÕES: Deployment of web application directory C:morais_sanDevelopmentTomcatapache-tomcat-8.0.43webappsROOT 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'
MAI 02,2017年9:59:27 pm org.springframework.web.servlet.dispatcherservret initservletbean Informações:Frameworkservlet'SpringController':初始化开始 MAI 02,2017 9:59:27 pm org.springframework.web.context.support.xmlwebapplicationcontext preparereFresh Informações:名称空间" SpringController-Servlet"的刷新WebApplicationContext':启动日期[TUE 5月2日21:59:27 BRT 2017];上下文层次结构的根 MAI 02,2017 9:59:27 pm org.springframework.beans.factory.xml.xmlbeandefinitionReader loadBeanDefinitions Informações:从ServletContext Resources [/web-inf/springController-servlet.xml加载XML Bean定义] MAI 02,2017 9:59:28 PM org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping寄存器 Informações:映射" {[/search],methods = [get]}" public java.lang.string br.com.sigcom.controller.searchcontroller.redirectsearchpage(( MAI 02,2017 9:59:28 PM org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping寄存器 Informações:映射到公共java.lang.string br.com.sigcom.controller.springcontroller.redirectadminpage((上的" {[/admin]}" MAI 02,2017 9:59:28 PM org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping寄存器 Informações:映射" {[||/||/home ||/default ||/index]}" to public java.lang.string br.com.sigcom.controller.springcontroller.redirctdectroller.redirectdefaultpage(( MAI 02,2017 9:59:28 pm org.springframework.web.servlet.mvc.method.Annotation.RequestMappingHandlerAdlerAdlerAdlerAdapter InitcontrollerAdviceCache Informações:寻找@controllerAdvice:webapplicationContext for namespace'springController-servlet':启动日期[TUE 5月21:59:27 BRT 2017];上下文层次结构的根 MAI 02,2017 9:59:28 pm org.springframework.web.servlet.mvc.method.Annotation.RequestMappingHandlerAdlerAdlerAdlerAdapter InitcontrollerAdviceCache Informações:寻找@controllerAdvice:webapplicationContext for namespace'springController-servlet':启动日期[TUE 5月21:59:27 BRT 2017];上下文层次结构的根 MAI 02,2017 9:59:28 org.springframework.web.servlet.handler.simpleurlhandlermpapping登记册 Informações:映射URL路径[/resources/**]到处理程序'org.springframework.web.servlet.resource.Resourcehttprequesthandler#0' MAI 02,2017 9:59:28 org.springframework.web.servlet.dispatcherservlet initservletbean Informações:Frameworkservlet'SpringController':初始化在1456毫秒内完成 MAI 02,2017 9:59:28 PM org.apache.catalina.core.standardcontext重新加载 Informações:带有名称[/sigcom]的重新加载上下文已完成
我尝试了一个设置一个带有相同设置的项目,除非没有过滤器,过滤器映射,错误页面,web.xml中的侦听器,我可以访问没有404的访问URL。
控制器也能够解析模板。您是否也使用上下文路径正确输入URL?例如http://localhost:8080/ sigcom /admin
我已经使用您的代码创建了一个新项目。但最后,我得到了正确的东西。也许您可以像一楼所说的那样检查您的URL。
已解决。当你们没有过滤器的情况下运行时,我评论了我的工作。现在我知道该怎么办。感谢您的帮助,伙计们!