About Struts 1.1 Exception Handler Application Supplements

zhaozj2021-02-17  55

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" />

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" />

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"%>

struts 1.1 Exception Handler Demo </ Title></p> <p><HTML: BASE /></p> <p></ hEAD></p> <p><body></p> <p><H1> The system has an accident </ h1></p> <p>Please feed back the following prompt information to your system administrator: <br></p> <p><html: errors /> <! - will be displayed here "Repeated Registered User!" -></p> <p></ body></p> <p></ html: html></p> <p>5. There is an abnormality in our way.</p> <p>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:</p> <p>Repeated registered users!</p> <p>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.</p> <p>The above is handled by using the Struts default exception handle handler org.apache.struts.action. ExceptionHandler.</p> <p>We can also define your own processing, just inherit it and implement the Execute method, this method is defined in ExceptionHandler as follows:</p> <p>Public ActionForward Execute (Exception EX,</p> <p>ExceptionConfig AE,</p> <p>ActionMapping mapping,</p> <p>Actionform FormInstance,</p> <p>HTTPSERVLETREQUEST REQUEST,</p> <p>Httpservletresponse response</p> <p>Throws servletexception</p> <p>Supplementary description:</p> <p>FORMBEAN.RETURnstring (): It is a friendly prompt for our own definition to display in user page.</p> <p>Debug is a class that we define the display console error message</p> <p>DAO / OPER: Specific database operation or other class</p> <p>thank:</p> <p>http://www.scioworks.net</p> <p>2003-8-8</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-30645.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="30645" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.038</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'qm6iuHNpXc4TV72Xa_2FSbANvmYFond6WYkM_2BvHoK0adRyEErxpwJs1gX8_2FOt2sfXOK1u6vs_2ByFO5QZuNQD_2BJ2_2BA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>