From the previous validation: Stringutils.isempty .... Verification to struts: xxxvalidate
Now use jsr303 to make it easier
Dependent Hibernate-validator-4.xx.jar
Changes in the Entity class
@Entity @cache (usage=cacheconcurrencystrategy.read_write) Public class Admin { @Id @GeneratedValue (Strategy=generationtype.identity) Private Integer ID; // Add notempty Annotations, if the empty hint @NotEmpty (message= "account cannot be empty") private String name; @NotEmpty (Message= "Password cannot be empty") private String password;
How the form is written has changed
First import the SPRINGMVC label
<%@ taglib prefix= "form" uri= "Http://www.springframework.org/tags/form"%>
<!--send an admin before you come to this form. Commandname= "Admin" -<Form:formMethod= "POST"CommandName= "Admin"> <legend>Login</legend> <!--<form:errors path= "*" cssclass= "help-inline text-error" element= "span"/> so that all errors are shown here, or can be written separately as below - <label>Account</label> <!--path is equivalent to the previous Name property - <Form:inputPath= "Name"/> <form:errorsPath= "Name"CssClass= "Help-inline text-error"element= "span"/> <label>Password</label> <Form:passwordPath= "Password"/> <form:errorsPath= "Password"CssClass= "Help-inline text-error"element= "span"/> <Divclass= "Form-actions"> <Buttonclass= "Btn btn-primary">Login</Button> </Div></Form:form>
----------------------------------------------------------
/*It used to be this way @RequestMapping (value= "/", Method=requestmethod.get) public String index () {return "index" ; } *//*now with the commandname= "admin" this form property must be passed to the page before the admin, although it is empty*/@RequestMapping (Value= "/", method=requestmethod.get) PublicString Index (model model) {Model.addattribute ("Admin",NewAdmin ()); return"Index";} /*There are also changes in the method of submission*/@RequestMapping (Value= "/", method=requestmethod.post)/*using jsr303, the submitted form must be accepted by the object, not with Name,password need to annotate @valid indicate that admin needs to be verified and @valid admin admin must be on the front*/ PublicString Login (@Valid Admin admin,bindingresult bindingresult,httpsession session,redirectattributes Redirectattributes) {/*add Bindingresult This parameter, if the validation has an error, return to the index page note, not redirect to index to do this effect is if there is an error will still be on the index page Face and prompt account and password can not empty the account write, but the password is not written, will only prompt the password is blank, and the Account input box fill in the information will not disappear is because 1. When we come to this page, we send an empty admin object. , so he put the value you wrote in the set into the admin box in the 2.input box, the information is not gone, because we return index instead of redirect, and then we get the value in admin to go in*/ if(Bindingresult.haserrors ()) {return"Index"; } Admin curradmin=Adminservice.login (Admin.getname (), Admin.getpassword ()); if(Curradmin = =NULL) {Redirectattributes.addflashattribute ("Message", "Incorrect account or password"); <!--redirects--return"redirect:/"; } Else{Session.setattribute ("Curr_admin", curradmin); return"Redirect:/book"; } }
springmvc-3.2-jsr303 Troubleshooting server-side validation issues