Announcement Announcement Module
Collapse
No announcement yet.
bind input to attributes other than the command class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • bind input to attributes other than the command class

    A newbie question again :wink:

    While playing around with a sample application I created a simple user manager. To add a user you have to enter a username, firstname, lastname, password, retype the password and the mail address.

    I use a SimpleFormController to process/store the submitted data. Binding the User attributes works fine! BUT how do I process the second password for validation if the user entered the password correctly twice? I can't bind html forms to attributes in the SimpleFormController class other than the User.

    One solution I see is to create a class which holds all the necessary data attributes I need and use that as the command class. This would be an overkill for such a small case in my opinion.

    Is there any other way to realize something like that?


    Code:
    <bean id="userFormController" class="example.web.UserFormController"> 
    		<!--  optional - if you don't define it then the default name is command -->
    		<property name="commandName">
    			<value>user</value>
    		</property> 
    		<property name="commandClass"> 
    			<value>example.model.User</value> 
    		</property> 
    		<property name="formView">
    			<value>userForm</value>
    		</property> 
    		<property name="successView">
    			<value>showUser.html</value>
    		</property> 
    		<property name="userManager">
    			<ref bean="userManager"/>
    		</property> 
    		
    		<property name="validator">
    			<ref bean="beanValidator"/>
    		</property>
    	</bean>
    the jsp
    Code:
    &#91;...&#93;
    <tr> 
    			<th><fmt&#58;message key="user.password"/>&#58; </th> 
    			<td> 
    				<spring&#58;bind path="user.password"> 
    					<input type="password" name="password" value="$&#123;user.password&#125;"/> 
    					<span class="fieldError">$&#123;status.errorMessage&#125;</span> 
    						<c&#58;if test="$&#123;not empty status.errorMessage&#125;"> 
    						</c&#58;if> 
    				</spring&#58;bind> 
    			</td> 
    		</tr> 
    		
    		<tr> 
    			<th><fmt&#58;message key="user.secondPassword"/>&#58; </th> 
    			<td> 
    				<spring&#58;bind path="secondPassword"> 
    					<input type="password" name="secondpassword" value="$&#123;secondPassword&#125;"/> 
    					<span class="fieldError">$&#123;status.errorMessage&#125;</span> 
    						<c&#58;if test="$&#123;not empty status.errorMessage&#125;"> 
    						</c&#58;if> 
    				</spring&#58;bind> 
    			</td> 
    		</tr> 
    &#91;...&#93;
    here is my Form Controller with the secondPassword attribute which is not part of the command class:
    Code:
    public class UserFormController extends SimpleFormController &#123; 
    	private static Log log = LogFactory.getLog&#40;UserFormController.class&#41;; 
    	private UserManager mgr = null;
    	private String secondPassword;
    	
    	public void setUserManager&#40;UserManager userManager&#41; &#123; 
    		this.mgr = userManager; 
    	&#125; 
    	
    	public UserManager getUserManager&#40;&#41; &#123; 
    		return this.mgr; 
    	&#125;
    	
    	public String getSecondPassword&#40;&#41; &#123;
    		return secondPassword;
    	&#125;
    
    	public void setSecondPassword&#40;String secondPassword&#41; &#123;
    		this.secondPassword = secondPassword;
    	&#125;
    &#91;...&#93;

  • #2
    I'd do the following.

    1. Create a new AddUserCommand object that contains a reference to the example.model.User object and an additional secondPassword property. That way, you don't put use-case specific details into your example.model.User class and keep it nice and clean. This seems (?) to be what you're trying to do.

    2. In your JSP, <spring:bind> to command.user.password and command.secondPassword.

    3. In your JSP, always use status.value and status.expression for the value and name attributes of the input field. This just means you're specifying the field name once -- in the bind tag.

    4. Don't store state in the controller. Follow Spring conventions unless you've got good reason and store state in the command object.

    Comment


    • #3
      Originally posted by gmatthews
      I'd do the following.

      1. Create a new AddUserCommand object that contains a reference to the example.model.User object and an additional secondPassword property. That way, you don't put use-case specific details into your example.model.User class and keep it nice and clean. This seems (?) to be what you're trying to do.
      thanks for the fast answear!

      That is what I want to avoid for so simple actions! For just one input field I have to create a new class that holds that value and the rest of the data. It is an overkill in my opinion if I do that for every single form that has got one piece of data which does not belong to the command class. Of course it would be cleaner to create a new command class with different attributes .... but only for bigger forms and not just for throwaway input forms like this "retype password field"

      I mean I just could get that value with a request.getParameter but that wouldn't be the Spring way... i guess :wink:

      Comment


      • #4
        take a look at the petstore example , it does exactly what you are looking for.

        serge

        Comment

        Working...
        X