为什么我的Spring Web项目在启动时没有加载正确的页面?

时间:2016-04-12 21:31:37

标签: java spring jsp spring-mvc web.xml

标题说真的。我有一个小的Spring Web项目,在启动时,应该在我的浏览器中打开redirect.jsp。这应该通过默认控制器发送给home.jsp。问题是,它没有。相反,它会在我的浏览器中自动打开一些默认索引页面我显示的索引页面在我的项目中不存在,标题为"起始页面"并且只显示带有" Hello World"的标题。奇怪的是,我的浏览器中的网址只有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>

我的redirect.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"); %>

1 个答案:

答案 0 :(得分:0)

<强>更新

改变这个:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>          <------- Change this
    <url-pattern>/home.htm</url-pattern>  <------- Add this
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>                    
    <welcome-file>/home.htm</welcome-file>   <------- Change this
</welcome-file-list>