【发布时间】:2016-04-12 21:31:36
【问题描述】:
标题确实说明了一切。我有一个小型 Spring Web 项目,在启动时应该在我的浏览器中打开 redirect.jsp。这应该通过默认控制器将我发送到home.jsp。问题是,它没有。相反,它会自动在我的浏览器中打开一些默认索引页面。我显示的索引页在我的项目中不存在,标题为“起始页”,仅显示带有“Hello World”的标题。奇怪的是,我浏览器中的 url 只是 localhost:port/projectrootfolder 而不是 localhost:port/projectrootfolder/home.htm 或类似的东西。
我的项目结构
root
|...
|Web pages
| |WEB-INF
| | |jsp
| | | |home.jsp
| |dispatcher-servlet.xml
| |web.xml
|redirect.jsp
我的 dispatcher-servlet.xml
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- Specify that this dispatcher works with by spring annotations-->
<mvc:annotation-driven conversion-service="conversionService"/>
<context:component-scan base-package="com.exevan.ipweb.controller" />
<!-- Spring bean that maps url's to controllers -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="home.htm">homeController</prop>
</props>
</property>
</bean>
<!-- Spring bean that converts logical view name to view location -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!-- Index controller -->
<bean name="homeController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="home" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean id="dateToStringConverter" class="com.exevan.ipweb.converter.DateToStringConverter"/>
<bean id="stringToDateConverter" class="com.exevan.ipweb.converter.StringToDateConverter"/>
<bean id="idToPublisherConverter" class="com.exevan.ipweb.converter.IdToPublisherConverter"/>
</list>
</property>
</bean>
</beans>
我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
我的重定向.jsp
<%--
Views should be stored under the WEB-INF folder so that
they are not accessible except through controller process.
This JSP is here to provide a redirect to the dispatcher
servlet but should be the only JSP outside of WEB-INF.
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("/home.htm"); %>
【问题讨论】:
-
您在 myredorect.jsp 上的重定向是 home.htm
-
应该是/home
-
应该是 /home 因为你的控制器会有映射把它重定向到 home.jsp
-
在 redirect.jsp 中从 /home.htm 更改为 /home 不起作用。当我运行项目时,它仍然默认为一些样板索引页面(顺便说一句,我的项目中实际上并不存在)。
-
你看到tomcat主页了吗
标签: java spring jsp spring-mvc web.xml