Java / Spring MVC起始页面项目?

时间:2014-05-21 15:59:10

标签: java spring spring-mvc

我试图用java / spring / hibernate开发一个mvc web应用程序。

实际上,在完成所有配置后,我只是一个问题。

这是我的web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>BookStore</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationcontext.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

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

  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

这是我的mvc-dispatcher-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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">

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

    <!-- Used for scan Controller Spring annotations -->
    <context:component-scan
        base-package="progettoPW.BookStore.backend.servlet.controller" />

    <bean name="/index" class="progettoPW.BookStore.backend.servlet.HomeServlet" />

</beans>

(HomeServlet将应用程序重定向到index.jsp页面) 我的项目只为一件事做得很好:当我使用网址时

http://localhost:8080/BookStore/

我收到404错误。我使用http://localhost:8080/BookStore/index

我尝试在web.xml中添加此标记

<welcome-file-list>
    <welcome-file>/index</welcome-file>
</welcome-file-list>

当我使用起始网址http://localhost:8080/BookStore/

时,我只想进入我的索引页面

有些帮助吗?感谢

2 个答案:

答案 0 :(得分:1)

您可以在mvc-dispatcher-servlet.xml中添加以下行:

<mvc:view-controller path="/" view-name="redirect:/index" />

PS:您需要将mvc架构添加到beans根代码中:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    ...
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        ...
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

答案 1 :(得分:0)

您是否尝试过使用

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>