Home > J2EE, MVC, Spring Framework, Spring MVC 3 > Spring MVC Default Controller Name

Spring MVC Default Controller Name

November 30th, 2012 Leave a comment Go to comments

In post I will show how to set a Spring MVC 3 default controller.

Add a request mapping to “/”. Other, more specific mapping will “win” while this mappaing will be used as default for example: localhost://myapp.com/ will fall into this method.

@Controller
public class MyUserInfoController {

	@RequestMapping(value = "/")
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response) {

		ModelAndView modelAndView = new ModelAndView("mydefault"); 
		
		return modelAndView;
	}
...

Now the create a simple mydefault.jsp file and that will be placed in the viewResolver bean in spring-servlet.xml (where all other jsp file are placed.)

        
		
		
		
	

In web.xml file set the url pattern to be the root of the application.

Note that though there is the <mvc:resources mapping=”/resources/**” location=”/resources/”>, for small projects for which I want at least some of the static resources to be under the same web application (images, js and css etc…) I dont set the pattern URL to be “/” but I use something like: “/app/”
I do that because with certain application servers the resources configuration was not working as expected andI had to work hard on that. Setting a base URL for spring MVC mappings is very convinient, so it may work for you in terms of URL structure. I recommend doing so.


		spring
		org.springframework.web.servlet.DispatcherServlet
		1
	
	
		spring
		/app/*
	

Enjoy and Good Luck!

  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.