Page 6 of 8 FirstFirst ... 45678 LastLast
Results 51 to 60 of 73

Thread: How to do validation using MultiActionController

  1. #51
    Join Date
    May 2009
    Posts
    6

    Default Using validators in multiaction controller

    Hi saravanan,
    Your example really worked.. As per your example i hv done changes but still there is some other error occurs.. Below is my codes. please refer and let me know where am i wrong?
    My bean class
    ------------------------
    public class StudRegBean {
    private String username;
    public String getUsername() {
    return username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
    }
    -----------------------------
    my controller class
    ----------------------
    public class regController extends MultiActionController {
    ServletRequestDataBinder binder;
    public ModelAndView saveRegistration( HttpServletRequest request, HttpServletResponse response,StudRegBean studRegBean) throws Exception { ModelAndView mv = new ModelAndView("Registration/studentRegistration");
    bind(request,studRegBean);
    validate(studRegBean);
    System.out.println("in save reg : " + studRegBean.getUsername() );
    if(errors.hasErrors())
    {
    Object[] tmp=errors.getModel().keySet().toArray();

    mv.addObject("username",errors.getModel());
    }
    return mv;
    }
    else
    { mv.addObject("studRegBean",studRegBean);
    return mv;
    }
    }
    BindingResult errors;
    public BindingResult getErrors() {
    return errors;
    }
    public void setErrors(BindingResult errors) {
    this.errors = errors;
    }
    @Override
    protected void bind(HttpServletRequest request, Object command) throws Exception {
    StudRegBean studRegBean=(StudRegBean)command;
    ServletRequestDataBinder binder=createBinder(request, command);
    binder.bind(request);
    errors=binder.getBindingResult();
    }

    public void validate(Object command)
    {
    Validator[] validators = getValidators();
    if (validators != null)
    {
    for (int index = 0; index < validators.length; index++)
    {
    Validator validator = validators[index];
    if (validator instanceof StudRegValidator)
    {
    if (((StudRegValidator)validator).supports(command.ge tClass()))
    ValidationUtils.invokeValidator(validators[index], command, errors);
    }
    else if (validator.supports(command.getClass()))
    ValidationUtils.invokeValidator(validators[index], command, errors);
    }
    }
    }
    }

    ------------------------------------------------------------------------
    My Validator class
    ------------------------------------------
    public class StudRegValidator implements Validator{
    public boolean supports(Class arg0) {
    return arg0.equals(StudRegBean.class);
    }
    public void validate(Object obj, Errors errors) {
    StudRegBean studRegBean=(StudRegBean)obj;
    try{
    if (studRegBean == null) {
    errors.rejectValue("username", "error.login.not-specified", null,"Value required.");
    }
    else{
    if (studRegBean.getUsername()== null || studRegBean.getUsername().trim().length() <= 0) {
    System.out.println("user name null value");
    ValidationUtils.rejectIfEmpty(errors, "username",null,"cannnot be null");
    }
    }
    }
    catch(Exception e){
    System.out.println("error caught in validator");
    e.printStackTrace();
    }
    }
    }
    ----------------------------------------------------------------------
    My Jsp Page
    ---------------------------
    <html><head>
    <script>
    function saveRegistration(){
    document.forms[0].param.value="saveRegistration";
    document.forms[0].submit();
    }
    </script></head>
    <body>
    <form:form action="studReg.htm" commandName="studRegBean">
    <input type="hidden" name="param" />
    <table cellpadding="2" border="1">
    <tr>
    <td class="labels" colspan="2" align="left" ><b>
    Account Details</b>
    </td>
    </tr>
    <tr>
    <td class="labels" align="left">*Username</td>
    <td align="left">
    <form:input path="username"/>
    <form:errors path="username"/>
    </td>
    </tr>
    <tr><td align="center">
    <input type ="button" value="Submit" onclick="saveRegistration()"/>
    <input type ="reset" value="Reset"/>
    <input type ="button" value="Cancel"/>
    </td></tr>
    </table>
    </form:form>
    </body>
    </html>
    -----------------------------------------------------------------------
    Finally the dispatcher servlet.xml
    -----------------------------------
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="index.htm">indexController</prop>
    <prop key="studReg.htm">regController</prop>
    </props>
    </property>
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver"
    prefix="/WEB-INF/jsp/"
    p:suffix=".jsp" />
    <bean id="selectMethodResolver" class="org.springframework.web.servlet.mvc.multiac tion.ParameterMethodNameResolver">
    <property name="paramName">
    <value>param</value>
    </property>
    </bean>
    <bean id="regController" class="controllers.regController">
    <property name="methodNameResolver">
    <ref bean="selectMethodResolver"/>
    </property>
    <property name="validators">
    <ref bean="studRegValidator"/>
    </property>
    </bean>

    ----------------------------------------------------------------------
    Error that is displayed is
    --------------------------------------------------
    org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'studRegBean' available as request attribute org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:521)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:409)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:336)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    org.springframework.web.servlet.view.InternalResou rceView.renderMergedOutputModel(InternalResourceVi ew.java:167)
    org.springframework.web.servlet.view.AbstractView. render(AbstractView.java:239)
    org.springframework.web.servlet.DispatcherServlet. render(DispatcherServlet.java:1158)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:900)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:808)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:476)
    org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:441)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doF ilter(ReplyHeaderFilter.java:96)
    root cause
    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'studRegBean' available as request attribute
    org.springframework.web.servlet.support.BindStatus .<init>(BindStatus.java:142)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.getBindStatus(AbstractData BoundFormElementTag.java:176)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.getPropertyPath(AbstractDa taBoundFormElementTag.java:196)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.getName(AbstractDataBoundF ormElementTag.java:147)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.autogenerateId(AbstractDat aBoundFormElementTag.java:134)
    org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.writeDefaultAttributes(Abs tractDataBoundFormElementTag.java:123)
    org.springframework.web.servlet.tags.form.Abstract HtmlElementTag.writeDefaultAttributes(AbstractHtml ElementTag.java:379)
    org.springframework.web.servlet.tags.form.InputTag .writeTagContent(InputTag.java:139)
    org.springframework.web.servlet.tags.form.Abstract FormTag.doStartTagInternal(AbstractFormTag.java:90 )
    org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(RequestContextAwareTag.java:7 7)
    org.apache.jsp.WEB_002dINF.jsp.Registration.studen tRegistration_jsp._jspx_meth_form_005finput_005f0( studentRegistration_jsp.java:649)
    org.apache.jsp.WEB_002dINF.jsp.Registration.studen tRegistration_jsp._jspService(studentRegistration_ jsp.java:318)
    org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:373)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:336)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    org.springframework.web.servlet.view.InternalResou rceView.renderMergedOutputModel(InternalResourceVi ew.java:167)
    org.springframework.web.servlet.view.AbstractView. render(AbstractView.java:239)
    org.springframework.web.servlet.DispatcherServlet. render(DispatcherServlet.java:1158)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:900)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:808)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:476)
    org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:441)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doF ilter(ReplyHeaderFilter.java:96)
    -------------------------------------------------------------------------

    My target is to show the errors beside the textboxes that are empty..
    Please refer it and let me know where am i wrong.
    Thanks for you reply..
    Thanks & Regards
    Levis[/LEFT][/CENTER][/LEFT][/LEFT]

  2. #52

    Default Hi prande

    Hi prande,

    your question,

    Can you please check if your form tag contains attribute commandName?


    my answer,

    No. My form tag does not contain attribute commandName. I skipped that attribute.

    Because,

    Since MultiActionController handles multiple requests, that is, since it can handle multiple forms, I made the commandName as default name.

    The default command name for MultiActionController is 'command'

    You need not worry about the command name.

    Refer the following scenario,

    Assume that, you have a jsp page called customer.jsp containing fristname and lastname as fields. And assume that you have bean class Customer.java with 2 fields firstname and lastname, along with getters and setters. And again assume that when I click the submit button in customer.jsp, the following things should be done,

    1. the customer.jsp form should be validated
    2. once there is no errors, it should go to success.jsp
    3. if there is errors in the form fields in customer.jsp, I should display the same in customer.jsp

    In ur controller which extends MultiActionController, assume that you methodhandler for customer.jsp, there, you have to give like this,

    Customer newCustomer = (Customer)newCommandObject(Customer.class);


    if errors, display the same page as (refer my reply #49)

    return new ModelAndView("customer",errors.getModel());

    Here is another scenario,

    Assume that, you want to display a new view (customerform.jsp) from home.jsp

    You may have methodhandler for handling the home.jsp request right? there you should write like the following

    return new ModelAndView("customerform", "command", newCommandObject(Customer.class));

    NOTE:
    If u see the above line, I used the default command name. It is better to have the default command name.

    Suppose, If ur multiactioncontroller handles more than one form, use default command name for all. for example, to return inventoryform.jsp once I click the submit button in customerform.jsp, use the same as,


    return new ModelAndView("inventoryform", "command", newCommandObject(Inventory.class));


    I hope I have done something useful. Let me know if anyone have any opinion.

    Enjoy,
    SARAVANAN SIVAJI
    Last edited by saravanansivaji; Jul 1st, 2009 at 06:42 AM.

  3. #53
    Join Date
    May 2009
    Posts
    6

    Default validators in multiaction controller

    Quote Originally Posted by saravanansivaji View Post
    Hi prande,

    your question,

    Can you please check if your form tag contains attribute commandName?


    my answer,

    No. My form tag does not contain attribute commandName. I skipped that attribute.

    Because,

    Since MultiActionController handles multiple requests, that is, since it can handle multiple forms, I made the commandName as default name.

    The default command name for MultiActionController is 'command'

    You need not worry about the command name.

    Refer the following scenario,

    Assume that, you have a jsp page called customer.jsp containing fristname and lastname as fields. And assume that you have bean class Customer.java with 2 fields firstname and lastname, along with getters and setters. And again assume that when I click the submit button in customer.jsp, the following things should be done,

    1. the customer.jsp form should be validated
    2. once there is no errors, it should go to success.jsp
    3. if there is errors in the form fields in customer.jsp, I should display the same in customer.jsp

    In ur controller which extends MultiActionController, assume that you methodhandler for customer.jsp, there, you have to give like this,

    Customer newCustomer = (Customer)newCommandObject(Customer.class);


    if errors, display the same page as (refer my reply #49)

    return new ModelAndView("customer",errors.getModel());

    Here is another scenario,

    Assume that, you want to display a new view (customerform.jsp) from home.jsp

    You may have methodhandler for handling the home.jsp request right? there you should write like the following

    return new ModelAndView("customerform", "command", newCommandObject(Customer.class));

    NOTE:
    If u see the above line, I used the default command name. It is better to have the default command name.

    Suppose, If ur multiactioncontroller handles more than one form, use default command name for all. for example, to return inventoryform.jsp once I click the submit button in customerform.jsp, use the same as,


    return new ModelAndView("inventoryform", "command", newCommandObject(Inventory.class));

    I hope I have done something useful. Let me know if anyone have any opinion.

    Enjoy,
    SARAVANAN SIVAJI

    Hi Saravanan,
    Did you went through my code that i had sent in the post. I am waiting for ur reply

    Regards
    Levis

  4. #54

    Default Hi levis

    Hi levis,

    do the following changes,

    1. in ur jsp page,

    <form:form action="studReg.htm" commandName="studRegBean">

    remove the attribute commandName,

    <form:form action="studReg.htm">


    2. in ur controller class,

    you have the following method right?

    public ModelAndView saveRegistration( HttpServletRequest request, HttpServletResponse response,StudRegBean studRegBean)


    remove the 3rd argument, and do the following changes

    public ModelAndView saveRegistration( HttpServletRequest request, HttpServletResponse response)
    {
    try
    {
    // do your process here
    }
    catch(Exception expObj)
    {
    HttpSession session = request.getSession();
    session.setAttribute("expInformation", expObj);
    return new ModelAndView("ShowException");
    //how u r handling the exception, is up to you
    }
    }



    3. In ur controller class,

    In the same saveRegistration method, you the following right?

    bind(request,studRegBean);

    include the following line before bind(request,command) method,

    StudRegBean studRegBean = (StudRegBean)newCommandObject(StudRegBean.class);
    bind(request,studRegBean)


    I hope this help,

    let me know if u have problem,

    Enjoy,
    SARAVANAN SIVAJI
    Last edited by saravanansivaji; Jul 1st, 2009 at 05:48 AM.

  5. #55
    Join Date
    May 2009
    Posts
    6

    Default validators in multiaction controller

    Quote Originally Posted by saravanansivaji View Post
    Hi levis,

    do the following changes,

    1. in ur jsp page,

    <form:form action="studReg.htm" commandName="studRegBean">

    remove the attribute commandName,

    <form:form action="studReg.htm">


    2. in ur controller class,

    you have the following method right?

    public ModelAndView saveRegistration( HttpServletRequest request, HttpServletResponse response,StudRegBean studRegBean)


    remove the 3rd argument, and do the following changes

    public ModelAndView saveRegistration( HttpServletRequest request, HttpServletResponse response)
    {
    try
    {
    // do your process here
    }
    catch(Exception expObj)
    {
    HttpSession session = request.getSession();
    session.setAttribute("expInformation", expObj);
    return new ModelAndView("ShowException");
    //how u r handling the exception, is up to you
    }
    }



    3. In ur controller class,

    In the same saveRegistration method, you the following right?

    bind(request,studRegBean);

    include the following line before bind(request,command) method,

    StudRegBean studRegBean = (StudRegBean)newCommandObject(StudRegBean.class);
    bind(request,studRegBean)


    I hope this help,

    let me know if u have problem,

    Enjoy,
    SARAVANAN SIVAJI


    Hi Saravanan,
    Thnx for ur reply.. I changed the code as u told. but still there is an error..
    error is as follows
    ---
    org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
    org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:521)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:409)

    ---

    now it says that binding is not with command..

    waiting for ur reply

    Thanks and regards
    levis

  6. #56
    Join Date
    Jun 2009
    Posts
    9

    Thumbs up

    Hi saravanansivaji,

    Thanks for your last post It really helped me ..

    I was doing mistake near following statment by not taking newCommandObject

    Customer newCustomer = (Customer)newCommandObject(Customer.class);

    Is it possible to validate nested property using same strategy?
    Last edited by prande; Jul 1st, 2009 at 06:31 AM.

  7. #57

    Default Hi levis

    Hi levis,

    Do the following changes too, I forget to mention the following,

    you have the following code in ur handler method in controller class right?

    ModelAndView mv = new ModelAndView("Registration/studentRegistration");

    if(errors.hasErrors())
    {
    Object[] tmp=errors.getModel().keySet().toArray();

    mv.addObject("username",errors.getModel());
    }
    return mv;
    }
    else
    { mv.addObject("studRegBean",studRegBean);
    return mv;
    }



    Now,

    change the following line, from
    mv.addObject("username",errors.getModel());

    to
    mv.addObject("command",errors.getModel());

    And, also in else part, change the following line, from

    mv.addObject("studRegBean",studRegBean);

    to


    mv.addObject("command",studRegBean);

    ----------------------------------------------------------------------------------------------------------
    NOTE:

    Have a look on the following link before u proceed


    http://forum.springsource.org/showpo...5&postcount=52
    ----------------------------------------------------------------------------------------------------------
    Hope I am directing you in correct way. Let me know, if u have problem still

    enjoy coding,
    SARAVANAN SIVAJI
    Last edited by saravanansivaji; Jul 1st, 2009 at 06:42 AM.

  8. #58

    Default Hi prande

    hi prande,

    ur question,

    Is it possible to validate nested property using same strategy?

    my friend,

    Could u post ur sample code containing the nested property?

    so that, its easy for me to exactly tell u the solution.

    regards,
    SARAVANAN SIVAJI

  9. #59
    Join Date
    May 2009
    Posts
    6

    Default

    Quote Originally Posted by saravanansivaji View Post
    Hi levis,

    Do the following changes too, I forget to mention the following,

    you have the following code in ur handler method in controller class right?

    ModelAndView mv = new ModelAndView("Registration/studentRegistration");

    if(errors.hasErrors())
    {
    Object[] tmp=errors.getModel().keySet().toArray();

    mv.addObject("username",errors.getModel());
    }
    return mv;
    }
    else
    { mv.addObject("studRegBean",studRegBean);
    return mv;
    }



    Now,

    change the following line, from
    mv.addObject("username",errors.getModel());

    to
    mv.addObject("command",errors.getModel());

    And, also in else part, change the following line, from

    mv.addObject("studRegBean",studRegBean);

    to


    mv.addObject("command",studRegBean);

    ----------------------------------------------------------------------------------------------------------
    NOTE:

    Have a look on the following link before u proceed


    http://forum.springsource.org/showpo...5&postcount=52
    ----------------------------------------------------------------------------------------------------------
    Hope I am directing you in correct way. Let me know, if u have problem still

    enjoy coding,
    SARAVANAN SIVAJI

    Hi Saravanan,
    Thanx for your prompt replies, I am realy enjoying the coding but real enjoyment will be when it is succeeded.. and still it didn't. I hv done modification as u said.. but the following error occurs

    org.apache.jasper.JasperException: org.springframework.beans.NotReadablePropertyExcep tion: Invalid property 'username' of bean class [java.util.HashMap]: Bean property 'username' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:521)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:415)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:336)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    org.springframework.web.servlet.view.InternalResou rceView.renderMergedOutputModel(InternalResourceVi ew.java:167)
    org.springframework.web.servlet.view.AbstractView. render(AbstractView.java:239)
    org.springframework.web.servlet.DispatcherServlet. render(DispatcherServlet.java:1158)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:900)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:808)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:476)
    org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:441)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doF ilter(ReplyHeaderFilter.java:96)


    Waiting for your reply

    Regards
    levis

  10. #60

    Default Hi Levis, sorry for the belated reply

    hi levis,

    Have you solved your problem? Do u need any help?

Posting Permissions

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