Webwork2 Tutorial (Chinese version) (4.2)

zhaozj2021-02-12  140

4.2, using Velocity in Webwork

Use Velocity as a view, there are two ways:

l Use the velocity result type to present a Velocity Template

l Sign up in Web.xml, request the Velocity template file directly to render; this method To add a servlet mapping for WebWorkVelocityServlet in Web.xml, as follows:

velocity

com.opensymphony.webwork.views.Velocity.WebWorkVelocityServlet

1

velocity

*. VM

Use the Velocity result type means that the Velocity template is rendered through action. If you access the .vm file, this file will not be rendered, but returns a text file. Therefore, the Velocity Template file should be placed in a web-inflicity to allow it to be directly accessed.

Using WebWorkVelocityServlet means rendering the Velocity template by requesting the .vm file, this may need security checks in the template.

Regardless of which method is used, all features of Velocity are valid when writing templates, and some of WebWork can be used. Here, you are familiar with Velocity, focus on the specific functionality of WebWork.

(1) Specific functions of Webwork

Webwork provides some accessible objects in Value Stack, including:

l Current HTTPSERVLETREQUEST

l Current httpservletResponse

l Current OgnwaStack

l Ognltool instance

l All properties of the current Action class

To access objects in Value Stack, you need to use Velocity reference correctly in the template:

l $ req = httpservletRequest

L $ res = httpservletResponse

L $ stack = ignlvaluestack

L $ OGNL = Ognltool

l $ name-of-protety = Properties of the current Action class

(2) Type of Velocity results

The following example is the use of the Velocity template as a result, to implement the previous Hello example, Note tag is replaced by the $ Person reference:

Xwork.xml:

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

EX01-index.jsp

EX01-surcess.vm

HelloAction.java: (as in the example)

EX01-index.jsp: (with the example in the example)

EX01-Success.VM:

Webwork Tutorial - Lesson 4.2 - Example 1 </ Title></p> <p></ hEAD></p> <p><body></p> <p>Hello, $ Person</p> <p></ body></p> <p></ html></p> <p>(3) Using WebWork tags in Velocity</p> <p>Use the Velocity template to replace the JSP tag, will lose the ability to use JSP tags. However, Webwork's Velocity Servlet provides a method for using JSP tags in a Velocity template: using # tag, # bodytag and #param velocimaos. Below is a general syntax:</p> <p>#tag (Name-of-tag list-of-attributes)</p> <p>or:</p> <p>#bodytag (Name-of-tag List-of-attributes)</p> <p>#param (key value)</p> <p>#param (key value)</p> <p>...</p> <p>#end</p> <p>The following example uses the Velocity implementation of the Demonstration UI tag usage in Section 4.1.1:</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). -> <include file = "Webwork-default.xml" /></p> <p><! - configuration for the default package. -></p> <p><package name = "default" extends = "Webwork-default"></p> <p><! - Default Interceptor Stack. -></p> <p><default-interceptor-ref name = "defaultstack" /></p> <p><! - Actions: Lesson 4.2: FormProcessing Downloaded Velocity. -></p> <p><action name = "formProcessingVelocityIndex" class = "lesson04_02.formProcessing"></p> <p><result name = "success" type = "velocity"> EX02-index.vm </ result></p> <p></ action></p> <p><action name = "formssingvelocity" class = "lesson04_01_01.formprocessing"></p> <p><result name = "input" type = "velocity"> EX02-index.vm </ result></p> <p><result name = "success" type = "velocity"> eX02-surcess.vm </ result></p> <p><Interceptor-Ref Name = "ValidationWorkflowstack" /></p> <p></ action></p> <p></ package></p> <p></ xwork></p> <p>EX02-INDEX.VM:</p> <p><html></p> <p><HEAD></p> <p><Title> Webwork Tutorial - Lesson 4.2 - Example 2 </ Title></p> <p><style type = "text / css"></p> <p>{color: red;</p> <p></ style></p> <p></ hEAD></p> <p><body></p> <p><p> ui form tags example usufactage using velocity: </ p></p> <p>#bodytag (form "action = 'formProcessingVelocity.Action'" "Method = 'POST'")</p> <p>#tag (CheckBox "name = 'checkbox'" "Label = 'a checkbox'" "FieldValue = 'Checkbox_Value')</p> <p>#tag (file "name = 'file'" "label = 'a file field') # Tag (hidden" name = 'hidden' "" value = 'hidden_value')</p> <p>#TAG (label "label = 'a label')</p> <p>#tag (Password "name = 'password'" "label = 'a password field')</p> <p>#tag (Radio "Name = 'Radio'" "" LABEL = 'Radio Buttons' "" List = {'One', 'Two', 'Three'} ")</p> <p>#tag (select "name = 'select'" "" "LABEL = 'a select list'" "" list = {'one', 'two', 'three'} "</p> <p>"EMPTYOPTION = True")</p> <p>#tag (Textarea "Name = 'Textarea'" "Label = 'A Text Area'" "Rows = '3'" "COLS = '40 '")</p> <p>#tag (textfield "name = 'textfield'" "Label = 'a text field')</p> <p>#tag (Submit "Value = 'Send Form'))</p> <p>#end</p> <p></ body></p> <p></ html></p> <p>EX02-Success.VM:</p> <p><html></p> <p><HEAD></p> <p><Title> Webwork Tutorial Lesson 4.2 - Example 2 </ Title></p> <p></ hEAD></p> <p><body></p> <p><p> ui form tags example result sale using velocity: </ p></p> <p><ul></p> <p><li> Checkbox: $! CheckBox </ li></p> <p><li> file: $! file </ li></p> <p><li> hidden: $! hidden </ li></p> <p><li> Password: $! Password </ li></p> <p><li> Radio: $! radio </ li></p> <p><li> SELECT: $! SELECT </ li></p> <p><li> Textarea: $! textarea </ li></p> <p><li> Textfield: $! textfield </ li></p> <p></ ul></p> <p></ body></p> <p></ html></p> <p>FormProcessingAction.java: (Examples of Section 4.1.1)</p> <p>FormProcessingAction-Validation.xml: (Example of 4.1.1) The following example uses Velocity implementation of custom components in Section 4.1.1, pay attention to #PARAM usage:</p> <p>EX03.VM:</p> <p><html></p> <p><HEAD></p> <p><Title> Webwork Tutorial - Lesson 4.2 - Example 3 </ Title></p> <p></ hEAD></p> <p><body></p> <p><p> Custom Component Example: </ P></p> <p><p></p> <p>#bodytag (Component "Template = / files / templates / components / datefield.vm")</p> <p>#param ("Label" "Date")</p> <p>#param ("name" "mydatefield")</p> <p>#param ("Size" "3")</p> <p>#end</p> <p></ p></p> <p></ body></p> <p></ html></p> <p>/files/templates/components/datefield.vm: (Examples of 4.1.1)</p> <p>4.3, use FreeMaker in Webwork</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-6428.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="6428" 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 = '5WFrmzlVWg_2BGb8id2g4ZeWvrlWIi6ECKoyJZyy7_2BcDoTfV2OuNyCMGD1SibLqNhtffUWfBflWFHb9D3f0zDQ2w_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>