0

I am trying to create a simple site with Spring MVC, basic idea is to create separate controller for each page.

sitename.com must be handled by WelcomeController

@Controller
public class WelcomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
         System.out.println("----This line is executed on tomcat7 console---");
        return "index";
    }
}

Here are Spring MVC configurations

@Configuration
@EnableWebMvc
@ComponentScan(basePackages={"com.mvc.spring4"})
public class MyAppConfig {
    @Bean
    public ViewResolver jspViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

and

public class ServletInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[0];
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{MyAppConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/*"};
    }

}

There exist a file /WEB-INF/jsp/index.jsp, however I am getting 404 error

Here is the console output of the tomcat7

2016-01-13 14:47:01 DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/Spring4MVC]
2016-01-13 14:47:01 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /
2016-01-13 14:47:01 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public java.lang.String com.mvc.spring4.WelcomeController.index(org.springframework.ui.Model)]
2016-01-13 14:47:01 DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'welcomeController'
2016-01-13 14:47:01 DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/Spring4MVC] is: -1

----This line is executed on tomcat7 console---

2016-01-13 14:47:01 DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/WEB-INF/jsp/index.jsp]] in DispatcherServlet with name 'dispatcher'
2016-01-13 14:47:01 DEBUG o.s.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/jsp/index.jsp] in InternalResourceView 'index'
2016-01-13 14:47:01 DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/Spring4MVC/WEB-INF/jsp/index.jsp]
2016-01-13 14:47:01 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /WEB-INF/jsp/index.jsp
2016-01-13 14:47:01 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/WEB-INF/jsp/index.jsp]
2016-01-13 14:47:01 WARN  o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/Spring4MVC/WEB-INF/jsp/index.jsp] in DispatcherServlet with name 'dispatcher'
2016-01-13 14:47:01 DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
2016-01-13 14:47:01 DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request

Read many blogs every one is saying one should not use / because it will intercept all the calls, then how it is possible to show first page when only site name is typed?

[My Solution] Then by error and trial found settings that work for my case,

  1. if nothing is typed after site name then WelcomeController only method is called,
  2. If incorrect url is typed then 404 error is received which is correct(Before that every wrong url was redirected to WelcomeController) like sitename.com/adjahdkahXepowe
  3. Correct url mapped properly on other configured Controllers like sitename.com/about and sitename.com/about/development

Here is the change (instead of /*, used /)

@Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

My question is it the correct way to create default controller for welcome screen only when site name is only typed in browser.

e.g: sitename.com
PHP Avenger
  • 1,744
  • 6
  • 37
  • 66

1 Answers1

0

Just to make sure we are talking about the same thing here let's agree on sitename.com being the root URL of your web application. Meaning, your web server will somehow make sure that any request to sitename.com is redirected to your servlet.

If so, your current approach is correct.

You might want to have a look at the spring-MVC-project template provided in STS (see https://spring.io/tools/sts). Apart from you annotating and the authors of that template using .xml it's well related.

Don't worry about a default Controller. There's no need to have a dedicated Controller for your '/' URL. You might as well create one Controller for all pages of your application that are more or less static, for example legal notice page, about page, contact page and so on.

Depending on the size of your project it is a good idea to spread URLs over several Controllers, so to keep a clear view. I wouldn't create one for every URI on the other hand.

As to the question of servlet mapping '/' vs '/*' see Difference between / and /* in servlet mapping url pattern. As usual BalusC explains complete and plain.

MyBrainHurts
  • 2,480
  • 2
  • 27
  • 27