Struts exception handling mechanism
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:
......
......
Scope = "request" Path = "error.jsp" /> global-exceptions> ...... ...... 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 carrying the exception information of the self-specific comparison specification EXPIRED.INVALIDITEMSCATALOGNAME, Expired.INValidItemscatalogName can be found in the application's resource profile, such as: Expired.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" /> ation> action-mappings> ...... ...... (About Tiles "Another article" Struts use tiles auxiliary development ") Let's put your attention to the specific action, see how the strut is abnormal ItemscatalogAgction.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 side, you can understand that you should judge the legitimacy of the product category to add by calling the specific business class. Call Map.ValidateNameOfCatalog (thisForm); This is thrown in the validatenameofcatalog method defined in ItemscatalogMap. The InvaliItemsCatalogNameException is thrown. 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, let's see what Execute is: it throws Exception (all unusual parental class) The above description is just 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 in ExceptionHandler Definition 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.