簡體   English   中英

Java / Spring MVC起始頁項目?

[英]Java/Spring MVC starting page project?

我正在嘗試使用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頁面)我的項目僅對一件事有效:當我使用url時

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/時,我只想進入索引頁面

一些幫助? 謝謝

您可能會在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">

您是否嘗試過使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM