JSP tutorial (5) -Jsp Action

zhaozj2021-02-16  54

JSP: Userization of UseBean Action First, Syntax: |> Other elements } Action allows you to load a JavaBean into a JSP page. This is a very useful ability because it allows you to use reusable Java classes without sacrificing performance. The simplest syntax is used to specify a bean: This usually means "instantification of a class object by specifying a class, and will pass ID specifies the variable binding of the name. However, just as we see, you can specify a scope property to make the bean not only contact the current page. In this case, it is very useful to get a reference to the existing bean, and only one new thing is created only when there is no the same ID and Scope beans. Now, you already have a bean, you can modify it via JSP: setProperty, or use the scriptlet or a clear call method by using the name specified by ID. When you say "this bean has a" X-type property called Foo "," You really means "this class has a method called getfoo, it returns a certain value of the X type, and another method is called Setfoo, it is parameter in x. "This JSP: setProperty action will introduce in detail in the next unit, but now you can give a clear value, give a property to indicate that this value is inherited from the parameter from the request. It is also possible to simplify the attribute to mark this value from inheritance from the parameter as the attribute name. You can get existing JSP expressions or scripTlet properties by calling applicable getxxx methods, or more common, using JSP: getProperty Action. Note that the class specified for the bean must be under the classpath of the server's rules, rather than reserved the path of the class that is automatically loaded when changing. For example, on the Java Web Server, it must go to the class directory or in a JAR file in the lib directory, instead of in the directory of servlets. Let us look at a very simple example that loads a bean and sets / get a simple string parameter.

Beantest.jsp 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> SimpleBean.java The following is the original code of Bean: package Hall ; public class SimpleBean {private String message = "No message specified"; public String getMessage () {return (message);} public void setMessage (String message) {this.message = message;}} run results: output page: Reusing Javabeans in JSP B> Message: Hello WWW II, JSP: User's Details Detailed Usage The simplest way is: <JSP: usebean id = "name" class = "package.class" /> To load bean, need Use JSP: SetProperty and JSP: getProperty to modify and retrieve the properties of the bean. Moreover, there are two other options. First, you can use the container's format, that is, <JSP: usebean ...> body </ jsp: usebean> To point out, the Body section should be performed only when Bean is first instantiated, not Every time you are found and used. Beans can be shared, so not all JSP: Usebean statements produce a new bean instance. Second, in addition to ID or Class, there are three properties you can use: scope, type, and beanname. These properties summarize as follows: The property usage ID is named gives a variable, which will point to bean. If there is a bean having the same ID and Scope, it is not new. Class pointed out the full package of Bean. Scope indicates that the Bean can be used before and after it. There are four possible values: Page, Request, Session, and Application. The default is Page, indicating that the bean is available only in current pages (saved in the current PageContext). A value of Request indicates that the bean is only for the current client request (saved in the servletRequest object).</p> <p>The value of the session pointed out that the object is available for all pages in the current HTTPSession life cycle. Finally, the value of Application pointers that the object can be used on all the shared servletsContext. Use JSP: UseBean creates a new bean only when there is no Same id and scope, if you already use it, and ignore the code that starts and ends with JSP: UseBean flag. TYPE indicates the type of variable that will point to the object. This must match the class name or a superclass or an interface implementation class. Remember, the name of the variable is specified by the id attribute. BeanName gives a name, you should be available in the instantiation method of Beans. It allows you to give Type and a beanName and omitted class properties. Three, jsp: setProperty Action Syntax: <jsp: setPropertyname = "beanInstanceName" {property = "*" | property = "propertyName" [param = "parameterName"] | property = "propertyName" value = "{string | <% = Expression%>} "} /> We know that you can use JSP: setProperty to assign a value for a bean property. You can use two ways to implement it. First, in JSP: Usebean (instead of), use JSP: setProperty: <jsp: usebean id = "myname" ... /> ... "jSP:SetProperty name =" = "Property =" SomeProperty "... /> In this manner, JSP: setProperty will be executed whether there is a bean with the same ID and Scope. Alternatively, JSP: setProperty appears within JSP: UseBean Elements, such as: <JSP: UseBean ID = "MyName" ...> ... <JSP: setProperty Name = "MyName" property = "someproperty". .. /> </ jsp: UseBean> In this case, JSP: setProperty is only performed when the new object is instantiated. The following is available in four JSP: SETPROPERTY: Property Usage Name This is a must-select property. It pointed out which bean's attribute will be set. JSP: Usebean must appear before JSP: setProperty. Property This is a must-select attribute. Indicates which property you will set. However, there is a special case: if it is worth "*", all names match the request parameters that match the properties of the bean will be passed to the corresponding attribute setting method. Value This is an optional attribute. It specifies the value of the properties set. The value of the string will be automatically converted to Numbers, Boolean, Boolean, Byte, Byte, Char, and Character by the standard Valueof method of the corresponding object or package. For example, the value of the Boolean or Boolean property will transform through the boolean.valueof method, and the value "42" of an int or Integer property will be converted via Integer.Valueof.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-20425.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="20425" 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.050</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 = 'E7ozZSx8G6jhJ7jhzrOqzbM_2B4aFUEIwaYoD_2BUgH_2BJfob2_2FwPevm2BktgTKWSe2zT5D2DH4QWiht95M0WSQYacw_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>