0

我想在启动/启动时将我的应用程序(在 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 错误“请求的资源 () 不可用。”

4

3 回答 3

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>

如果您使用注释。

于 2012-05-28T06:08:05.307 回答
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"/>

于 2012-05-27T07:27:22.553 回答
0

您的 index.htm 在项目结构中到底在哪里?

而且您不能将index.htm定义为欢迎文件并将其与控制器映射。

你有两个解决方案。

  1. 如果您没有使用 index.htm,则将其从欢迎文件列表中完全删除,然后仅使用“/”值映射您的控制器。

  2. 或者您可以将 index.htm 仅作为控制器中的映射,而不使用“/”映射。并且不要使用任何欢迎文件列表。

希望这对您有所帮助。

干杯。

于 2012-05-28T05:54:11.360 回答