Webwork2 tutorial (Chinese version) (5) (End)

zhaozj2021-02-12  163

5, Interceptors

Interceptors allows code that is executed after the calling stack is included before and / or processing before processing. This is your code simple, more reuse. Most of XWork and WebWork is implemented in Interceptors. You can apply your own interceptors to the specified action by external configuration, according to your defined order. Apply your own Interceptors.

When you visit .Artion URL, WebWork's servletdispatcher boots an action object, starting to be interrupted by other objects before Action is executed, which is called Interceptor. Execute Interceptor before the specified action (or later), as long as the properties are configured in xwork.xml. Here are an Interceptor configuration that shows an example of the UI tag usage in Section 4.1.1:

eX01-index.jsp

eX01-surcess.jsp

FormProcessingAction uses ValidationWorkflowstack. This is an interceptor heap that organizes a set of interceptors in order. ValidationWorkflowStack is configured in WebWork-Default.xml, so we just use in the action configuration, or use to use it in the package configuration. Below is an Interceptor configuration of the HelloWebWorld example:

"http://www.opensymphony.com/xwork/xwork-1.0.dtd">

eX01-surcess.jsp

Look at how Interceptor works

l Create an interceptor class, you need to extend the com.OpenSymphony.xWork.Interceptor.Interceptor interface (included in xwork-1.0.jar);

l In the xwork.xml file, use the embedded embedded in the ;

l Use to create an interceptor heap (optional);

l Which anterceptor is used by or ; the former is used by the specified action, the latter uses all Action

(1) Webwork-default.xml

Let's take a look at the contents of web-default.xml:

"http://www.opensymphony.com/xwork/xwork-1.0.dtd">

Class = "com.opensymphony.webwork.dispatcher.servletdispatcherResult" />

Class = "com.opensymphony.webwork.dispatcher.servletredirectResult" />

Class = "com.opensymphony.webwork.dispatcher.velocityResult" />

Class = "com.opensymphony.xwork.actionchainResult" />

Class = "com.opensymphony.webwork.views.xslt.xsltResult" />

Class = "com.opensymphony.xwork.interceptor.timinterceptor" />

Class = "com.opensymphony.xwork.interceptor.logginginterceptor" />

Class = "com.opensymphony.xwork.interceptor" />

Class = "com.opensymphony.xwork.interceptor.StaticParametersInterceptor" />

Class = "com.opensymphony.xwork.interceptor" />

Class = "com.opensymphony.xwork.interceptor.modeldriveninterceptor" />

Class = "com.opensymphony.xwork.interceptor.component.componentinterceptor" />

Class = "com.opensymphony.webwork.interceptor.tokeninterceptor" />

Class = "com.opensymphony.webwork.interceptor.tokenssionStoreInterceptor" />

Class = "com.opensymphony.xwork.validator.validationinterceptor" />

Class = "com.opensymphony.xwork.interceptor.defaultWorkflowInterceptor" />

Class = "com.opensymphony.webwork.interceptor.servletconfiginterceptor" />

Class = "com.opensymphony.xwork.interceptor.prepareinterceptor" />

Class = "com.opensymphony.webwork.interceptor.webworkconversionerrorinterceptor" />

Since WebWork-Default.xml is included in our xwork.xml, we can use these Interceptor or Interceptor piles in Action. Below is what these Interceptors do:

l Timer: Perform timing (including Nested Interceptor and view)

l Chain: Make the properties of the previous Action valid for the current action, usually create an Action chain

l Static-Params: Setting parameters in xwork.xml to action ( embedded in )

l Params: Settings the request parameter (POST or GET) to the action

l Model-Driven: If the action realizes ModelDriven, push getModel () results into the Value Stack

l Component: Enable and register components to make it valid for Action

l Token: Check the effective token in action to prevent repeated submission

l Token-session: Same as above, but saves submitted data in the session when processed to invalid token

l Validation: Use the validator defined in {Action} -vailDation.xml for data verification

l Workflow: Call the validate () method in the Action class, return to the INPUT view when an error occurs; should be used with the Validation Interceptor

l Servlet-Config: Access to HTTPSERVLETREQUEST and HTTPSERVLETRESPONSE (Because Bind to Servlet API, it is best not to use)

l prepare

l conversionerror

(2) Creating your own interceptor If the above Interceptor is not suitable, you can create your own interceptor. The following example assumes that we need an interceptor to place a welcome information based on the day of the SESSION:

GreetingInterceptor.java:

Package lesson05;

Import java.util.calendar;

Import com.opensymphony.xwork.interceptor.interceptor;

Import com.opensymphony.xwork.actioninvocation;

Public class greetinginterceptor imports interceptor {

Public void init () {}

Public void destroy () {}

Public String Intercept (ActionInvocation Invocation) throws exception {

Calendar Calendar = Calendar.getInstance ();

INT Hour = Calendar.get (Calendar.Hour_Of_DAY);

String Greeting = (Hour <6)? "Good evening":

((Hour <12)? "Good Morning":

((Hour <18)? "good insternoon": "good evening"));

Invocation.getinvocationContext (). GetSession (). PUT ("Greeting", Greeting;

String result = invocation.invoke ();

Return Result;

}

}

Xwork.xml:

"http://www.opensymphony.com/xwork/xwork-1.0.dtd">

eX01-result.vm

GreetingAction.java:

Package lesson05;

Import com.opensymphony.xwork.actionsupport;

Public class greetingaction extends actionSupport {

Public String Execute () throws exception {

RETURN SUCCESS;

}

}

EX01-Result.vm:

Webwork Tutorial - Lesson 5 - Example 1 </ Title></p> <p></ hEAD></p> <p><body></p> <p>#set ($ SES = $ Req.GetSession ())</p> <p><p> <b> $ {ses.getattribute ('Greeting')}! </ b> </ p></p> <p></ body></p> <p></ html></p> <p>Interceptor class To extend the com.opensymphony.xwork.interceptor.interceptor interface: init () calls when Interceptor initialization; Destroy () calls when destroying; Intercept (ActionInvocation Invocation) is the center of processing.</p> <p>Invocation.Invoke () is used to call the next interceptor in the interceptor pile, or Action (if not, if not). Therefore, we can completely bypass the Action and return the result (do not perform an action).</p> <p>The above example is called before the action is executed. If you want to call Interceptor after the Action is executed, simply put the execution code in Invocation.invoke ().</p> <p>Webwork provides an abstract class com.opensymphony.xWork.Interceptor.AroundInterceptor that implements this method, you can implement its Before (ActionInvocation Invocation) and After (ActionInvocation Dispatcher, String Result).</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-6422.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="6422" 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.036</b>, SQL: <b>12</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 = 'tIzgPR_2FYthBBDbqdORLVXMcbOmWuLw1oHFggRDrYEhzVoKr6m5MIAjkCfdLzCHP67pmQPF_2Fc2pnFzqDGV0dUjQ_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>