Direct assignment of data beans in JSP

xiaoxiao2021-03-06  38

Generally, most commonly used in the FORM form in the database, if the data item is much more complex, use the usual request.getParameter () to get a character pattern, because JSP is different from PHP Perl other scripting languages, JSP pair The data type requires very strict, and cannot be freely converted in use. Therefore, it is necessary to perform manual conversion data type after using request.getParameter (). If there is more data items, it is very complicated, and the boring is easy,

In fact, there is also a function of automatically converting data types in JSP, using the usebean function. Such as an Form form is as follows:

Product Name: Product Number: Product Price: .....

We have to store the data in this form to the database profile. Obviously, this work needs to be completed in Save.jsp.

In Save.jsp we assume that there is already a good ProfileDB database bean, ProfileDB's probably code is:

Public profileb {

Private string productname = ""; public void setProductName (string productname) {this.productname = productName}

PRIVATE INT Qty = 0; public void setqty (int qty) {this.qty = quiTY}

Private float price = 0.0; public void setprice (float price) {this.price = price}

Public void insert () {

...................

}

}

Note that Qty Price in ProfileDB is not a string type, so you want to assign Qty Price data in the Form to Profile Qty and Price, you need to perform data conversion: <% int = interger.parseint (Request.getParameter (" qty ")); float price = ....%> .....

Profile.insert ();

Is this very cumbersome? There is still a shortcomings, strictly, must have a Catch statement on the conversion statement, because if there is no Qty in the form, it is Request.GetParameter ("Qty") for null. Once executed, the JSP error program will only give you a NULL error message, no strict pointing out where null is there, will make your headache, people who have just contacted JSPs are the most afraid of NULL error.

In fact, the useBean in JSP provides a method property = "*" allows the system to complete the conversion: First, you must have a method for storage for each variable method in ProfileDB: setXXXX, such as setProductName setQty here XXXX (That is, ProductName Qty Price) The Name of INPUT should be consistent with the above form program, Save.jsp program new code: Different Use setProperty Property = "*" is to handle all the data submitted in the form to the bean deal, because it can make a variety of complex logic processing in Bean, so that PROFILE.INSERT () can be used directly. The data in the form is stored directly into the database. Is it very simple? Especially if there are many data options in your form, and the form data type is very complicated, use such a statement simple and convenient.

From now on, you will no longer be afraid that the huge data in the form is stored.

转载请注明原文地址:https://www.9cbs.com/read-68948.html

New Post(0)