简体   繁体   中英

How to validate Dropdown in Struts2

I am writing an action class in which I want to print errors using Struts2 . In my JSP page I got a drop down and I have 10 states in it. When I deploy the application the field error for Null always shows up without even clicking any button. I want that error to be printed in case if a user doesn't select any state from the dropdown.

Action Class:

public String modifyState() {
        CatastropheDataTO catDataTo = new CatastropheDataTO();

        try {
            catDataTo.setState(this.state);
            catDataTo.setActive(Boolean.valueOf(this.active));
            catDataTo.setStartDate(this.startDate);

            if( getState() != null ||  getActive() != null || getStartDate() != null ) {
                getCatastropheManager().updateCatastropheData(catDataTo);
                addActionMessage(this.getErrorMessageFactory().generateMessage(Constants.ERROR_CODE_7).getMessageText());

            } else if(getState() == null){
                addFieldError("state", "Please select a state");
            }

        } catch (Exception ex) {
            addActionError(this.getErrorMessageFactory().generateMessage(Constants.ERROR_CODE_3028).getMessageText());
        }
        return SUBMIT;
    }

Somehow the getState() is always null due to which "Please select a state" always show up, while I want this to be printed if a user directly clicks on the submit button without selecting any state.

Simply use two actions ( that can be two methods of the same Java class ), the first one to render the page, the second one to perform the business that should be executed after pressing the button.

In your case:

public String showStates() {
    return SUCCESS;
}

public String modifyState() {
    CatastropheDataTO catDataTo = new CatastropheDataTO();

    try {
        catDataTo.setState(this.state);
        catDataTo.setActive(Boolean.valueOf(this.active));
        catDataTo.setStartDate(this.startDate);

        if( getState() != null ||  getActive() != null || getStartDate() != null ) {
            getCatastropheManager().updateCatastropheData(catDataTo);
            addActionMessage(this.getErrorMessageFactory().generateMessage(Constants.ERROR_CODE_7).getMessageText());

        } else if(getState() == null){
            addFieldError("state", "Please select a state");
        }

    } catch (Exception ex) {
        addActionError(this.getErrorMessageFactory().generateMessage(Constants.ERROR_CODE_3028).getMessageText());
    }
    return SUBMIT;
}

That said, your approach is bypassing the mechanisms of the framework, that provide you several inbuilt methods to perform the validation ( validate() , XML , annotations ...).

The most similar to the one you're using is the validate() method that (to prevent the previously explained problem to arise again, can be applied to some method only, instead that to all the action methods of the class, with validateXXX() or @SkipValidation .

The result would be something like:

@SkipValidation
public String showStates() { // call this the first time
    return SUCCESS; // or INPUT, or whatever
}

public void validate(){
    if(getState() == null){
        addFieldError("state", "Please select a state");
    }
}

public String modifyState() {
    CatastropheDataTO catDataTo = new CatastropheDataTO();

    try {
        catDataTo.setState(this.state);
        catDataTo.setActive(Boolean.valueOf(this.active));
        catDataTo.setStartDate(this.startDate);

        if( getActive() != null || getStartDate() != null ) {
            getCatastropheManager().updateCatastropheData(catDataTo);
            addActionMessage(this.getErrorMessageFactory().generateMessage(Constants.ERROR_CODE_7).getMessageText());

        } 

    } catch (Exception ex) {
        addActionError(this.getErrorMessageFactory().generateMessage(Constants.ERROR_CODE_3028).getMessageText());
    }
    return SUBMIT;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM