ActionForm is the object of form, and the integrity check works for form data should be performed, such as whether the user fills in all the fields, whether all attributes in the ActionForm are set, you can redefine Actionform's Validate. ) methods to carry out this work, for example: package onlyfun.caterpillar; import javax.servlet.http *; import org.apache.struts.action *; public class UserForm extends ActionForm {protected String name; protected String password; public.. void setName (String name) {this.name = name;} public void setPassword (String password) {this.password = password;} public String getName () {return name;} public String getPassword () {return password;} public void reset (ActionMapping mapping, HttpServletRequest req) {name = null; password = null;} public ActionErrors validate (ActionMapping mapping, HttpServletRequest request) {ActionErrors errors = new ActionErrors (); if (getName () == null || getUsername ( ) .length () <1) {Errors.add ("name", new actionerror ("error.name.Required"); } IF (getpassword () == null || getpassword (). Length () <1) {ErrorS.Add ("password", new actionerror ("error.password.required));} return error;} When the user sends a form, and when there is a column in the form, the request will include the parameter name, but the value is a empty string, and if the ActionForm has some attributes, the form does not send the corresponding parameters, will not set The corresponding properties in ActionForm, these attributes will be null, and our validate () is primarily inspected these two situations.
The Validate () method will return Actionerror file, and ActionerRors can store the ActionError message, each ActionError will query the key-value corresponding in the resource file. When Validate () is dropped back, ActionServlet will not continue to follow. Work, but is the location set by StructS-config.xml, for example:
Actionform verifies the possibility of NULL and empty strings. This is the verification of the integrity of the data. Next, we must verify the correctness of the data, whether it meets the name and password we set, we rewrive the LoginAction of the previous theme, look any different wording: package onlyfun.caterpillar; import javax.servlet.http *; import org.apache.struts.action *; import org.apache.commons.beanutils *; public class LoginAction extends Action {public... ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {String name = (String) PropertyUtils.getSimpleProperty (form, "name"); String password = (String) PropertyUtils.getSimpleProperty (form, "password" ); if ((name.equals ( "caterpillar") && password.equals ( "1234")!)) {ActionMessages messages = new ActionMessages (); messages.add (ActionMessages.GLOBAL_MESSAGE, new ActionMessage ( "message.namepass. NOTMATCHED ")); SaveMessages (Request, Messages); Return mapping.findforward ("Welcome");} else {request.getations (). setttribute ("Valid_user", Form); return mapping.findforward ("greeting");}}} In this program, we use it Org.apache.comMons.BeanUtils PropertyUtils category to help us take the value in the actionform, the advantage is not to pay attention to the real form of Actionform, and PropertyUtils will automatically help us, getSimpleProperty () is Object, we will convert to String. ActionMessages is the new category of Struts 1.1. It has become the parent category of ActionerRRORS. Similarly, ActionMessage is also a new category of Struts 1.1, which is the parent category of ActionError, the data format and integrity check In Actionform we have verified Next, we check if you meet the name and password in the action, if you do not meet, join the relevant message.
In Struts 1.1, Message is distinguished from ERROR, which is determined that the so-called error is incorrectly incorrect by the user's input in integrity or format, and Message means that the input information is basically no error, but does not match the subsequent business. deal with. In order to be able to display an error and message, we must join the key-value corresponding to the application_zh.properties, as follows: # - error - Error.name.Required = No Enter name error.password.required = No password # - Message --Message.NamePass.notmatch = Name and password incorrectly in order to use Chinese, remember to convert using the Native2ASCII tool program, let's take a look at how our Welcome.jsp is written, pay attention to it is Please login h3>