Domanda

Suppose I have a bean which has lets say 10 properties. Also I have a error onject which has three propertes viz. errorFlag, errCd, and errMsg And I want to validate that bean properties sequentialy and I want to break on first occcurence of error and populate the error object. How can I do that, I want to have minimum If. else if statements.

ErrorObj Bean

public class ErrObj{
    private String errorMessage;
    private String errorCode;
    private boolean errorFlag;
    public String getErrorMessage() {
        return errorMessage;
    }
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
    public String getErrorCode() {
        return errorCode;
    }
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
    public boolean isErrorFlag() {
        return errorFlag;
    }
    public void setErrorFlag(boolean errorFlag) {
        this.errorFlag = errorFlag;
    }   
}

Below is the class where I am validating the Bean Assume, im using some static methods of Custom util class which return boolean depending upon if a field is valid or not.

 public class ValidateMyBean{

    ErrObj errObj = new ErrObj();
    if(!CustomUtils.isValid(myBean.getProp1))
    {
       // set some error conditions
       errObj.errorCode("123");
       errobj.setErrorMessage("validation 1 Failed");
    }
    else if(!CustomUtils.isValid(myBean.getProp2))
    {

    }
    ..........so on

    }

So, if my bean has 10 properties and I have to validate each property against lets say 3 validator methods, I don't want to write multiple "if - elseif" statements or write mutliple "if" statements with return statement at the end of each if statement, in case of any error. Is there any cleanser way or there are some frameworks which can help me reach the desired result? PS:This Java application is using Spring Core framework and is a webservice application and not a MVC application.

È stato utile?

Soluzione

There are many validation utils. You can check this example:

Spring MVC: How to perform validation?

There is also a jsr spec about it: JSR-303

http://beanvalidation.org/1.0/spec/

Enjoy

Altri suggerimenti

Accessing Java Beans using JSP:

Using JSP writing a java bean program becomes more easier.

The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a scripting variable that can be accessed by both scripting elements and other custom tags used in the JSP. The full syntax for the useBean tag is as follows:

<jsp:useBean id="bean's name" scope="bean's scope" typeSpec/>

Here values for the scope attribute could be page, request, session or application based on your requirement. The value of the id attribute may be any value as a long as it is a unique name among other useBean declarations in the same JSP.

Following example shows its simple usage:

<html>
<head>
<title>useBean Example</title>
</head>
<body>

<jsp:useBean id="date" class="java.util.Date" /> 
<p>The date/time is <%= date %>

</body>
</html>

This would produce following result:

The date/time is Thu Sep 30 11:18:11 GST 2010

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top