我想在启动/启动时将我的应用程序(在Apache Tomcat 7.0上运行的Spring)路由到提供主视图的Controller
因此:
1)我在web.xml
中的welcome文件标签中定义了一个index.htm2)我注释了HomeController @Controller @RequestMapping(“/ index.htm”)
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>
片段:
<!-- 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" />
我的HomeController:
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错误“请求的资源()不可用。”
答案 0 :(得分:0)
尝试对控制器进行上下文组件扫描。
在..- servlet.xml中添加以下行
<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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.yourmarketnet.controller.spring"/>
答案 1 :(得分:0)
项目结构中的index.htm究竟在哪里?
您无法将 index.htm 定义为欢迎文件并将其与控制器一起映射。
你有两个解决方案。
如果您没有使用index.htm,请将其从welcome-file-list中完全删除,然后仅使用&#34; /&#34; 值映射您的控制器
或者您可以将index.htm仅作为控制器中的映射,而不使用&#34; /&#34; 映射。并且不要使用任何欢迎文件列表。
希望这会对你有所帮助。
干杯。
答案 2 :(得分:0)
这样做
@Controller
public class HomeController {
@RequestMapping({"/"})
public String requestHandler()
{
return "Home";
}
}
你已经全部准备好了。
你不需要
<bean id="unAuthenticatedUrlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.htm">HomeController</prop>
</props>
</property>
</bean>
如果您正在使用注释。