Combined with verification reduces the number of action and actionforms in Struts

zhaozj2021-02-12  124

In web applications developed with Struts frameworks, it is generally used to build an actionform for each action in the database, and each table member is actions. For example, in a simple application of a user registration, there may be some operations: new registration an account, modify an existing account, and delete an account. (Three basic operations corresponding to the database table).

The easiest way is to write three Actions, respectively, each Action corresponds to an ActionForm. This is relatively simple, but will result in a large number of classes, and the ActionForm corresponding to a table is generally the same (maybe some fields are not). So I use a registrationform.java in the struts-example in Struts I have a good action. In addition, in order to reduce the number of JSPs, it uses the tag to determine the properties Action in the RegistrationForm. This shows a few (examples of two), which are similar, such as the value of different Actions.

But even if this, the operation of each table still corresponds to several Actions. So Struts provides Dispatchction

[org.apache.struts.Actions.dispatchaction] To reduce the number of Actions of these related business logic to facilitate the development and maintenance of the system. Its method is:

1. Write an action inheritance Dispatchation. If it is a tool such as JBuilder, remember to remove the exact () method, because Struts will call this method first, if not, it will only look for an action mapping

The Parameter property is called according to this attribute to call the corresponding method. If you don't find the method specified by parameter, it will be wrong. JBuilder generates an exception-throwing Execute method, and we can develop this method originally, so habits a NULL, and then we implemented the method specified by parameter, and the results have been unable to pass.

2. Action mapping is added to the action mapping in the struts-config.xml file to use different methods to indicate different parameters, habits to take Parameter to Method, and the value of the facts can be made. If there is a request parameter in the actionform, it will cause errors. Action [/ newaccountaction] Does not contact method name is the error when I add a name for the text input box in the request (I entered when I entered).

In addition, many business logic associated with updating databases will need to verify, and only verify data in ActionForm before or before org.apache.struts.valiDator. If the client also needs to be verified, the JSP programmer must write JavaScript code yourself. This can be a very headache, because the general Java IDE does not debug the function of JavaScript, sometimes I have found that Document.FormName is wrong. . After introducing Validator Plugin, everything becomes simple, to make some basic verification (type, email, etc.) simply in validation.xml, or write an actionform's validate () method.

How to use: 1. Write a class inherited validatorform [org.apache.struts.validator.validatorForm], if you want to use dynamic FORMBEAN, modify the Struts-Config.xml FORM element.

2. Add

elements in Validation.xml. For example, the Struts-Validator example is used:

Depends = "Required, Mask, MINLENGTH">

Mask

^ / w $

minlength

5

It means: The FirstName property of the ValidatorForm (defined in struts-config.xml ) requires 3 validation: Required, the input is one or more characters, at least 5 character. Please refer to the Struts reference documentation for the property and child elements of the field. Regular expressions are referred to ORO and Regexp, which are Jakarta projects.

The above FORM element has a necessary attribute Name, which indicates the logical name of the ActionForm that needs to be verified, if an ActionForm corresponds to an action, which is of course no problem. But if an ActionForm wants to be used by multiple Action, and these Action needs to be verified, such as new registration a user needs to verify, and the information that has been registered users cannot be verified. Therefore, Struts provides a ValidatorActionform, which features that it is not based on the logical name of the Form, but verifies different ActionForms according to the path to the Action, because the Action of this Action is specified with the Name property when Action Mapping. Such an ACTIONFORM can be selectable or not authenticated.

Our goal is to correspond to a table, only one Action class (or its subclasses) and an actionform class (or its subclass). Therefore, you can choose to use Dispatchction to process all relevant business logic. The ActionForm can be ValidatorForm or ValidatorActionform. I am in the initial idea is: use ValidatorActionForm. In this way, you can specify the Name property of the FORM element in validation.xml as an action of Path, but there is no way to use the client to verify it if you find this. Client verification To specify FormName (logical name) in , I tried to change the RegistrationForm of Struts1.1's Validator example to the subclass of ValidatorActionform, and found it all validates. And in JSP, JavaScript is displayed directly. Change the FormName property to the action of the action, no use, do not know if there is any other way to implement (cannot modify the Validator class). Even if you don't consider the client authentication, you will also encounter a problem: to submit it to an action, some need to verify (such as new registration a user), some do not need (such as view user information). If you don't have any processing, you can only be verified, or you don't verify it. This is obviously not possible. At that time, I thought that if Validator can decide whether to verify it according to the parameters of the request action. For example, I am using it in validation.xml.

Depends = "Required">

If my request is registeraction? Method = adduser, it verifies, and if it is

Registeraction? Method = viewuser, it does not verify. So I joined the source code of the Validator package to find that the content in this validation.xml reads a instance of the ValidatorResource class. When verify the instance of this class, find the corresponding FORMBEAN, for example I am in , it is in the ValidatorPlugin's variable in the ValidatorPlugin when it is called by ValidatorPlugin's init () method (in init () method is called by ActionServlet). If the customer requests an action, it will now generate an actionform (or already existing) if this ActionForm's logical name is registerform, and the validate () of this ActionForm's parent class is called (this Actionform does not have a validate () method, or With super.validate () in the validate () method, ValidatorForm will find this ValidatorResource to find the logical name of the ActionForm you want to verify, and the ValidatorActionForm first find an action through the path, then find the Action Mapping Name property to find you want to verify Actionform. So I use , it naturally can't find an action Path is / registerAction? Method = adduser, so there is no verification. It seems that it doesn't work.

Is there any other way? DispatchAction can select different methods according to different parameters, so you should be able to select different verification according to different parameters. So I just need to rewrite the Validate method. If the parameter is the ViewUser, it is not verified, otherwise the parent class is called VALIDATE (). However, there is a problem with this method, that is, you can't use dynamic Actionform, which is Dynavalidatorform, because it doesn't have this class at all, and it is impossible to write the validate () method.

Public ActionerRors Validate (ActionMapping ActionMapping, HttpservletRequest HttpservletRequest) {

/ ** @ Todo: Finish this method, this is just the skeleton. * /

String parameter = actionMapping.getParameter ();

String paramvalue = httpservletRequest.getParameter (parameter);

System.out.println ("from actionmapping" paramvalue;

Parameter = httpservletRequest.getParameter ("Method");

System.out.println ("From Request" parameter);

IF (Paramvalue.equals ("Viewuser")) {

Return NULL;

}

ActionerRors Errors = Super.Validate (ActionMApping, httpservletRequest); Return Errors;

}

Test it, it is fine, it is normal. Wrapped around a half-day, there was a countless breakpoint in the Struts source code, but found such a simple implementation method. But you have learned a lot. For open source projects, we always complain that there are too little documents, and the source code is the best document.

转载请注明原文地址:https://www.9cbs.com/read-7357.html

New Post(0)