Let the Jfinal controller's Getmodel method support the non-prefixed variable name, convenient ExtJS and so on use

Source: Internet
Author: User

This implementation needs to modify the Jfinal source code, the code implementation has been submitted to jfinal, the next version is estimated to add this feature. If you want to use it in advance, recompile your code to jfinal it.

Modify the Modelinjector.java file under the Com.jfinal.core package (replace the entire content)

/** * copyright  (c)  2011-2015, James Zhan  Giampo   ([email protected]).  * * Licensed under the Apache License, Version 2.0  (The   "License"); * you may not use this file except in  Compliance with the license. * you may obtain a copy of  the license at * *      http://www.apache.org/licenses /license-2.0 * * unless required by applicable law or agreed  to in writing, software * distributed under the license is  distributed on an  "As is"  basis, * without warranties or  conditions of any kind, either express or implied. * see  the license for&nbsP;the specific language governing permissions and * limitations under  the License. */package com.jfinal.core;import java.lang.reflect.Method;import  java.util.map;import java.util.map.entry;import javax.servlet.http.httpservletrequest;import  com.jfinal.kit.strkit;import com.jfinal.plugin.activerecord.activerecordexception;import  com.jfinal.plugin.activerecord.model;import com.jfinal.plugin.activerecord.table;import  com.jfinal.plugin.activerecord.tablemapping;/** * modelinjector */final class  modelinjector {@SuppressWarnings ("Unchecked") Public static <t> t inject (Class <?> modelclass, httpservletrequest request, boolean skipconverterror)  { String modelname = modelclass.getsimplename ();return  (T) inject (modelclass,  Strkit.firstchartolowercase (modelname), request, Skipconverterror);} @SuppressWarnings ({  "rawtypes",  "Unchecked" &NBSP;}) public static final <t>  t inject (class<?> modelclass, string modelname, httpservletrequest  Request, boolean skipconverterror)  {object model = null;try {model =  modelclass.newinstance ();}  catch  (exception e)  {throw new runtimeexception (e);} if  (Model instanceof model) Injectactiverecordmodel (model) Model, modelname, request,  skipconverterror); Elseinjectcommonmodel (model, modelname, request, modelclass,  Skipconverterror);return  (T) model;} Private static final void injectcommonmodel (Object model, string modelname,  httpservletrequest request, class<?> modelclass, boolean skipconverterror )  {method[] methods = modelclass.getmethods ();//When MOdelname is null or "", do not add prefix string modelnameanddot =  ""; if (Strkit.notblank (modelname)) { modelnameanddot= modelname +  ".";} for  (Method method : methods)  {string methodname = method.getname ();   (Methodname.startswith ("set")  == false)// only setter methodcontinue; Class<?>[] types = method.getparametertypes ();if  (types.length != 1)//  only one parametercontinue; String attrname = methodname.substring (3); String value = request.getparameter (Modelnameanddot + strkit.firstchartolowercase ( attrname));if  (value != null)  {try {method.invoke (Model, typeconverter.convert ( Types[0], value));}  catch  (exception e)  {if  (skipconverterror == false) throw new  RuntimeException (e);}}} @SuppressWarnings ("Rawtypes") private static final vOid injectactiverecordmodel (model<?> model, string modelname,  Httpservletrequest request, boolean skipconverterror)  {//prefix string is not added when modelname is null or ""  modelNameAndDot =  ""; if (Strkit.notblank (modelname)) {modelnameanddot= modelname +   ".";} Table table = tablemapping.me (). GetTable (Model.getclass ()); Map<string, class<?>> columntypemap = table.getcolumntypemap (); Map<string, string[]> parasmap = request.getparametermap ();//Iterate through the table field for (Entry< String, class<?>> entry: columntypemap.entryset ()) {string key =  Modelnameanddot + entry.getkey ();//request set the corresponding attribute if (Parasmap.containskey (key)) {Class coltype  = entry.getvalue (); String[] paravalue = parasmap.get (Key);try {// object value =  Converter.convert (COLTYPE,&NBSP;PARAVALUE&NBSP;!=&NBSP;NULL&NBSP;?  paravalue[0] : null);object value = paravalue[0] != null ?  Typeconverter.convert (Coltype, paravalue[0])  : null;model.set (Entry.getkey (),  value);}  catch  (Exception ex)  {if  (skipconverterror == false) throw new  RuntimeException ("can not convert parameter: "  + key, ex);}}}}


How to use:

Getmodel (Model.class, "")

When the second argument is empty, it means that the property value is obtained without a prefix, and the property value of the corresponding prefix is obtained when the part is empty.

Assume that User.class has a username,password of two properties.

For example, Getmodel (User.class, ""), gets the Username,password property.

Getmodel (User.class, "User"), gets the User.username,user.password property.

Let the Jfinal controller's Getmodel method support the non-prefixed variable name, convenient ExtJS and so on use

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.