0

I use the following code to map the DispatcherServlet to / URL:

@Override
protected String[] getServletMappings() {
    // TODO Auto-generated method stub
    return new String[] {"/"};      
}

And I can visit my @Controller handler method with the following URL:

http://localhost:8080/MyWebApp/controller1/method1

Then I changed the DispatcherServlet mapping to below:

@Override
protected String[] getServletMappings() {
    // TODO Auto-generated method stub
    return new String[] {"/app"};    //<=== HERE    
}

I expected the new URL below should work, but actually not.

http://localhost:8080/MyWebApp/app/controller1/method1

It always gives me 404 error.

I tried the following 2 ways to configure the Controller, neither works.

Option 1:

@Controller
@RequestMapping("controller1")
public class Controller1 {
    @RequestMapping("method1")
    public String Method1(Model model) {
     ...
    }

Option 2:

@Controller
@RequestMapping("/app/controller1") //<--- Still doesn't work.
public class Controller1 {
    @RequestMapping("method1")
    public String Method1(Model model) {
     ...
    }

Why? How does the getServletMappings() affect the final URL?

ADD 1

Strange enough. If I write /app/* instead of just /app. It works. But if so, how could / work at all? It should be /* if the logic is consistent.

But as I tried, if I use /*, the servlet engine seems malfunctioning, it even failed to parse the jsp page. Below is what I see in my browser. How could the JSP page source be displayed?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> ROOT: ${root}!

WEB: ${servlet}!

ADD 2

My WebMVC configuration:

@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses= {com.its.cloud.web.BeaconClassForComponentScan.class})
public class ITSCloudWebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ServletApplicationContextExplorer servletApplicationContextExplorer() {
        return new ServletApplicationContextExplorer();
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

The structure of the package that contains my controllers:

enter image description here

By using the BeaconClassForComponentScan as the marker class for @ComponentScan annotation, I believe the web, interfaces, impls and controllers pacakges will all be scanned.

Ref:

Maybe a related thread, but still not fixing the issue: Spring MVC configure url-pattern

Community
  • 1
  • 1
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • Check your component scan, it seems your controllers are not registered as spring beans. – px5x2 Nov 04 '15 at 13:22
  • @px5x2 Sorry, I forget to paste the `@Controller` annotation in my code snippet. I do have it in my code. The component scan should be alright. I updated my question. – smwikipedia Nov 04 '15 at 13:38
  • Could you also provide your web mvc configuration? – px5x2 Nov 04 '15 at 13:39
  • @px5x2 Added the webmvc configuration class to my question. – smwikipedia Nov 04 '15 at 13:43
  • Could you include your controllers' package as `basePackages` in `@ComponentScan` annotation? Like `@ComponentScan(basePackages="com.foo", ...)` – px5x2 Nov 04 '15 at 13:55
  • @px5x2 Still the same issue. And I updated my question. – smwikipedia Nov 04 '15 at 14:04
  • 1
    This has very little to do with Spring MVC. A servlet mapping is not a path prefix. It has rules that a Servlet container must follow. Those are explained in the linked question. – Sotirios Delimanolis Nov 04 '15 at 15:46
  • @SotiriosDelimanolis Thanks! Indeed. It's part of the Servlet spec. I am new to Servlet technology. That link does help. – smwikipedia Nov 05 '15 at 04:00

0 Answers0