我正在尝试使用 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">
关于Java/Spring MVC 起始页面项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23788311/