0

I am unable to got error message in jsp page.Actually, i trying to check the username and password is correct or not in database.Iam thrown dataexception from dao and catch at controller check the below code

SimpleFormController

public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,Object command,BindException errors) throws Exception {

        log.info("onSubmit handleRequest method"+request.getParameter("username"));
        boolean isError=false;
        String username="",password="",errMessage="";
        username=request.getParameter("username");
        password=request.getParameter("password");

        UserBean ubean=null;
        try{
             ubean=userservice.chkUsername(username,password);
        }catch(DataException ex){

            errMessage=ex.getMessage();
            isError=true;
            //throw ex;
        }
        if(isError==true){
            System.out.println("errMessage handleRequest method"+errMessage);
            errors.reject(errMessage);
            return showForm(request, response, errors);
        }
        log.info("After shownform method called");
        HttpSession session=request.getSession(true);
        session.setAttribute("User",ubean);
        return  new ModelAndView("UserPage");
        }



public ModelAndView showForm(HttpServletRequest request, HttpServletResponse response,BindException errors) throws Exception {
                 //Displaying Count size
    log.info("Show form Error Count"+errors.getErrorCount()+getFormView());
    return new ModelAndView(getFormView());
}

Iam trying to display in two ways in jsp page i.e.,

Jsp Page:-

1)
                <spring:hasBindErrors name="UserBean">
                     <h3>You have errors in your input!</h3>  
    <font color="red">  
                    <c:forEach items="${errors.allErrors}" var="error">  
     <spring:message code="${error.code}" text="${error.defaultMessage}"/>  
    </c:forEach>  
    </font>  
                   </spring:hasBindErrors>
2) <form:form action="userpage.htm" method="post"  onsubmit="javascript:fun()">
                    <tr>
                     <form:errors path="*"/> 
                      --------
                       --------
  </form:form>

i trying to dispaying above two ways in jsp.But finally i didn't got any thing.is there any issues with above code.plz help me

user1357722
  • 7,088
  • 13
  • 34
  • 43

2 Answers2

0

The approach I used to form validation was the following:

Add 'validator' property to your controller:

<bean class="YourController">
    ...
    <!-- Map a validator -->
    <property name="validator">
        <bean class="CustomerValidator" />
    </property>
    ...
 </bean>

Create class implementing org.springframework.validation.Validator and implement two methods: supports and validate:

public class CustomerValidator implements Validator{


   @Override
   public boolean supports(Class clazz) {

   }

   @Override
   public void validate(Object target, Errors errors) {

   }
}

I would eliminate validation logic from the body of your controller class.

Check the following example, I believe you will find it useful: Spring MVC form handling example

BartoszMiller
  • 1,245
  • 1
  • 15
  • 24
  • Iam checking the data with database table then i thrown the exception i..e invalida username/apssword.But if iam using validator,it working between form submittion and before database hit of object.is it right? – user1357722 May 09 '12 at 13:32
  • In my opinion you should inject your dao into your validator and implement all the logic inside your custom validator. Quote taken from [link](http://stackoverflow.com/questions/1045895/should-validators-in-spring-access-the-database): "Well your validators are just spring beans, right, so they can be injected with the service objects that handle data access. You can have your validators get data from the database without compromising the design." – BartoszMiller May 09 '12 at 13:50
0

Have you tried using BindingResult.hasError()? And specify on which field you want to display the error.

Also make sure that the error code you are using is there in your proerpty file.

If you use the ModelAttribute to bind the model to your view then its much easier to handle the errors in the jsp. If you want then I can put some code here.

Cheers.

Japan Trivedi
  • 4,445
  • 2
  • 23
  • 44