Example of Webwork2 and Springframework Integration (Original) (5-2)

xiaoxiao2021-03-06  72

(4) Form processing Action: PriceIncReaseformAction

Package web;

Import org.apache.commons.logging.log;

Import org.apache.commons.logging.logfactory;

Import bus.priceincrease;

Import Bus.ProductManager;

Import com.opensymphony.xwork.actionsupport;

Public class priceIncreaseformaction extends actionSupport {

/ ** Logger for this class and subclasses * /

Protected final log logger = logfactory.getlog (getclass ());

Private productManager product;

Private priceIncrease priceincrease;

Public String Execute () throws exception {

INT INCREASE = GetPriceIncrease (). getpercentage ();

Logger.info ("Increase PriceS By" Increase "%.");

Prodman.Increaseprice (Increase);

RETURN SUCCESS;

}

Public productManager getProdman () {

Return Prodman

}

Public void setProdman (ProductManager Productman) {

This.Prodman = product;

}

Public PriceInCrease getPriceIncRease () {

Return PriceinCrease;

}

Public void setPriceIncrease (PriceIncrease PriceIncrease) {

this.priceincrease = priceIncrease;

}

}

l PriceInCreaseformAction implements the same features as the PriceInCreaseformController, but you should be simple:

Ø Do not need to collect the data required by the result view, this is implemented by the SpringAppAction (see the configuration of PriceInCreaseFormAction)

Ø Data return function is implemented on the page

l PriceInCreaseFormAction Configure in xwork.xml

Class = "Web.PriceInCreaseformAction">

Prodman

/ web-inf / template / priceIncrease.vm

/ web-inf / jsp / priceIncrease.jsp

->

SpringApp

l Indicates that if the table single field element data validity verification failed, redirected URL

l CHAIN ​​type Indicates that the processing is successfully redirected to the specified action, specifying the action reference name defined in xwork.xml by parameter actionname

l Intersource INTERCEPTOR reference for data validity verification so as to perform data validity verification before performing action, if the verification fails, does not execute an action, redirect to Specified URL

l ExternalValidationWorkflowStack is defined in the previous , which contains REFERENCE-Resolver for external references and ValidationWorkflowstack for data validation verification (WebWork2 default authentication interceptor stack)

Class = "com.opensymphony.xwork.interceptor.externalReference" />

(5) Data validity verification

l WebWork2 XWORK, using XWork's data validity verification mechanism

l Xwork's Validation Framework allows you to apply Enter Authentication Rules to Action Before executing action

l Register Validator: WW2 / Web-INF / CLASS / VALIDATORS.XML

Ø The top is some commonly used verification provided by Xwork and WebWork2, you can use it directly

Ø specifies the reference name of the validator, uses in the verification rule file; the Class property specifies the full path to the verification

Ø If the above verifier is not suitable, you can extend the verification framework of the XWork, custom verification, and register L activation verification in validators.xml:

Ø For an action that requires data validity verification, you need to add a reference to ValidationInterceptor to the configuration.

Ø The default ValidationWorkflowStack already contains references to the ValidationInterceptor, usually more is to add a ValidationWorkflowstack reference (as described in the Action Configuration in front)

l Specify the verification rule (with the same directory with the Action class): PriceIncReaseformAction-Validation.xml

Value Required.

priceIncrease.percentage> 0

value to low.

50

value too high.

l Data verification rules definition in the XML file, the file name is an actionName-value.xml, where the actionname is the corresponding Action class name or its parent class name, naming rules Similar to the resource bundle

l Defines the form field to verify; Name property specifies the form of the form

l Specifies the verification to be performed; the Type property specifies the reference to the validator, consistent with the validator specified in Validators.xml, so that the corresponding verifier is verified; the short-circuit property is optional, indicating if if Verification failure, continue the following verification L The same form field can define multiple validiar for verification, different verifiers have different parameters, such as the "FieldExpression" verifier has Expression parameters, indicating the expression to verify, and "" INT "The verifier has a MAX parameter, indicating the maximum value.

l At least one element provides a prompt information that the verification failed; the key attribute of is optional, used to specify the message as the message in the resource bundle, if not found, use Body content provided by default messages

Error.Not-Specified = Percentage Not Specified !!!

Error.Too-low = you have to specify a percentage higher Than $ {expression}%!

Error.Too-high = don't be bigy - you can't raise price by more $ {max}%!

l The message in the resource bundle can use the parameters of the validator using the form of $ {paramname}, but there is no use of other parameters, this is no Spring MVC Framework flexible

l In addition, since WebWork2 is set to set attribute values, verify, for INT type properties, if null, automatically convert to 0, so there is not much significant for the first verification (What better way? Custom verifier)

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

New Post(0)