Announcement Announcement Module
No announcement yet.
Spring MVC UrlBasedViewResolver Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC UrlBasedViewResolver

    Hello guys.

    I have started learning Spring specifically MVC, so please bear with my newbie question. I am encountering some difficulties which I cannot overcome. I have a user registration form that when I request the form the view resolver doesnt take me to the jsp, instead it shows a blank page with the RegisterFormBean object. Here is my configuration.

    In my mvc-config.xml
    <bean class='org.springframework.web.servlet.view.ContentNegotiatingViewResolver' p:order='1'>
    		<property name='mediaTypes'>
    				<entry key='json' value='application/json' />
    				<entry key='html' value='text/html' />
    		<property name='defaultContentType' value='application/json' />
    		<property name='favorPathExtension' value='true' />
    		<property name='viewResolvers'>
    				<bean class='org.springframework.web.servlet.view.BeanNameViewResolver' />
    				<bean class='org.springframework.web.servlet.view.UrlBasedViewResolver'>
    					<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    					<property name="prefix" value="/WEB-INF/jsp/"/>
    					<property name="suffix" value=".jsp"/>
    		<property name='defaultViews'>
    				<bean class='org.springframework.web.servlet.view.json.MappingJacksonJsonView' />
    		<property name='ignoreAcceptHeader' value='true' />
    My Link to the form
    <a href="store/dispatcher/register">Register</a>

    My RegisterUserController
    public class RegisterUserController {
    	@RequestMapping(method = RequestMethod.GET)
    	public String setupForm(Model model) {
    		model.addAttribute(new RegisterFormBean());
    		return "user/registerform";
    	@RequestMapping(method = RequestMethod.POST)
    	public String processSubmit(@ModelAttribute RegisterFormBean user, BindingResult result, SessionStatus status) {
    		new RegisterUserValidator().validate(user, result);
    			return "/user/register";
    		else {
    			return "redirect:/user/somepage";
    The view that I get returned looks like this.[email protected]/5436995868/

    Ok, I'm fairly certain that the problem lies in the mvc-config.xml. When I request the url I get JSON data back instead of getting returned to a view. Can someone please look at my mvc config and help me figure out what is wrong with my config?

    thank you.

    Thank you
    Last edited by spring_mojo; Feb 11th, 2011, 03:40 PM. Reason: Update

  • #2
    First thing I'd suggest is to use the InternalResourceViewResolver instead of the UrlBasedViewResolver. I don't think this is the problem, but it'll remove the need for you to specify the JstlView.

    The key problem appears to be the fact that you're declaring JSON to be your default content type. As a result, the ONLY requests that will render with the JstlView are those that end with ".html" i.e. <a href="store/dispatcher/register.html">Register</a>

    I'd recommend switching to text/html as your default content type (you can remove the defaultView) and just explicitly specifying json requests. You can try to do the reverse, but in my experience it can be very easy to forget the suffix in a web page.

    Hope this helps
    - Don


    • #3
      Thanks Don, that resolved my issue.