Spring MVC default controller

2019-08-11 06:54发布

I have a temporary solution for this and I'll explain it bellow, but I'm looking if there's a better solution.

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>

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>

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.

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?)

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.

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.

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!

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-11 07:24

I haven't actually tried this, but I think it should work.

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);
    }
}

Annotate your HomeController at the class level (not the method level) with @RequestMapping({"/", "/home"}).

In the bean definitions of dispatcher-servlet.xml, replace Spring's ControllerClassNameHandler with MyControllerClassNameHandler.

查看更多
登录 后发表回答