Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Go to successView() with a model

  1. #1

    Default Go to successView() with a model

    Hi

    I need to make a controller that returns a ModelAndView to a successView with a model. The problem is that the same controller must also, if is the case, return to the formView with errors. I'm using a SimpleFormController and the code for now is:

    Code:
    public class LoginFormController extends SimpleFormController{
    	
    	private UserManager userMan;
    	protected final Log logger = LogFactory.getLog(getClass());
    	
    	public ModelAndView onSubmit(Object command, BindException errors) throws Exception{    
    		String username=((PasswordGetter) command).getUser();
    		logger.info(((PasswordGetter)command).getUser());
    		
    		String inputPass=((PasswordGetter) command).getPassword();
    		
    		String realPass=userMan.getPassword(username);
    	
    		if(realPass==null){
    			logger.info("user rejected");
    			errors.rejectValue("password","error.wrongPassword",null,"Wrong Password");
    			return new ModelAndView(this.getFormView(), errors.getModel());
    		}
    		
    		else if(inputPass.equals(realPass)){
    			logger.info("returning from LoginFormController view to " + getSuccessView());        
    			User person=userMan.getInfoList(username);
    			logger.info("teste="+person.toString());
    			logger.info("user.fname="+person.getFirst_name());
    			logger.info("user.lname="+person.getLast_name());
    						
    			ModelAndView nModel= new ModelAndView(new RedirectView(getSuccessView()),errors.getModel());
    			
    			return nModel;
    		}
    		else{
    			logger.info("password rejected");
    			
    			errors.rejectValue("password","error.wrongPassword",null,"Wrong Password");
    			return new ModelAndView(this.getFormView(), errors.getModel());
    		}
    	}
    How can I return the person object to the successView?

    Thanks

    Rui Gonçalves

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Well how about adding it to the model.

    Code:
    else if(inputPass.equals(realPass)){
    	User person=userMan.getInfoList(username);
    	Map model = errors.getModel();
            model.put("person", person);
    	ModelAndView nModel= new ModelAndView(new RedirectView(getSuccessView()),model);
    	return nModel;
    }
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Hi again

    My question now is: how can I catch the 'person' in the jsp? I've tried:
    Code:
    <c:forEach items="${person.person}" var="user">
      	<c:out value="${user.first_name}"/> <i>$<c:out value="${user.last_name}"/></i><br><br>
    </c:forEach>
    <c:out value="${person.first_name}"/> <c:out value="${person.getLast_name }"/>
    and any of these works...

    Thanks

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    If you added the person to the model with the name person then in your jsp you can do

    Code:
    <c:out value="${person.first_name}/>
    This also requires that in your User class you have a getter for the first_name property. Like so:

    Code:
    public String getFirst_name()
    But to give a more precise answer, what is going wrong? Is nothing displayed? Are errors shown? Or something else?

    Also make sure you have the correct taglibraries included in your jsp file.

    Also you might want to check out the chapter regarding spring mvc in the reference guide. Especially the Spring MVC part and the part regarding JSTL
    Last edited by Marten Deinum; Oct 11th, 2006 at 09:38 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    What happens it's that nothing is displayed.
    The libraries that I use are:
    <%@ taglib prefix="spring" uri="/spring" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

    I've the respective getters and setters...
    Last edited by pranxas; Oct 11th, 2006 at 09:56 AM.

  6. #6

    Default

    My understanding is, if you add your objects to ModelAndView and then do a RedirectView, the attributes will not be visible to the jsp.

    The ModelAndView.addObject() simply adds the attributes to the request (--> request.setAttribute), while RedirectView does a sendRedirect internally, which means a new 'request' object.

    In which case you store the data in session and retrieve that in the jsp (like using pageContext.getSession()).

    Buf if you dont do RedirectView, and if the user simply browser-refreshes the successView, the browser will display the previous view/form (not expected behaviour).

  7. #7

    Default

    I also have been struggling with this issue.

    @vasya10's last post:

    In which case you store the data in session and retrieve that in the jsp (like using pageContext.getSession()).
    This is the best solution, right? How would a code sample look for this? Googling "spring +pagecontext" doesn't return much. Is pageContext just like the session variables in request.getSession()?

    And does this solution have these properties:
    -After user submits form, they can refresh this new view without having a POST-refresh warning/issue
    -One can somehow (how?) pass data from the form controller to the JSP page

    [edit] I think I found something finally. Apparently the solution is more complicated than I originally thought: http://www.springframework.org/docs/reference/mvc.html
    Last edited by veraction; Oct 11th, 2006 at 07:43 PM.

  8. #8

    Default

    In your onSubmit , you can add:

    Code:
    WebUtils.setSessionAttribute("person", person);
    In your jsp, you can reference the "pageContext" object directly (its an implicit object)

    Code:
    <%
      Person p = (Person) pageContext.getSession().getAttribute("person");
      p.getFirstName(); 
    %>
    If you want to use Jstl, you can still do:

    Code:
    <c: out  value="${person.name}"/>
    The c tag looks into both request object and pageContext objects to obtain the object.

    -After user submits form, they can refresh this new view without having a POST-refresh warning/issue
    When you do a redirectView, the POST warning will not happen, because now you are doing a http-GET on the servlet to retrieve the data.

    One can somehow (how?) pass data from the form controller to the JSP page
    I think the answer is yes - by using session (as mentioned above).

  9. #9

    Default

    Thanks for the quick response. That seems like something I'll have to utilize in an application I'm working on.

    By the way, are these two things the same:
    Code:
    WebUtils.setSessionAttribute("person", person);
    
    request.getSession().setAttribute("person", person);
    I wonder if WebUtils could then provide my Validators with access to session variables.

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Quote Originally Posted by vasya10
    The ModelAndView.addObject() simply adds the attributes to the request (--> request.setAttribute), while RedirectView does a sendRedirect internally, which means a new 'request' object.
    Well you are right . Totally read past the part of RedirectView.

    However if you are using a redirect view I also assume (I know I know never assume ) you are redirecting to a page which also has a controller? You could pass the id of the user to this controller and retrieve the user in that controller putting it in the model (referenceData) and you should be good to go.

    In your first controller
    Code:
    else if(inputPass.equals(realPass)){
    	User person=userMan.getInfoList(username);
    	Map model = errors.getModel();
            model.put("person", person);
    	ModelAndView nModel= new ModelAndView(new RedirectView(getSuccessView()+"uname=person.getUserName()));
    	return nModel;
    }
    Then in the referenceData method of your next controller
    Code:
    protected Map referenceData(HttpServletRequest request) throws Exception {
         String username = ServletRequestUtils.getStringParameter(request, "uname");
         User person = userMan.getInfoList(username);
         Map model = new HashMap();
         model.put("person", person);
    }
    Of when you have put it into the session you could also change the referenceData to this instead of adding code to your jsp page.

    Code:
    protected Map referenceData(HttpServletRequest request) throws Exception {    
         User person = (User) WebUtils.getSessionAttribute(request, "person");
         Map model = new HashMap();
         model.put("person", person);
    }
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •