我想在启动/启动时将我的应用程序(在 Apache Tomcat 7.0 上运行的 Spring)路由到提供主视图的控制器
所以:
1)我在web.xml的欢迎文件标签中定义了一个index.htm
2) 我用 @Controller @RequestMapping("/index.htm") 注释了 HomeController
3) 我还在 xml bean 中映射了 HomeController
我的 web.xml:
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.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>yourmarketnet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>yourmarketnet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
我的 spring-servlet.xml 的片段:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" p:viewClass="org.springframework.web.servlet.view.JstlView" />
<!-- Controller beab mappinh -->
<bean class="com.yourmarketnet.controller.spring.HomeController"
name="HomeController"/>
<bean id="unAuthenticatedUrlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.htm">HomeController</prop>
</props>
</property>
</bean>
来自 applicationContext.xml 的片段:
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans. For example @Controller and @Service. Make sure to set the correct base-package-->
<context:component-scan base-package="com.yourmarketnet.mvc.controller.spring" />
<!-- Configures the annotation-driven Spring MVC Controller programming model. Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />
<!-- mapping of static resources-->
<mvc:resources mapping="/resources/**" location="/resources/" />
<import resource="hibernate-context.xml" />
我的家庭控制器:
package com.yourmarketnet.controller.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value={"/","/index.htm"})
//If have also tried RequestMapping("/index.htm") OR //If have also tried RequestMapping("index.htm")
public class HomeController {
@RequestMapping(method = RequestMethod.GET)
public String requestHandler()
{
return "Home";
}
}
这也是我的项目结构:
然而
我在应用程序启动时收到 404 错误“请求的资源 () 不可用。”