1

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!

8
  • 1
    Is there a specific reason you're not just using Spring Boot? If you did, you'd just have to put @RequestMapping("/") on the method and not think about anything else. Commented Feb 6, 2016 at 14:49
  • @chrylis I know, I started using Spring Boot, but then was informed that I had to get around without it (configurations are a must). It's not a personal project. Commented Feb 6, 2016 at 14:50
  • 1
    So you have an employer who's actively insisting on using outdated tools for a new project, and not even applying best practices in using a dependency manager? Good luck. Commented Feb 6, 2016 at 14:51
  • @chrylis it pays until the next job :) but I know what you mean... Commented Feb 6, 2016 at 14:57
  • 1
    If you're not gonna use spring boot, it's ok, but even with plain spring mvc, you can live without web.xml and stuff. Spring has a great doc, so.. Commented Feb 6, 2016 at 15:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You sir are a wizard! I honestly haven't thought of doing that, I just thought there was a built-in way to do this. But modifying that class did it as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.