gpt4 book ai didi

java - 根 ("/"上的 spring mvc 网站)

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:07:33 27 4
gpt4 key购买 nike

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

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

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

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

我有这个 Controller :

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 上,我如何使排除起作用?

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

附言This answer一个非常相似的问题的答案。

另外注意:这个问题不是关于tomcat上下文的。

最佳答案

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

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

关于java - 根 ("/"上的 spring mvc 网站),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11786115/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com