在Spring MVC中映射/(根URL) [英] Map / (root URL) in Spring MVC
问题描述
这是我认为应该很容易的事情,但是到目前为止,我还无法使其正常工作.
This is something that I think should be very easy, but so far I have not been able to get it to work.
我要做的是将我的根路径映射到Spring MVC Controller.对于普通的Servlet
,我只需在我的web.xml
中为"/
"添加一个映射,它将很好地拾取它.但是使用Spring MVC并没有那么多.
What I want to do is map my root path to a Spring MVC Controller. With a normal Servlet
, I would just add a mapping for "/
" in my web.xml
, and it would pick it up quite well. But with Spring MVC, not so much.
我尝试了很多组合,但似乎都没有用.我认为以下一种应该有效.
I have tried many combinations, but none seem to work. I think the following one should work.
在web.xml
中:
<servlet-mapping>
<servlet-name>myDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在我的contextConfigLocation
文件中:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true"/>
<property name="mappings">
<util:map>
<entry key="/" value-ref="rootController"/>
</util:map>
</property>
</bean>
<bean id="rootController" class="my.package.RootController">
那么显然,控制器本身就是这种情况.我不知道如何将方法映射到实际的根路径.我的尝试是这样的:
Then obviously, there is the case of the controller itself. I have no clue how to map the method to the actual root path. My attempt is something like this:
public class RootController extends MultiActionController {
@RequestMapping("/")
public ModelAndView display(HttpServletRequest request, HttpServletResponse response) throws Exception {
final Map<String, Object> model = new HashMap<String, Object>();
model.put("someVariable", "Hello, MVC world.");
return new ModelAndView("rootPage", model);
}
}
因此,假设我的应用程序在http://localhost:8080/app/
上运行,我希望该确切的URL执行方法display
.我不想在/app/
之后输入任何内容.实际上,/app/
之后的某些内容已映射到其他控制器,并且一切正常(必须继续工作).
So let's say my application runs on http://localhost:8080/app/
, I want that exact URL to execute the method display
. I do not want to type anything after /app/
. In fact, some things after /app/
are mapped to other controllers, and that all works fine (and they have to keep working).
我在这里想念什么?为什么这不简单地起作用?如果我使用相同的url-pattern
映射到普通的Servlet
实例,则可以正常工作,并且可以使用doGet
方法,但是使用Spring MVC时,我似乎缺少一些特殊的黑魔法来使其正常工作.>
What am I missing here? Why is this not simply working? If I use the same url-pattern
to map to a plain Servlet
instance, it works fine and I reach the doGet
method, but with Spring MVC I seem to missing some particular black magic to get this to work.
推荐答案
您可以在web.xml
文件中声明欢迎页面,而不是映射到/
:
Instead of mapping to /
you can declare a welcome page in your web.xml
file:
<welcome-file-list>
<welcome-file>welcome.htm</welcome-file>
</welcome-file-list>
,因此您的/
路径将被处理为/welcome.htm
,然后,如果您的控制器已正确映射到/welcome.htm
,它将处理/
,就好像它是/welcome.htm
请求一样,而无需更改其他配置.
so your /
path will be processed as /welcome.htm
and then if your controller is correctly mapped to /welcome.htm
it will process /
as if it was a /welcome.htm
request, without making changes to other configuration.
这篇关于在Spring MVC中映射/(根URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!