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

  • Validator

    Hi,
    I have written a class that implements the Validator interface.The following is the code

    Code:
    public class ValidateRegistration implements Validator {
    	public boolean supports(Class clazz) {
    		return clazz.equals(RegistrationInfo.class);
    	}
    	public void validate(Object command,Errors errors) {
    		RegistrationInfo registrationInfo = (RegistrationInfo) command;
    		ValidationUtils.rejectIfEmpty(errors, "firstName", "required.firstName");
    	}
    }
    Here firstName is the field name in the bean class.And i have included this validator in the servlet xml as follows :

    Code:
    class="com.dispatcher.RegistrationController">
    		<property name="formView" value="registration"></property>
    		<property name="successView" value="RegSuccess"></property>
    		<property name="registrationService" ref="registrationService" />
    
    		<property name="validator">
    			<ref bean="registrationValidator" />
    		</property>
    		
    	</bean>
    	
    	<bean id="registrationValidator" class="com.dispatcher.ValidateRegistration"></bean>
    But it is not validating.Is there something i should do in the jsp page?
    Can anyone let me know this.

  • #2
    Can you show us the Controller code and the jsp. Might be that there is something wrong with that.

    Just as an aside I would change the error code to required instead of required.firstName. Spring will generate error codes for you including the required.firstName one.

    Comment


    • #3
      The following is the controller code
      Code:
      public class RegistrationController extends SimpleFormController {
      	protected final Log logger = LogFactory.getLog(getClass());
      	private RegistrationService registrationService;
      	public RegistrationController() {
      		setCommandClass(RegistrationInfo.class);
      	}
      	protected void doSubmitAction(Object command) throws Exception {
      		RegistrationInfo registrationInfo = (RegistrationInfo) command;
      		//logger.info("Got it"+registrationInfo);
      		System.out.println("form view:"+getFormView());
      		registrationService.registerUser(registrationInfo);
      		
      	}
      	public void setRegistrationService(RegistrationService registrationService) {
      		this.registrationService = registrationService;
      	}
      }
      In brief, the RegistrationInfo is a bean file.I have a jsp page which would take name of the user.If the user is not gonna enter the name and type submit my application should throw an error.

      My jsp is very simple,
      Code:
      <form name="regForm" method="post">
      <h4><a href="<c:url value="HomePage.htm"/>">Home</a></h4>
      <h1 align="center">Registration Form</h1>
      
      <table border="0" cellpadding="0" cellspacing="0" width="100%"
      	height="60%">
      	<tr>
      		<td>
      		<table align="center" border="0" cellpadding="0" cellspacing="0"
      			width="35%" height="65%">
      			<tr>
      				<td>FirstName</td>
      				<td><input type="text" name="fname"></td>
      			</tr>
      </table>
      		<table align="center" border="0" width="35%">
      			<tr>
      				<td align="center"><input type="submit" value="Submit"  /></td>
      			</tr>
      		</table>
      		</td>
      	</tr>
      </table>
      
      </form>
      
      </body>
      </html>

      Comment


      • #4
        Should the error message be printed in a separate jsp page.If so how it should be done

        Comment


        • #5
          Hi mdeinum,
          Can u help me out in this.I have sent the jsp and the controller code

          Comment


          • #6
            Your jsp is incorrect. You aren't binding to any fields you are just simply printing out a form with some basic input fields. Check the samples shipped with spring on how to do that, also check the showcases regarding the form tags in spring.

            It also looks like as if your jsp isn't posting to anywhere. I don't see an action attribute on your form.

            Your form part of your jsp should look something like this.

            Code:
            <form:form>
            <h4><a href="<c:url value="HomePage.htm"/>">Home</a></h4>
            <h1 align="center">Registration Form</h1>
            
            <table border="0" cellpadding="0" cellspacing="0" width="100%" height="60%">
            	<tr>
            		<td>
            		<table align="center" border="0" cellpadding="0" cellspacing="0"
            			width="35%" height="65%">
            			<tr>
            				<td>FirstName</td>
            				<td><form:input path="firstName"/></td>
            			</tr>
            			<tr>
            				<td align="center" colspan="2"><input type="submit" value="Submit" /></td>
            			</tr>
            		</table>
            		</td>
            	</tr>
            </table>
            
            </form:form>
            I suggest you read up on how to use spring mvc and its correspondig tags.
            Last edited by Marten Deinum; May 4th, 2007, 09:37 AM.

            Comment


            • #7
              displaying the error message

              Thanks for that information.I have just started off with springs.

              I latter realized that there are no form action in my jsp page. Can u just suggest me a link for referring the spring tags and stuff.

              And after including actions form how can i diaply my error message.

              I learn t tht there is a command status.errorMessage given by the framework as such to display error messages.Which sping tag should be use to diaply the error message.

              Comment

              Working...
              X