Announcement Announcement Module
Collapse
No announcement yet.
Path Variable, resolving view Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Path Variable, resolving view

    Hello all,

    I'm new with Spring 3. I'm stuck in the following problem: I cannot map the view to the method with path variable.

    My code:

    Code:
    @Controller
    @RequestMapping(value = "/owner")
    public class OwnerController
    {	
            ............
    	
    	@RequestMapping(value = "/{ownerId}/edit", method=RequestMethod.GET)
    	public ModelAndView edit(@PathVariable("ownerId") String ownerId)
    	{
    		ModelAndView mav = new ModelAndView("edit");
    		return mav;		
    	}
    
            .............
    }
    I've got the following debug information:

    14:20:36.356 DEBUG DispatcherServlet.java:693 - DispatcherServlet with name 'web' processing GET request for [/Tutorial1/owner/1/edit.html]
    ...
    14:50:03.074 DEBUG InternalResourceView.java:236 - Forwarding to resource [/jsp/owner/1/edit.jsp] in InternalResourceView 'owner/1/edit'


    My view resolver configuration:

    Code:
    <bean id="viewResolver"
    		
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    
    		<property name="prefix" value="/jsp/" />
    		<property name="suffix" value=".jsp" />		
    	</bean>
    JSP file is located in the WebContent/jsp/owner/edit.jsp file.

    I create link to that method in the following way (another JSP file):

    Code:
    <table>
    			<c:forEach var="owner" items="${owners}">
    				<tr>
    					<td>${owner.firstName}</td>
    					<td>${owner.lastName}</td>
    					<td><a href="${owner.id}/edit.html">Edytuj...</a>
    				</tr>
    			</c:forEach>
    		</table>

    How can I map this method to the single view, not bothering about {ownerId}?

    Thanks in advance for your help,
    Best regards.
    Last edited by erhaminus; Feb 3rd, 2011, 08:51 AM.

  • #2
    (BTW, this is better covered over in the Web forum, but I'll answer it here nonetheless).

    Remember, the view name is completely independent of the request URL (unless you use conventions, but let's not go there right now).

    Right now, you return a view with the name "edit". To work with the InternalResourceViewResolver you've defined, you just need to return a view name of "owner/edit", i.e.

    Code:
    	@RequestMapping(value = "/{ownerId}/edit", method=RequestMethod.GET)
    	public ModelAndView edit(@PathVariable("ownerId") String ownerId)
    	{
    		ModelAndView mav = new ModelAndView("owner/edit");
    		return mav;		
    	}
    The path variable doesn't affect the view name in any way.

    As an aside, remember that you can just return the view name as a String, and let Spring MVC create the ModelAndView, i.e.

    Code:
    	@RequestMapping(value = "/{ownerId}/edit", method=RequestMethod.GET)
    	public String edit(@PathVariable("ownerId") String ownerId)
    	{
    		return "owner/edit";
    	}
    Hope this helps
    - Don

    Comment


    • #3
      Thank you dbrinker for your reply.

      Unfortunately, it does not work for me. Somehow, it is expecting jsp/owner/1/edit, not jsp/owner/edit.

      Please look at the following debug info:

      19:52:15.649 DEBUG InternalResourceView.java:236 - Forwarding to resource [/jsp/owner/1/edit.jsp] in InternalResourceView 'owner/1/edit'

      Maybe such behaviour is expected in Spring, I do not know, but if so, how to handle it?

      Best regards

      Comment


      • #4
        Right. I've seen this before, when people try to mix annotation-based mappings with returning a ModelAndView. Sorry I didn't recognize it up front.

        What's happening is that Spring is expecting a String for the view name (like I showed in my second example). If it doesn't get it, it automatically adds the return type to the model with a default name and tries to get a name based on the request URL (in this case, /owner/1/edit).

        Change your return type from a ModelAndView to a String and just return the view name. That should solve your problems. If you need to add stuff to the model, just pass in a Model argument to the method as well.

        Hope this helps
        - Don

        Comment


        • #5
          Thank you, Don. You have really helped me a lot. It works now perfectly.

          If I understand you correctly the following piece of code:

          Code:
          return "owner/edit"
          is NOT equal to

          Code:
          ModelAndView mav = new ModelAndView();
          mav.setViewName("owner/edit");
          return mav;
          My tests proved you are right, the first code works, the second does not.

          I am really curious why. I think I do not really catch it. I am reading manual atm but I cannot find any information on that. Is this approach just a bad practice and I am not allowed to use @RequestMapping annotation and ModelAndView at the same time? Why this makes difference?

          Thank you and best regards!

          Comment

          Working...
          X