Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC portlet actionmapping redirect (avoid rendermapping) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC portlet actionmapping redirect (avoid rendermapping)

    Hi all, I am trying to solve this problem:

    I got a portlet with different method

    main method is a rendermapping

    Code:
    @RenderMapping
    	public String showMovies(RenderRequest request, RenderResponse response, Model model) throws IOException{
                model.addAttribute("movies",movieService.getAll());
                return "movies/index"
    }
    in my index.jsp i got a renderurl to

    Code:
    @RenderMapping(params={"action=showAddMovieForm"})
    	public String addMovieForm(RenderRequest request, RenderResponse response, Model model) throws IOException{
    		System.out.println("method: " + new Object(){}.getClass().getEnclosingMethod().getName());
    		model.addAttribute("movie", new Movie());
    		return "movies/showAddMovieForm";
    	}
    and the relative showAddMovieForm.jsp

    Code:
            // CUT  ...
    	<portlet:renderURL var="addMovieUrl" escapeXml="false">
    			<portlet:param name="action" value="addMovie" /> 
    		</portlet:renderURL > 
    
    		<form:form modelAttribute="movie" method="POST" action="${addMovieUrl}"  htmlEscape="false"  >
    		 <table>
    		  <tr>
    		   <td><form:label path="title">Title:</form:label></td>	  
    		   <td><form:input path="title"/> <form:errors path="title" cssClass="error"/></td>
    		  </tr>
    		 </table>
    		  
    		 <input type="submit" value="Add" />
    		</form:form>
         // CUT ...
    when i send the form the actionurl is handled by

    Code:
    	@ActionMapping(params={"action=addMovie"})
    	public void addMovie(
    			@ModelAttribute("movie") Movie movie,
    			BindingResult result,
    			ActionRequest request,
    			ActionResponse response) throws IOException{
    		
    		movieValidator.validate(movie, result);
    		
    		if(result.hasErrors()){
    			logger.error("result has errors");
    			return; <-------------------------------------------- here i want to come back to showAddMovieForm (when it displays the error), and i want to do that avoding the annotation of this method in a rendermapping method 
    		}else{
    			logger.debug("result is error free");
    		}
    		
    		movieService.add(movie);
    		logger.debug("movie " + movie.getTitle() + " added" );
    	
    	}
    automatically after the actionmapping annotated method the showMovies method si automatically called

    someone have any idea how to solve this problem (read into the code of the last block) ?

    thanks a lot


    ps
    this is how I can solve the problem using a rendermapping annotated method:

    Code:
    @RenderMapping(params={"action=addMovie"})
    	public String addMovie(
    			@ModelAttribute("movie") Movie movie,
    			BindingResult result,
    			RenderRequest request,
    			RenderResponse response,
    			Model model) throws IOException{	
    		
    		movieValidator.validate(movie, result);
    		
    		if(result.hasErrors()){
    			logger.error("result has errors");
    			return "movies/showAddMovieForm";
    		}else{
    			logger.debug("result is error free");
    		}
    		
    		movieService.add(movie);
    		logger.debug("movie " + movie.getTitle() + " added" );
    		
    		model.addAttribute("movies",movieService.getAll());
    		
    		return "movies/index";
    	}
    Last edited by iv4l; Jun 13th, 2012, 04:15 AM.

  • #2
    Originally posted by iv4l View Post

    Code:
    	@ActionMapping(params={"action=addMovie"})
    	public void addMovie(
    			@ModelAttribute("movie") Movie movie,
    			BindingResult result,
    			ActionRequest request,
    			ActionResponse response) throws IOException{
    		
    		movieValidator.validate(movie, result);
    		
    		if(result.hasErrors()){
    			logger.error("result has errors");
    			return; <-------------------------------------------- here i want to come back to showAddMovieForm (when it displays the error), and i want to do that avoding the annotation of this method in a rendermapping method 
    		}else{
    			logger.debug("result is error free");
    		}
    		
    		movieService.add(movie);
    		logger.debug("movie " + movie.getTitle() + " added" );
    	
    	}
    Hi,

    Try this:

    Code:
    	@ActionMapping(params={"action=addMovie"})
    	public void addMovie(
    			@ModelAttribute("movie") Movie movie,
    			BindingResult result,
    			ActionRequest request,
    			ActionResponse response) throws IOException{
    		
    		movieValidator.validate(movie, result);
    		
    		if(result.hasErrors()){
    			logger.error("result has errors");
                            
                                
                            response.setRenderParameter("action","showAddMovieForm"); 
    
    			return; <-------------------------------------------- here i want to come back to showAddMovieForm (when it displays the error), and i want to do that avoding the annotation of this method in a rendermapping method 
    		}else{
    			logger.debug("result is error free");
    		}
    		
    		movieService.add(movie);
    		logger.debug("movie " + movie.getTitle() + " added" );
    	
    	}
    Every time after action, portlet will enter in render phase. By default will enter in method annotated with @RenderMapping which is default. But if you set parameters for other annotated method @RenderMapping, portlet will know what method to call.

    And use the action URL:
    Code:
    <portlet:actionURL var="addMovieUrl" escapeXml="false">
           <portlet:param name="action" value="addMovie" />
       </portlet:actionURL>
    Last edited by adrian.steuerhaus; Jan 30th, 2013, 11:20 AM.

    Comment

    Working...
    X