Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC UrlBasedViewResolver Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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.

    web.xml
    Code:
    	<servlet>
    		<servlet-name>MVC-Dispatcher</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>
    				/WEB-INF/spring/*.xml
    			</param-value>
    		</init-param>
    		<load-on-startup>5</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>MVC-Dispatcher</servlet-name>
    		<url-pattern>/dispatcher/*</url-pattern>
    	</servlet-mapping>
    In my mvc-config.xml
    Code:
    <bean class='org.springframework.web.servlet.view.ContentNegotiatingViewResolver' p:order='1'>
    		<property name='mediaTypes'>
    			<map>
    				<entry key='json' value='application/json' />
    				<entry key='html' value='text/html' />
    			</map>
    		</property>
    
    		<property name='defaultContentType' value='application/json' />
    		<property name='favorPathExtension' value='true' />
    
    		<property name='viewResolvers'>
    			<list>
    				<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"/>
    				</bean>
    			</list>
    		</property>
    
    		<property name='defaultViews'>
    			<list>
    				<bean class='org.springframework.web.servlet.view.json.MappingJacksonJsonView' />
    			</list>
    		</property>
    		<property name='ignoreAcceptHeader' value='true' />
    	</bean>
    My Link to the form
    <a href="store/dispatcher/register">Register</a>

    My RegisterUserController
    Code:
    @Controller
    @RequestMapping("/register")
    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);
    		if(result.hasErrors()){
    			return "/user/register";
    		}
    		else {
    			return "redirect:/user/somepage";
    		}
    	}
    	
    }
    The view that I get returned looks like this.
    http://www.flickr.com/photos/98628127@N00/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, 04: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

    Comment


    • #3
      Thanks Don, that resolved my issue.

      Comment

      Working...
      X