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.