Programming tips in the Web layer of Web Engineering

Source: Internet
Author: User

In this article, we take a look at the video of "The implementation of CRM case" by Fanglixun, the teacher's "JDBC Primer", which extracts the part of teacher in the simple case of designing management system, so as to hope to be helpful in the development process later.

In this video, the Customer Relationship Management system interface consists of a simple "add customer" and "View Customer" two hyperlinks. Where the "View Customer" feature involves using a database for page display pagination effect, the implementation of this feature please see the relevant blog.

In the "Add Customer" function, the teacher is very dexterous to use a servlet Doget method and the Dopost method, which reduces the definition of the class and optimizes the program. We know that in MVC design mode, a request from the client browser should be processed by the servlet first and then forwarded to the JSP for display to the user. Therefore, in the JSP page, the "Add User" hyperlink address is a servlet, the form submitted by the "Action" property is also a servlet, of course, add the user's hyperlinks and add the user's form is not the same JSP page, but can use the same servlet to handle, This is a very clever second place, as shown in:

In the Web layer processing add user Addcustomerservlet, in the Doget method, there is only one line of code, that is, jump (forward) to another JSP page, this JSP page is to add the user's form display, when we fill out the form and submit, Set the submission address or just addcustomerservlet, at this time should be used is the Dopost method, originally this method is specifically used to process the form data.

In the Dopost method of processing the form, first we need to validate the form, the form verification passed, to encapsulate the form object in the request into a javabean, in order to be able to implement the request object, regardless of what object is encapsulated in the complete package of its data into an entity JavaBean object, We build a tool class at the Web layer, and in this tool class we design a method that satisfies this, using generic, reflection, and Beanutils tool classes:

1  Public classWebutils {2      Public Static<T> T Request2bean (httpservletrequest request, class<t>Beanclass) {3         Try{4T Bean =beanclass.newinstance ();5Map Parammap =Request.getparametermap ();6 beanutils.populate (Bean, parammap);7             returnBean;8             9}Catch(Exception e) {Ten             Throw NewRuntimeException (e); One         } A     } -}

If you want to use the above to encapsulate the object in the request into the corresponding JavaBean object, you must ensure that the Name property in the form, the property name of the corresponding JavaBean in the domain package in the project, is consistent so that the data can be copied correctly when using Beanutils.

Note, however, that using Beanutils's populate method can only convert strings to strings and eight basic data types, and if there are properties of the date type in the Bean object, Then be sure to use the converter convertutils to register the type you want to convert before using the Beanutils method. The code is as follows:

1  Public classWebutils {2      Public Static<T> T Request2bean (httpservletrequest request, class<t>Beanclass) {3         Try{4T Bean =beanclass.newinstance ();5Map Parammap =Request.getparametermap ();6Convertutils.register (NewConverter () {7 @Override8                  Public<T> T Convert (class<t>type, Object value) {9                     if(Value = =NULL) {Ten                         return NULL; One                     } AString Datestr =(String) value; -                     if(Datestr.trim (). Equals ("")) { -                         return NULL; the                     } -SimpleDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd"); -                     Try { -Date Date =Format.parse (DATESTR); +                         return(T) date; -}Catch(ParseException e) { +                         Throw NewRuntimeException (e); A                     } at                 } -}, Date.class); - beanutils.populate (Bean, parammap); -             returnBean; -              -}Catch(Exception e) { in             Throw NewRuntimeException (e); -         } to     } +}

Programming tips in the Web layer of Web Engineering

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.