How does `getServletMapping ()` affect the URL in Spring WebMVC?

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 @Controller with the following URL:

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

Then I changed the DispatcherServlet mapping below:

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

I was expecting the new URL below to work, but not really.

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

He always gives me a 404 error.

I tried the following 2 ways to configure Controller , and it doesnโ€™t work.

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 getServletMappings() affect the final URL?

ADD 1

Itโ€™s strange. If I write /app/* instead of just /app . It is working. But if so, how / to work at all? It should be /* if the logic is consistent.

But, as I tried, if I use /* , the servlet mechanism seems to be faulty, it could not even jsp page. Below I see in my browser. How can I show the source of a JSP page?

<% @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 containing my controllers:

enter image description here

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

Link

Perhaps a related thread, but still does not fix the problem: Spring MVC configure url-pattern

0
spring-mvc servlets
Nov 04 '15 at 13:14
source share

No one has answered this question yet.

See similar questions:

eighteen
How are servlet url mappings in web.xml used?
5
Spring MVC configure url-pattern

or similar:

2480
How do I send JSON data using Curl from a terminal / command line in Test Spring REST?
1873
What is the difference between @Component, @Repository and @Service annotations in Spring?
1074
How do servlets work? Create, Sessions, Shared Variables, and Multithreading
470
How does an auto repair shop work in Spring?
60
Spring MVC: difference between <context: component-scan> and <annotation-driven / "> tags?
3
Spring MVC - jsp not running
2
What causes a display error in this Spring MVC application?
one
Map JSON Method to URL
0
Confusion over application contexts, url-pattern and RequestMapping - all in one project
0
Launch issue View controller $ (document) .ready handler response in Spring MVC



All Articles