Struts exception handling mechanism

xiaoxiao2021-03-06  72

In general, the abnormal process is added to the new version of Struts, which is called: Exception Handling, marks the framework as a whole, and Struts has become mature. Generally, in the process of developing with Struts, for abnormal processing, it is mainly manually processed: such as capturing exceptions by try / catch, then custom personalized comparison, detailed error information is placed in Actionerror Then, then feed back these error messages to the user (including the developer) in the specific return page. The abnormal original information is not wanting to see whether the end user or the developer is. The following focuses on how the abnormality is solved in Struts. The Exception Handleing in Struts is not difficult, simple and efficient is a relatively good evaluation to it. By configuring the configuration file (mainly struts-config.xml) to customize the exception processing, just like the definition formbean, there are two ways to customize the abnormality, and divide it into: "Global Abnormal" and "Local Excellence". The global abnormality, the definition method is as follows:

..........

Key = "expired.invalidItemscatalogname"

TYPE = "com.iplateau.jshop.common.waf.

Exceptions.invalidiItemsCatalogNameException "

Scope = "request"

Path = "error.jsp" />

..........

The above code defines a global exception in struts-config.xml. Its function is to throw InvaliItemscatalogNameException (this point means that when this category already exists) exceptions when it is added to Error.JSP And carry a self-specific comparison specification exception information expired.invalidItemscatalogName, Expired.INValidItemScatalogName can be found in the application's resource profile, such as expression.invalidItemscatalogName = you have to add a product category already exist, please add a new category! Local abnormality, the definition method is as follows:

..........

TYPE = "com.iplateau.jsop.action.ItemscatalogAction"

Name = "itemscatalogform">

TYPE = "com.iplateau.jshop.common.waf.

Exceptions.invalidiItemsCatalogNameException "

Path = "/ error.jsp" />

PATH = "*** Layout (here, use TILES to assist in development)" />

............

(About Tiles Content See Mr. "Struts Using Tiles Auxiliary Development") Let's put your attention to the specific action and see how struts is processed //ItemscatalogAction.java

Package com.Iplateau.jshop.action.ItemscatalogAction

Import com.iplateau.jshop.business.ItemscatalogMap;

Import com.Iplateau.jshop.action.Itemscatalogform;

IMPORT ***;

Public class itemscatalogAgction extends baseAction {

Public ActionForward Execute

ActionMapping mapping,

Actionform Form,

HTTPSERVLETREQUEST REQUEST,

Httpservletresponse response

Throws exception {

String flg = "error";

String Act = Request.getParameter ("ACT");

ItemscatalogMap Map = new itemscatalogMap ();

Itemscatalogform thisform = (itemscatalogform) form;

// itemscatalogmap is a specifically processed the class.

IF (Act.Equals ("CREATE"))

{

Map.validateNameOfCatalog (thisform);

Map.create (thisform);

FLG = "Success";

Return mapping.findforward (flg);

}

Else {

Return mapping.findforward (flg);

}

}

}

It is a simple action, in which the ACT is used to determine the operation of the action to be processed. When the ACT is CTEATE, the addition of the product category is added, and the like. Looked at the code on the upper, you can understand that you should judge the legality of the product category to be added at this time before calling the specific business class, such as: call map.validatenameOfcatalog (thisform); ValidateNameOfcatalog defined in ItemscatalogMap Methods Throw InvalidiItemscatalogNameException. Let's run an example of adding a commodity category. If there is already a "milk product" category in the database, when we have to add this category, the program turns like Error.JSP as we expected and displayed. We must first make an error message: You have to add the product category already exist, please add a new category! In fact, it is so simple, our customized exception does not need to be captured in the program, and once we have defined an exception, it will go to the corresponding page, and carry custom information. I still remember that our requests in Struts are handled through the action of Perform, but now you have to handle it through Execute, which is a very important thing for this reason is "fulfill" Exception Handling. why? Because Perform just throws IOException and servletexception when declaring, this far cannot meet the requirements of Exception Handling, then let us see what EXECUTE is: it throws Exception (all unusual parental class) Explanation only A primer, and the default Struts is processed by org.apache.struts.Action. ExceptionHandler, you can define your own processing, just inherit it and implement the Execute method, this method is defined in ExceptionHandler as follows: public ActionForward Execute

(Exception EX,

ExceptionConfig AE,

ActionMapping mapping,

Actionform FormInstance,

HTTPSERVLETREQUEST REQUEST,

Httpservletresponse response

Throws servletexception

Specific and professional analysis of the contents of Exception Handling, please see Chapter 10 of "Programming Jakarta Struts" - the electronic version of this book can be downloaded.

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

New Post(0)