Struts exception handling mechanism

xiaoxiao2021-03-06  75

JPLATEAU has been added to the new version of the Struts, called: Exception Handling, marking as a holistic solution, Struts is proofful. 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".

Global exception, is defined as follows: ............ ......... The above code defines a global exception in struts-config.xml, its role is to throw INVALIDIITEMSCATALOGNAMEEXCEPTION (the meaning of this is when adding product classification) When this category already exists), return to Error.JSP, and carry the exception information of the custom comparison specification EXPIRED.INVALIDITEMSCATALOGNAME, Expired.INValidItemScatalogName can be found in the application's resource profile, such as: Expired.invalidItemscatalogName = You have to add the product category already exist, please add a new category! The local exception, the definition method is as follows: ......... .......... (About Tiles) Another article" Struts use tiles auxiliary development ") Let's put your attention to the specific action see how struts is exception handling //ItemsCatalogAction.javapackage com.iplateau.jshop.action.ItemsCatalogActionimport com.iplateau.jshop.business.ItemsCatalogMap; import com.iplateau.jshop.action.ItemsCatalogForm; import ***;

public class ItemsCatalogAction 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 specific processing product category class IF (Act.Equals ("create") {map.validatenameofcatalog (thisform); map.create (thisform); flg = " "return mapping.findforward (flg);} else {return mapping.findforward (flg);}}} is a simple action, where the specific content of ACT is used to determine the operation of this Action to process, If the ACT is CTEATE, the addition of commodity categories, etc. 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, see "Programming Jakarta Struts" Chapter 10 - this e-book Many places on the edition can be downloaded. Copyright Notice

Is this article helpful? Vote:

Yes

No Vote:

2

1

Discussant: Restart2001 Participation: 12 Experts: 0 Published: 2003-9-8 11:12 AM I feel this, it is better to catch the exception thrown by DAO in Action, then put it in the actionerror,

Discussant: Restart2001 Participation: 12 Expert points: 0 Published: 2003-9-8 11:15 AM 11:15 Add people data IF (EmployeedAo.Retrieve (Path, Employeeid) == null) {Employeedao.insert (Employee, LoginworkID) } Else {sqlduplicateError (Request, "ErroS.DataExist"); return mapping.findforward ("failure");} Put the error prompt defined in the resource file in the error message via the SqlDuplicateError method to go to the error page, Error.JSP page Define in the struts-congfig.xml file. .

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

New Post(0)