java Spring MVC 中的映射 /(根 URL)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12202302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Map / (root URL) in Spring MVC
提问by pbean
This is something that I think should be very easy, but so far I have not been able to get it to work.
这是我认为应该很容易的事情,但到目前为止我还没有能够让它发挥作用。
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.
我想要做的是将我的根路径映射到 Spring MVC 控制器。使用普通Servlet
,我只需/
在我的 中添加“ ”的映射web.xml
,它就会很好地提取它。但是对于 Spring MVC,不是那么多。
I have tried many combinations, but none seem to work. I think the following one should work.
我尝试了很多组合,但似乎都不起作用。我认为以下应该有效。
In web.xml
:
在web.xml
:
<servlet-mapping>
<servlet-name>myDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In my contextConfigLocation
file:
在我的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);
}
}
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).
因此,假设我的应用程序在 上运行http://localhost:8080/app/
,我想要该确切的 URL 来执行该方法display
。我不想在 之后输入任何内容/app/
。事实上,之后的一些东西/app/
被映射到其他控制器,并且一切正常(并且它们必须继续工作)。
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.
我在这里错过了什么?为什么这不简单?如果我使用它url-pattern
来映射到一个普通Servlet
实例,它工作正常并且我到达了该doGet
方法,但是使用 Spring MVC 我似乎缺少一些特殊的黑魔法来让它工作。
回答by Boris Treukhov
Instead of mapping to /
you can declare a welcome page in your web.xml
file:
/
您可以在web.xml
文件中声明一个欢迎页面,而不是映射到您:
<welcome-file-list>
<welcome-file>welcome.htm</welcome-file>
</welcome-file-list>
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.
所以您的/
路径将被处理/welcome.htm
,然后如果您的控制器正确映射到/welcome.htm
它,它将/
像处理/welcome.htm
请求一样处理,而不更改其他配置。
回答by dardo
I'd recommend getting rid of the SimpleUrlHandlerMapping and just doing the following:
我建议摆脱 SimpleUrlHandlerMapping 并执行以下操作:
@Controller
@RequestMapping("/")
public class RootController
{
@RequestMapping(method=RequestMethod.GET)
public ModelAndView display(...)
{
...
}
}
This should get the result you want. Also, add <mvc:annotation-driven/>
to your servlet context with a <context:component-scan base-package="some.package.path.to.controller" />
to have Spring wire up that controller.
这应该会得到你想要的结果。此外,<mvc:annotation-driven/>
使用 a添加到您的 servlet 上下文<context:component-scan base-package="some.package.path.to.controller" />
以使 Spring 连接该控制器。
Otherwise, you can probably map the URL with the SimpleUrlHandlerMapping as so:
否则,您可以使用 SimpleUrlHandlerMapping 映射 URL,如下所示:
<property name="mappings">
<value>
/*=rootController
</value>
<property>
If done this way, you can keep the bean defined for rootController.
如果这样做,您可以保留为 rootController 定义的 bean。
回答by westcoastmilt
For Spring Webflow, the suggestion from Boris Treukhov to use web-file-list led to my discovering a hack that worked for weblogic. For example, if welcome is a flow.xml file, do the following: Change the url-pattern from \ to *.html in web.xml. Place a dummy(empty) file welcome.htm into the folder src\main\webapp\WEB-INF. In SimpleUrlHandlerMapping map the value /welcome.htm=flowController. Finally, complete the registry, flow-location path="welcome.xml". Refer to link: http://forum.spring.io/forum/spring-projects/web/50547-welcome-file-list-with-spring.
对于 Spring Webflow,Boris Treukhov 建议使用 web-file-list 导致我发现了一个适用于 weblogic 的 hack。例如,如果welcome 是一个flow.xml 文件,请执行以下操作: 将web.xml 中的url-pattern 从\ 更改为*.html。将一个虚拟(空)文件welcome.htm 放入文件夹src\main\webapp\WEB-INF。在 SimpleUrlHandlerMapping 映射值 /welcome.htm=flowController。最后完成注册表,flow-location path="welcome.xml"。参考链接:http: //forum.spring.io/forum/spring-projects/web/50547-welcome-file-list-with-spring。