Spring MVC默认控制器 [英] Spring MVC default controller
问题描述
我对此有一个临时解决方案,下面将解释,但是我在寻找是否有更好的解决方案。
I have a temporary solution for this and I'll explain it bellow, but I'm looking if there's a better solution.
我有一个基本的Spring 4具有web.xml配置的MVC应用程序(无maven)。 web.xml如下:
I have a basic Spring 4 MVC app with web.xml configurations (no maven). The web.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app ...>
<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>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
和dispatcher-servlet.xml:
and the dispatcher-servlet.xml:
<?xml version='1.0' encoding='UTF-8' ?>
<beans ...>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean class="controllers.Controller"></bean>
<bean class="controllers.HomeController"></bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
我删除了将创建简单in-xml的部分( urlMapping
)用于索引视图的控制器,仅仅是因为索引页还需要完成一些后端工作。
I deleted the part where it would create a simple in-xml (urlMapping
) controller for the index view, simply because the index page needs some backend work done as well.
当前所有 /
请求由 Controller
类处理,而所有 / home /
请求均得到处理由 HomeController
类(因为默认情况下,spring会创建一个路径,因此控制器名称是主url路径,对吧?)
Currently all /
requests are handled by the Controller
class, while all /home/
requests are handled by the HomeController
class (because spring, by default, makes a path so controller name is the primary url path, right?)
我的问题是,是否可能,如果可以,如何将 HomeController
类设置为默认控制器。只是这样,我实际上不必具有 Controller
类。
My question is, is it possible and if yes, how can I make the HomeController
class my default controller. Just so I don't actually have to have a Controller
class.
对于熟悉ASP.NET MVC的人,我想要的是如何访问 site.com/Home/Index
还是只是转到 site.com
您引用同一控制器的时间。
For those familiar with ASP.NET MVC, what I want is how whether you go to site.com/Home/Index
or just site.com
, both the times you reference the same controller.
我想创建它,以便Spring自动将 site.com
连接到路由在 HomeController
类中。
I want to create it so spring automatically connects site.com
to a route in HomeController
class.
我希望我已经足够清楚了。我会回答所有不清楚的问题,并欢迎您提供任何答案或文档!谢谢!
I hope I'm clear enough. I'll answer any unclear questions and welcome any answers or documentations! Thanks!
推荐答案
我实际上没有尝试过,但是我认为它应该可以工作。
I haven't actually tried this, but I think it should work.
创建一个扩展 ControllerClassNameHandlerMapping
的新类,并覆盖 generatePathMappings
方法,以便控制器使用 @RequestMapping
进行注释,然后它将使用该注释的值作为路径而不是控制器的名称。
Create a new class that extends ControllerClassNameHandlerMapping
, and override the generatePathMappings
method so that if the controller is annotated with @RequestMapping
then it will use that annotation's value as the path instead of the controller's name.
public class MyControllerClassNameHandlerMapping extends ControllerClassNameHandlerMapping {
@Override
protected String[] generatePathMappings(Class<?> beanClass) {
if (beanClass.isAnnotationPresent(RequestMapping.class)) {
RequestMapping mapping = beanClass.getAnnotation(RequestMapping.class);
return mapping.value();
}
return super.generatePathMappings(beanClass);
}
}
为您的 HomeController注释
在类级别(不是方法级别)上,并带有 @RequestMapping({ /, / home})
。
Annotate your HomeController
at the class level (not the method level) with @RequestMapping({"/", "/home"})
.
在 dispatcher-servlet.xml 的bean定义中,将Spring的 ControllerClassNameHandler
替换为 MyControllerClassNameHandler
。
In the bean definitions of dispatcher-servlet.xml, replace Spring's ControllerClassNameHandler
with MyControllerClassNameHandler
.
这篇关于Spring MVC默认控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!