When using Struts, I feel useful for the front and back parameters of Struts, but I don't want to put Struts in the project, and it is impossible (some projects have been completed, and it is inconvenient to modify). So try to do the implementation in Struts. Main implementation: In the front desk HTML or JSP Submitted Form, there are many parameters, put these parameters in a bean, as long as the background is submitted, call the corresponding processing module to directly handle the parameter bean, and You don't have to do some of the front desk parameters, such as Req.GetParameter, etc., to do data isolated. The following is a class: package com.xxxx.common;
Import java.lang.reflect.field;
Import javax.servlet.http.httpservletRequest;
Public class paraPro {
public Object fillBean (HttpServletRequest req) {Object retObj = null; String BeanName = (String) req.getParameter ( "ParaClass"); System.out.println ( "bn is" BeanName); try {retObj = Class.forName ( Beanname) .newinstance ();} catch (instantiationException e) {// TODO automatically generates Catch block E.PrintStackTrace ();} catch (} catch (IllegalaccessException E) {// TODO automatically generates Catch block E.PrintStackTrace ();} catch ClassNotFoundException E) {// TODO automatically generates Catch block E.PrintStackTrace ();
Field Fields [] = retobj.getClass (). GetDeclaredfields (); string fieldName; string fieldType; string strGetParameter; for (int i = fields.length - 1; i> -1; i--) {Fields [i]. Setaccessible (TRUE); FieldName = Fields [i] .GetName (); FieldType = Fields [i] .gettype (). TOSTRING (); try {if (Req.getParameter (FieldName)! = null) {system.out. Println (FieldType.Tostring ()); if ("Class Java.Lang.integer")) {Fields [i] .SET (Retobj, New Integer (ref.getParameter (FieldName)); System.out .println ("FIELDNAME IS" FieldName "Type is" FieldType);
IF ("Class Java.lang.String"))) {Fields [i] .Set (Retobj, Req.GetParameter (FieldName)); System.out.Println ("FieldName IS" FieldName TYPE IS " FieldType);}}
} Catch (IllegalarGumentexception E1) {// TODO automatically generates Catch block E1.PrintStackTrace ();} catch (IllegalaccessException E1) {// TODO automatically generates Catch block E1.PrintStackTrace ();
}
Return Retobj;}
} The following is the process of submitted servlet: package com.xxxx.common;
Import java.io.ioException; import javax.servlet.servlet; import javax.servlet.servletException;
Import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletResponse;
/ ** * @version 1.0 * @Author * / public class argprocess extends httpservlet imports servlet {
/ ** * @see javax.servlet.http.HttpServlet # void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) * / public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Object Obj_tmp = null; parapro parapro = new parapro (); system.out.println ("begin" (string) Req.getParameter ("paraclass")); obj_tmp = parapro.FillBean (REQ); parabean pb = (parabean) Obj_tmp; system.out.println ("get is" pb.begindate "rate is" pb.rate);
}
} The following parameters used as the parameters: public class parabean {string begindate; string enddate; integer rate;
} The following is the HTML: < HTML>