Webwork2 tutorial (Chinese version) (3)

zhaozj2021-02-16  82

3, Actions and Results

Actions is the basic execution unit that is registered in the WebWork configuration to respond to a specific request. In the MVC, Actions is the control section. Below is the basic steps for creating an action in the WebWork:

l Create a JSP page for calling an action;

l Create an Action class;

l Create a JSP page for processing an action returns a result;

l Register an action in xwork.xml;

(1) Example of Hello WebWorld

l xwork.xml file content is as follows:

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

eX01-surcess.jsp

The configuration file tells webwork, there is an action called HelloWebWorld, implemented by the Lesson03.HellowebWorldAction; at the same time defining a result of success, pointing to the EX01-SUCCESS.JSP page;

l Call the page of the action EX01-INDEX.JSP:

Webwork Tutorial - Lesson 3 - Example 1 </ Title></p> <p></ hEAD></p> <p><body></p> <p><p> Click The Button Below To Activate HelloWorldAction. </ P></p> <p><form action = "hellowebworld.action" method = "post"></p> <p><p> <input type = "submit" value = "Hello!" /> </ p></p> <p></ form> </ body></p> <p></ html></p> <p>When you click on the button button, the browser submits form data to hellowebworld.action; since the URL match map * .Action, servlet container activates WebWork's servletdispatcher; servletdispatcher reads xwork.xml, look for an action named hellowebworld, if you find it A new instance of the Action class, calling the execute () method</p> <p>l Action class: HelloWebWorldAction.java</p> <p>Package lesson03;</p> <p>Import com.opensymphony.xwork.actionsupport;</p> <p>Public class hellowebWorldAction Extends ActionSupport {</p> <p>String hello;</p> <p>Public string getHello () {</p> <p>Return hello;</p> <p>}</p> <p>Public String Execute () throws exception {</p> <p>Hello = "Hello, WebWorld!";</p> <p>RETURN SUCCESS;</p> <p>}</p> <p>}</p> <p>The Action class inherits com.Opensymphony.xWork.ActionSupport, and implements the execute () method; the return value of the execute () method Success and SUCCESS (<result> Name property value) corresponds to the result of the name match, transfer to the specified JSP page EX01-SUCCESS.JSP</p> <p>l Results show JSP page EX01-Success.jsp:</p> <p><% @ Taglib Uri = "Webwork" prefix = "ww"%></p> <p><html></p> <p><HEAD></p> <p><Title> Webwork Tutorial - Lesson 3 - Example 1 </ Title></p> <p></ hEAD></p> <p><body></p> <p><ww: protoyal value = "hello" /></p> <p></ body></p> <p></ html></p> <p><ww: protoyal value = "hello" /> Find the Hello property in the Action class, call the setter method of the Hello property to get the attribute value (in Execute () already set it), show hello, webworld!</p> <p>(2) Example of providing data to ACTION</p> <p>Xwork.xml:</p> <p><! Doctype xwork public "- // opensymphony group // xwork 1.0 // en"</p> <p>"http://www.opensymphony.com/xwork/xwork-1.0.dtd"></p> <p><xwork></p> <p><! - INCLUDE WebWork Defaults (from Webwork-2.1 ​​Jar). -></p> <p><include file = "Webwork-default.xml" /></p> <p><! - configuration for the default package. -></p> <p><package name = "default" extends = "Webwork-default"> <! - default interceptor stack. -></p> <p><default-interceptor-ref name = "defaultstack" /></p> <p><! - ACTION: Lesson 03: HelloAction. -></p> <p><action name = "hello" class = "lesson03.helloaction"></p> <p><result name = "error" type = "dispatcher"> EX02-index.jsp </ result></p> <p><result name = "success" type = "dispatcher"> eX02-surcess.jsp </ result></p> <p></ action></p> <p></ package></p> <p></ xwork></p> <p>HelloAction.java:</p> <p>Package lesson03;</p> <p>Import com.opensymphony.xwork.actionsupport;</p> <p>Public class helloaction extends actionsupport {</p> <p>String Person;</p> <p>Public string getPerson () {</p> <p>Return Person;</p> <p>}</p> <p>Public void setperson (String Person) {</p> <p>THIS.PERSON = Person;</p> <p>}</p> <p>Public String Execute () throws exception {</p> <p>IF ((Person == Null) || (Person.Length () == 0)) Return Error;</p> <p>Else Return Success;</p> <p>}</p> <p>}</p> <p>EX02-index.jsp:</p> <p><html></p> <p><HEAD></p> <p><Title> Webwork Tutorial - Lesson 3 - Example 2 </ Title></p> <p></ hEAD></p> <p><body></p> <p><p> what's your name? </ p></p> <p><form action = "hello.action" method = "post"></p> <p><p> <infut type = "text" name = "person" /> <input type = "submit" /> </ p></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>EX02-Success.jsp:</p> <p><% @ Taglib Uri = "Webwork" prefix = "ww"%></p> <p><html></p> <p><HEAD></p> <p><Title> Webwork Tutorial - Lesson 3 - Example 2 </ Title></p> <p></ hEAD></p> <p><body></p> <p>Hello, <ww: protoyal value = "person" /></p> <p></ body></p> <p></ html></p> <p>This example uses the POST method to send form data (using the Person name) to the action, after the HelloAction instance, servletdispatcher calls the setter method to set the corresponding attribute of the Action; therefore, before executing execute (), the Person property has already already setup</p> <p>(3) RESULT type</p> <p>The result type used in the above example is Dispatcher, and the WebWork's Result Type is configured in WebWork-Default.xml:</p> <p>l dispatcher (com.opensymphony.webwork.dispatcher.servletdispatcherResult): Forwards result to the specified location URL</p> <p>l Redirect (com.opensymphony.webwork.dispatcher.servletredirectRectResult): redirects result to the specified location URL; Different from Dispatcher Will not send form data</p> <p>l velocity (com.opensymphony.webwork.dispatcher.velocityResult: Using the Velocity template as a result, you need to configure VelocityServlet in web.xml, this is a good method</p> <p>l Chain (com.opensymphony.xwork.actionchainResult): Action chain, transfer the result to another action</p> <p>l xslt (com.opensymphony.webwork.views.xslt.xsltResult): Formatting results using XSLT style</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-13753.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="13753" 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 = 'BdCggZU08qm0aRr7T1ClwrhDsABAwd8syyhMNFJjUT3UKlLX3evPMY4D4KcnXdYFZbuJifeOKVeY2vRpJbU0tQ_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>