Typically, we have used Struts 1.0 for development, which is mainly manually handled.
These error messages are given to the user or developers, such as capturing exceptions by try / catch, then formbean.returnstring () or debug.println ("");
In Struts 1.1, new exception handling mechanisms are introduced, allowing us to customize the exception processing by profile (mainly struts-config.xml), just like definition formbean.
Below, let's take a look at how to use this new feature specifically.
First, custom abnormalities (defined in the struts-config.xml file)
There are two ways to customize anomalies, and it is divided into: "Global Excetions" and "Local Exception".
The "Global Abnormality" definition method is as follows:
Scope = "request" Handler = "org.apache.struts.action.exceptionhandler" Path = "error.jsp" /> global-exceptions> The above code defines a global exception in struts-config.xml, and its role is to throw INVALITIUSERNAMEXCEPTION (meaning here is when the user has existed) anomaly, return to Error.jsp And carry the exception information of the self-specific comparisons struts.test.invaliDuserName, Struts.test.invaliDUserName can be found in the application's resource profile, such as: Struts.test.invaliDuserName = Repeated registered users! Local abnormality, the definition method is as follows: TYPE = "struts.test.userlogonaction" Name = "UserLogonform"> TYPE = "struts.test.invalidiusernameexception" Handler = "org.apache.struts.action.exceptionhandler" Path = "/ error.jsp" /> action> action-mappings> The definition of Struts-Config.xml in Struts 1.1 is as simple as it is. Second, achieve our exception class (Test.invalidiUserNameException) In order to implement Struts.Test.invalidiUserNameException, we must write an exception inherited in Exception: InvaliTiUserNameException. File name: invalidiusernameexception.java Package struts.test; Public Class InvaliUsernameException Extends Exception { Public invaliusernameException () { Super (); } } Third, an Action throwing anomalies We assume that this exception will throw struts.test.invalidiUserNameException: Public class userlogonaction extends baseAction { Public ActionForward Execute (ActionMapping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, HttpservletResponse response) throws exception { Throw new struts.test.invalidiusernameException (); } } Fourth, show our exception information In order to display our friendly error prompt information, we can write an error.jsp file, the content is as follows: Error.jsp <% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%> <% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%> <% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%> hEAD>
Please feed back the following prompt information to your system administrator:
body>
html: html>
5. There is an abnormality in our way.
Ok, we can see our abnormalities. When executed on the above Action, the program will throw InvaliUserNameException, and the Struts1.1 Exception Handler will capture this exception and find the matching exception definition information in struts-config.xml, and finally go to Error.jsp and Show our definition error message:
Repeated registered users!
The above code is just from the whole, how to use Struts 1.1 exception handling mechanism, in our specific implementation, we can throw an exception at the DAO / OPER layer, do not need to be thrown at the Action layer. Therefore, our customized exception does not need to use TRY / CATCH to capture in the program. Once we have defined an exception, it will go to the corresponding page, and display the corresponding customized information.
The above is handled by using the Struts default exception handle handler org.apache.struts.action. ExceptionHandler.
We can also 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
Supplementary description:
FORMBEAN.RETURnstring (): It is a friendly prompt for our own definition to display in user page.
Debug is a class that we define the display console error message
DAO / OPER: Specific database operation or other class
thank:
http://www.scioworks.net
2003-8-8