Thirteen, JSP action

zhaozj2021-02-16  151

JSP Actions use the XML syntax format tag to control the behavior of the Servlet Engine. Using JSP actions can be dynamically inserted into files, reuse the JavaBean components, redirect the user to another page, generate HTML code for the Java plugin. JSP actions include: JSP: include: Introducing a file when the page is requested. JSP: Usebean: Find or instantiate a JavaBean. JSP: setProperty: Sets the properties of JavaBean. JSP: getProperty: Outputs the properties of a JavaBean. JSP: Forward: Turn the request to a new page. JSP: Plugin: Generate Object or Embed tags based on the browser type. 13.1 JSP: Include Action This action Inserts the specified file into the page being generated. Its syntax is as follows: I have already introduced the include directive, which is introduced when the JSP file is converted to servlet, and the JSP: include the number of thections here is different. The time inserted into the file is when the page is requested. JSP: The file introduction time of the incrude action determines its efficiency to be slightly worse, and the reference file cannot contain some JSP code (for example, the HTTP header can not be set), but its flexibility is much better. For example, the following JSP page plugs 4 news summary into a "what's new?" Page.

When you change the news summary, you only need to change these four files, and the primary JSP page can not be modified: whatsnew.jsp what's new </ title> </ head> # fdf5e6 "text =" # 000000 "link =" # 0000e "VLINK =" # 551a8b "alink =" # ff0000 "> <center> <Table border = 5 bgcolor = "# EF8429"> <TR> <TH class = "title"> What's new at jspnews.com </ table> </ center> <p> here is a summary of oes Most Recent News Stories: <ip> <li> <jsp: include page = "news / item1.html" Flush = "true" /> <li> <jsp: include page = "news / item2.html" flush = "true" / > <Li> <jsp: include page = "news / item3.html" Flush = "true" /> <li> <jsp: include page = "news / item4.html" flush = "true" /> </ ol > </ Body> </ html> 13.2 JSP: UseBean action JSP: UseBean action to load a JavaBean that will be used in the JSP page. This feature is very useful because it allows us to play both the Java component reuse, and avoid the loss of JSP distinguish between servlets. JSP: The simple syntax of UseBean action is: <JSP: Usebean id = "name" class = "package.class" /> Create an instance of the class specified by the Class property, then put it Binds to its name by the number of variables given by the id attribute. " However, just as you will see, define a scope property allows the bean to associate more pages. At this point, JSP: UseBean action only creates a new object instance without the same ID and Scope, and it is necessary to get a reference to the existing bean.</p> <p>After getting a bean instance, the attribute to modify the bean can be performed by JSP: SetProperty action, or the object variable named ID attribute can be used in the Scriptlet, and the properties are explicitly modified by calling the object. This makes us remember that when we say "a bean has a type X" foo ", it means" this class has a getfoo method with a return value type X, and there is a setfoo method to X-type value Parametric ". Details of the JSP: SetProperty action are discussed later. But now you must know, we can provide one value directly through the value attribute of JSP: setProperty action, or by declare the property value from the specified request parameter through the PARAM property, you can also list the BEAN property indicating that it should be The same name variable from the request parameter. Reading the bean property in the JSP expression or Scriptlet is implemented by calling the corresponding getxxx method, or more generally, using the JSP: getProperty action. Note that class files containing beans should be placed in the server formally stored in the Directory of the Java class, rather than retaining the directory of the class that can be loaded automatically after the modification. For example, for Java Web Server, the classes used by Beans and all beans should be placed in the classs directory, or when the package into the JAR file, put the lib directory, but should not put it under servlets. Here is a very simple example, its function is to load a bean, then set / read its Message property. Beantest.jsp <! Doctype html public "- // w3c // DTD HTML 4.0 Transitional // en"> <html> <head> <title> Reusing JavaBeans in jsp </ title> </ head> <body> <center > <Table Border = 5> <TR> <TH Class = "Title"> Reusing JavaBeans in JSP </ Table> </ center> <p> <jsp: usebean id = "test" class = "Hall.SIMPLEBean" / > <Jsp: setproperty name = "test" property = "message" value = "Hello WWW" /> <h1> Message: <i> <jsp: getProperty name = "test" property = "message" /> </ i > </ H1> </ body> </ html> SimpleBean.java beantest page is used in SimpleBean.</p> <p>SimpleBean code is as follows: package hall; public class SimpleBean {private String message = "No message specified"; public String getMessage () {return (message);} public void setMessage (String message) {this.message = message;}} 13.3 About JSP: Further Description of UseBean Use the easiest way to use Bean to load bean: <jsp: usebean id = "name" class = "package.class" /> then by JSP: SetProperty and JSP: GetProperty Modify and extract the properties of the bean. However, there must be two points. First, we can also instantiate bean: <JSP: usebean ...> body <jsp: usebean>> body </ jsp: usebean>, only when the first instantiation bean is performed, if It is not to execute the Body section using the existing Bean instance. As will be introduced below, JSP: UseBean does not always mean create a new bean instance. Second, in addition to ID and CLASS, JSP: USEBean has three other properties, namely: scope, type, beanname. The following table briefly explains the usage of these attributes. Attribute Usage ID Names the variable that references the bean. If you can find the identical bean instance of ID and Scope, JSP: UseBean action will use an existing bean instance instead of creating a new instance. Class Specifies the full package name of the bean. Scope Specifies which of which is available in the context, one of the four values ​​below: Page, Request, Session, and Application. The default value is Page, indicating that the bean is only available within the current page (saved within the current page). Request means that the bean is valid within the current customer request (saved within the servletRequest object). Session indicates that the bean is valid for all pages in the current HTTPSession. Finally, if application is valued, the bean is valid for all pages with the same servletContext. Scope is important because JSP: UseBean instantizes new objects only when there is no object with the same ID and Scope; if the ID and Scope are the same object, use existing objects directly, this Anything between the JSP: USEBean starts tag and end tags will be ignored. TYPE specifies the type of variable that references the object, it must be the name of the bean class, a super class name, one of the interface names implemented by this class. Remember that the name of the variable is specified by the id attribute. BeanName Specifies the name of the bean. If the Type property and the beanName property are provided, the Class property is allowed to omit. 13.4 JSP: SETPROPERTY Action JSP: SetProperty is used to set the properties of the instantiated Bean object, there are two usage.</p> <p>First, you can use JSP: setProperty outside the JSP: UseBean element, as shown below: <JSP: usebean id = "myname" ... /> ... ...... "Property = "SomeProperty" ... /> At this time, regardless of the JSP: UseBean finds an existing bean or a new BEAN instance, JSP: setProperty will execute. The second usage is to put JSP: setProperty into the JSP: UseBean element, as shown below: <jsp: usebean id = "myname" ...> ... <jsp: setProperty name = "MyName" Property = " SomeProperty "... /> </ jsp: usebean> This JSP: SetProperty will only be executed when a new bean instance is created, and JSP: setProperty is not performed if you use an existing instance. JSP: setProperty action has the following four properties: Property Description Name Name property is required. It means which bean is to set the property. The Property property is required. It indicates which property to set. There is a special usage: if the value of the Property is "*", the request parameters that match all the names and bean property names will be passed to the corresponding attribute set method. The Value Value property is optional. This property is used to specify the value of the bean property. String data automatically converts into numbers through standard Valueof methods in the target class, Boolean, Boolean, Byte, Byte, Char, Character. For example, the property value of the Boolean and Boolean types (such as "True") converted through the attribute value of the Boolean.ValueOf conversion, the INT, and Integer types (such as "42") via Integer.Valueof. Value and Param cannot be used at the same time, but can be used any one. Param Param is optional. It specifies which request parameter as the value of the bean property. If there is no parameters of the current request, what is not done, the system does not pass NULL to the set method of the bean property. Therefore, you can let the bean provide the default attribute value, and only modify the default attribute value when the request parameter is clearly specified. For example, the following code segment indicates that if there is a NumItems request parameter, set the value of the NumberOfItems property to the value of the request parameter NumItems; otherwise, nothing. <JSP: setProperty Name = "ORDERBEAN" Property = "NumberofItems" param = "numItems" /> If Value and Param are omitted, its effect is equivalent to providing a param and its value is equal to the value of Property. Further, this kind of thought of automatic assignment of automatic assignment by using the request parameter and attribute name, you can also specify "*" in the Property (name of the bean property), then omit value and param.</p> <p>At this point, the server will view all bean properties and request parameters, if the names are the same, automatically assign. Below is an example of using JavaBean calculations. If there is a Numdigits parameter in the request, the value is passed to the Numdigits property of the bean; Numprimes is similar. JSPprimes.jsp <! Doctype html public "- // w3c // DTD HTML 4.0 Transitional // en"> <html> <head> <title> Using JavaBean </ Title> </ head> <body> </ head> <body> < Center> <Table Border = 5> <TR> <TH Class = "Title"> Using JAVABean </ Table> </ center> <p> <jsp: usebean id = "prime porttedprimes in JSP =" prime porttedprimes) "/> <jsp: setProperty name =" primeTable "property =" numDigits "/> <jsp: setProperty name =" primeTable "property =" numPrimes "/> Some <jsp: getProperty name =" primeTable "property =" numDigits " /> Digit Primes: <JSP: getProperty Name = "Primetable" Property = "NUMBEREDLIST" /> </ body> </ html> Note: NumberedPrimes code is slightly. 13.5 JSP: GetProperty action JSP: getProperty action extracts the value of the specified bean property, converts into a string, and then output. JSP: getProperty has two essential properties, namely: name, represents the name of the bean; Property, indicates which attribute to extract. Here is an example, more examples can be found in the forebel.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-8974.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="8974" 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.041</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 = 'qrbKZeeIdKyr9_2FAMbBUv1hyOlJGAtpKFftzEElXqfCccCjzEIp9OqBCF24mz3pvLDl_2BzKgid8yaZOYtK'; 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>