Announcement Announcement Module
Collapse
No announcement yet.
formBackingObject bindings lost Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • formBackingObject bindings lost

    Hi!

    Im still working on getting Spring known and in my sample application I have got a user form to add and remove users. I created a simple UserFormCommand Bean which holds a User object and a second password.

    This is my command Object with the name "user"

    Code:
    public class UserFormCommand {
    	private User user;
    	private String secondPassword;
    }
    Code:
    <spring&#58;bind path="user.user.username"> 
    					<input type="text" name="username" value="$&#123;user.user.username&#125;"/> 
    					<span class="fieldError">$&#123;status.errorMessage&#125;</span> 
    				</spring&#58;bind>
    The controller (SimpleFormController) is used to create a new user (e.g. store it) or retrieve and display a user in the form.

    Code:
    protected Object formBackingObject&#40;HttpServletRequest request&#41; throws ServletException &#123; 
    		String username = request.getParameter&#40;"usernameID"&#41;; 
    		if &#40;&#40;username != null&#41; && !username.equals&#40;""&#41;&#41; &#123;
    				UserFormCommand userFormCmd = new UserFormCommand&#40;&#41;;
    				userFormCmd.setUser&#40; mgr.getUser&#40;request.getParameter&#40;"usernameID"&#41;&#41;&#41;;
    				return userFormCmd;
    		&#125; 
    		else &#123;
    			UserFormCommand cmd = new UserFormCommand&#40;&#41;; 
    			return cmd;
    		&#125; 
    	&#125;
    
    public ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41; 
    	throws Exception &#123; 
    		UserFormCommand userFormCmd = &#40;UserFormCommand&#41; command;
    		User user = userFormCmd.getUser&#40;&#41;;
    		
    		if &#40;request.getParameter&#40;"delete"&#41; != null&#41; &#123; 
    			log.debug&#40;"delete user"&#41;;
    			mgr.removeUser&#40;user.getUsername&#40;&#41;.toString&#40;&#41;&#41;; 
    			request.getSession&#40;&#41;.setAttribute&#40;"message", 
    					getMessageSourceAccessor&#40;&#41; 
    					.getMessage&#40;"user.deleted", 
    							new Object&#91;&#93; &#123;user.getFirstName&#40;&#41; + ' ' + user.getLastName&#40;&#41;&#125;&#41;&#41;;
    		&#125; else &#123; 
    			// distinct between update and save - because of assigned id in relation user
    			if &#40;request.getParameter&#40;"update"&#41; != null&#41; &#123;
    				mgr.updateUser&#40;user&#41;;
    			&#125;
    			else &#123;
    				mgr.saveUser&#40;user&#41;;
    			&#125;
    			
    			request.getSession&#40;&#41;.setAttribute&#40;"message", 
    					getMessageSourceAccessor&#40;&#41;.getMessage&#40;"user.saved", 
    							new Object&#91;&#93; &#123;user.getFirstName&#40;&#41; + ' ' + user.getLastName&#40;&#41;&#125;&#41;&#41;; 
    		&#125; 
    		
    		return new ModelAndView&#40;new RedirectView&#40;getSuccessView&#40;&#41;&#41;&#41;; 
    	&#125;
    The problem is that if I create a new UserFormCommand (like above) the bindings are gone and all properties are set to null in UserFormCommand. If I change my command object to User and bind the properties to that one everything works on onSubmit() (eg. all the bindings are still in User)

    Can anyone give me a hint why I loose the bound properties in UserFormCommand from the jsp page?

  • #2
    Code:
    <spring&#58;bind path="user.user.username">
                   <input type="text" name="username" value="$&#123;user.user.username&#125;"/>
                   <span class="fieldError">$&#123;status.errorMessage&#125;</span>
                </spring&#58;bind>
    Wouldn't you need name="user.username" to bind as you want?

    But to avoid such issues, better to do:
    Code:
    <spring&#58;bind path="user.user.username">
                   <input type="text" name="$&#123;status.expression&#125;" value="$&#123;status.value&#125;"/>
                   <span class="fieldError">$&#123;status.errorMessage&#125;</span>
                </spring&#58;bind>
    HTH

    Comment


    • #3
      thank you for your answear ...

      Wouldn't you need name="user.username" to bind as you want?
      I don't think so because "user" is the name of the command class (UserFormCommand) and the second "user" after the dot is the name of the class User in the command class.

      Code:
      public class UserFormCommand &#123;
         private User user;
         private String secondPassword;
      &#125;

      Comment


      • #4
        Code:
        <spring&#58;bind path="user.user.username">
                       <input type="text" name="$&#123;status.expression&#125;" value="$&#123;status.value&#125;"/>
                       <span class="fieldError">$&#123;status.errorMessage&#125;</span>
                    </spring&#58;bind>
        HTH[/quote]

        If I use "${status.expression} and ${status.value} the values are empty. So I get nothing back ..... maybe that has something to do with my problem.

        Im kind of desperate right now .. I spent a few hours now and I still don't know what the problem is.

        Comment


        • #5
          Same problem for me

          Strangely
          I have the same problem on code like that

          Code:
          <c&#58;forEach var="registration" begin="0" items="$&#123;presences.listOfStudents&#125;" varStatus="loopstatus">
            <tr>
              <spring&#58;bind path="presences.listOfStudents&#91;$&#123;loopstatus.index&#125;&#93;.status">
                <td><input type="radio" name="<c&#58;out value='$&#123;status.expression&#125;'/>" value="P"></td>
                <td><input type="radio" name="<c&#58;out value='$&#123;status.expression&#125;'/>" value="A"></td>
              </spring&#58;bind>
            </tr>
          </c&#58;forEach>
          The binding on presences.listOfStudents[${loopstatus.index}].status
          don't work as I don't get setted values

          Comment

          Working...
          X