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:
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:
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