【问题标题】:Why does my Spring web project not load the correct page on startup?为什么我的 Spring Web 项目在启动时没有加载正确的页面?
【发布时间】: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


【解决方案1】:

更新

改变这个:

<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>

【讨论】:

  • 不,我仍然得到 Hello World 页面。
  • 什么是 home.jsp?是您要显示的页面吗?你想显示 home.jsp 还是 home.html?
  • 考虑到我没有在我的项目结构中列出 home.html 文件,而且我的项目中甚至不存在这样的文件,我想说它相当明显我正在尝试加载home.jsp.
  • 为了回复您的编辑,我在问题中描述我的项目结构时犯了一个错误。我确实有一个 home.jsp 所在的 /jsp 文件夹。我也确信我不需要将@RequestMapping 添加到我的控制器,因为我使用在 dispatcher-servlet.xml 中定义的名为homeController 的标准 Spring 控制器来处理基本页面导航(包括导航到 hme 页面) .
  • 抱歉没有看到 ParameterizableViewController ,我更新了我的答案,尝试使用 /home.htm 而不是 /home
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 2021-03-17
  • 2015-09-05
相关资源
最近更新 更多