Web based automatic processing form filling JavaBean

Source: Internet
Author: User

Http://hi.baidu.com/smilelive/blog/item/e31b8f50ea1ef212367abe77.html

In web development, the form will definitely be designed to submit, and the form is generally designed to a table, the table and corresponding to a javabea. In general we are repeating a lot of request.getparameter () and x.setxx (); So is there any way to simplify the operation above.

Suppose we have a user class, attribute name,sex,age, a JSP or HTML page, and a form-form property name,sex,age and JavaBean corresponding. A servlet that processes form uploads.

This is mainly about the functionality of this servlet:

Enumeration en = Request.getparameternames ();//Get all the requested parameters
User user = new user ();
field[] fs = User.getclass (). Getdeclaredfields ();//Get a list of all the properties of the user class
while (En.hasmoreelements ()) {//Traverse request parameter
String param = en.nextelement (). toString (). Trim ();//Get Property name
for (int i = 0; i < fs.length i++) {//Traverse the property list of the user class
if (!fs[i].getname (). Equals (param)) {//If the property name and the request parameter name are not equal to continue, it is written here to reduce the nesting of if else;
Continue
}
Fs[i].setaccessible (TRUE);//Set property to be changed, private property in reflection cannot be accessed, set accessible to true this property can access
Object value = null;
if (Fs[i].gettype (). GetName (). Equals ("java.lang.String")) {//Judgment attribute type the JavaBean attribute type is defined as an extended data type of eight basic data types and eight basic types, The most common data types, such as date and time, are added.
Value = Request.getparameter (param). toString ();
else if (Fs[i].gettype (). GetName (). Equals ("int")) {
Value = Integer.parseint (Request.getparameter (param). Trim ());
}
try {
Fs[i].set (user, value);//Set the value of the property
catch (IllegalArgumentException e) {
E.printstacktrace ();
catch (Illegalaccessexception e) {
E.printstacktrace ();
}
Break
}
}

System.out.println (User.getname () + "T" +user.getage () + "T" +user.getsex ());

Such a serlvet can only set the value of a class, you want to set the value of multiple classes, you can add a parameter after the URL, such as the creation of Xx.do?jb=com.up72.bean.user,servlet object to

String className = Request.getparameter ("JB");

Object JavaBean = Class.forName (className). newinstance ();

Other operations are unchanged.

Of course, a better approach would be to write a configuration file that initializes the bean by reading the configuration file, which, combined with the simple servlet in front of it, basically completes the functionality of the simple Struts core controller. However, there is no solution to the problem of file uploading.

Rapid development of a reuse Ajax submission with the help of MooTools
http://jhaij.iteye.com/blog/1136595
(This article shows how to use automatic encapsulation)

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: info-contact@alibabacloud.com 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.