【问题标题】:spring mvc website on root ("/")根目录上的spring mvc网站(“/”)
【发布时间】:2012-07-31 22:26:34
【问题描述】:

我想将 spring mvc 控制器映射到根 (/**) 路径(而不是诸如“/something”之类的子文件夹),同时使用mvc:resources 进行例外处理(打开另一种方法)。

这应该是该框架的基础知识,但显然是一个非常复杂的问题。

我的app-servlet.xml 有这些明显的映射异常:

<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
<mvc:resources mapping="/robots.txt" location="/robots.txt" />

我有这个控制器:

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/**")
public class MainController {

    @RequestMapping(method = RequestMethod.GET)
    public String service(final HttpServletRequest request) {
        final String servlet_path = request.getServletPath();
        System.out.println(String.format("%s %s", new Date().toString(), servlet_path));
        return "test";
    }
}

现在当我点击“/”或“/test”或“/test/page”时,我会得到如下输出:

Fri Aug 03 00:22:12 IDT 2012 /
Fri Aug 03 00:22:13 IDT 2012 /favicon.ico

.. 看到了吗? service() 被调用为 /favicon.ico,即使它被明确排除在外!

现在我猜想@Controller 对 XML 有一些“优先级”,但是,我该如何使排除工作?

一个简单的要求 - 将网站放在“/”上。

P.S This answer 回答了一个非常相似的问题。

另一个注意事项:这个问题与tomcat上下文无关。

【问题讨论】:

    标签: java servlets spring-mvc mapping


    【解决方案1】:

    我想澄清一下,可以更改处理程序指令的声明顺序,而不是重写&lt;mvc:annotation-driven/&gt;

    <mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/>
    <mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/>
    <mvc:annotation-driven/>
    

    【讨论】:

      【解决方案2】:

      这里的问题是在&lt;mvc:resources 注册的底层HandlerMapping 与在&lt;mvc:annotation-driven/&gt; 注册的相比具有非常低的优先级。如果您的要求只是让某些东西响应“/”,那么更好的方法可能是使用与/** 不同的@RequestMapping,而不是将其设置为/home 并按照以下方式定义一些东西:

      &lt;mvc:view-controller path="/" view-name="home" /&gt;

      如果这不起作用,唯一的其他选择将是降低&lt;mvc:resources 的底层 handlerMapping 的优先级,这可以通过显式定义 HandlerMapping 来完成 - 有点复杂但可以做到。

      更新 这是一个可能的配置:

      先试试这个:

      <mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/>
      <mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/>
      

      如果仅此一项不起作用,请将 &lt;mvc:annotation-driven/&gt; 更改为 Spring 3.1.x 的类似内容:

      <bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
          <property name="webBindingInitializer">
              <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                  <property name="conversionService" ref="conversionService"></property>
                  <property name="validator">
                      <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
                          <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
                      </bean>
                  </property>
              </bean>
          </property>
          <property name="messageConverters">
              <list>
                  <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
                  <bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
                  <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
                  <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
                  <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
                  <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
                  <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
              </list>
          </property>
      </bean>
      
      <bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
          <property name="order" value="2"></property>
      </bean>
      

      【讨论】:

      • 您的解决方案是指单个 URL ("/" -> "home")。我需要它是一张通配符。如果您可以慷慨地详细说明如何提高mvc:resources 的优先级高于@RequestMapping,那么我猜,这将解决它。谢谢!
      • 这仅在您的主页不需要控制器操作时才有效(因此只是显示的视图文件,如欢迎页面......,没有花哨的逻辑)。如果您需要在进入视图之前使用需要一些控制器操作来执行操作的页面,您可以使用重定向&lt;mvc:view-controller path="/" view-name="redirect:home" /&gt;。这将重定向到 home 视图,然后该视图将调用 HomeController,或者您为控制器命名。
      • 我关注了您的回答,但我还无法访问它。我的问题在这里stackoverflow.com/questions/32302465/…
      猜你喜欢
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      相关资源
      最近更新 更多