|
JFinal是一个比较快速的JAVA WEB开发框架,一直在关注,最近用的比较多,项目中遇到页面提交一组MODEL数据提交的场景,但JFinal未尚未提供实现,因此临时写了一个,现在分享出来 ,给大家提供一个思路,代码仅供参考,如果不当之处请批评指正。
调用方法:
List<Const> beanList = ModelUtils.batchInjectModel(getRequest(),Const.class,"beans"); - package com.jfinal.core;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import javax.servlet.http.HttpServletRequest;
- import com.jfinal.plugin.activerecord.Model;
- /**
- * Model Tools
- *
- * @author wangkai
- * @date Feb 14, 2013 1:43:37 PM
- *
- */
- public class ModelUtils {
- /**
- * 将页面中的数组对象转换成List<Model>
- *
- * @param <T>
- * @param request
- * @param modelClass
- * @param prefix
- * @return
- *
- * @author wangkai
- * @date Feb 14, 2013
- *
- */
- @SuppressWarnings("unchecked")
- public static <T> List<T> batchInjectModel(
- final HttpServletRequest request,
- Class<? extends Model> modelClass, String prefix) {
- List<T> modelList = new ArrayList<T>();
- int size = getArrayLength(request, prefix);
- for (int i = 0; i < size; i++) {
- modelList.add((T) ModelInjector.inject(modelClass, prefix + "[" + i
- + "]", request, false));
- }
- return modelList;
- }
- /**
- * Get array key beans[0].ID -->beans[0] beans[1].ID -->beans[1]
- *
- * @param request
- * @param prefix
- * @return
- *
- * @author wangkai
- * @date Feb 14, 2013
- *
- */
- @SuppressWarnings("unchecked")
- public static Set<String> getArrayKeys(final HttpServletRequest request,
- String prefix) {
- Set<String> keys = new HashSet<String>();
- String arrayPrefix = prefix + "[";
- String key = null;
- Enumeration<String> names = request.getParameterNames();
- while (names.hasMoreElements()) {
- key = names.nextElement();
- if (!key.startsWith(arrayPrefix)) {
- continue;
- }
- if (key.indexOf("]") == -1) {
- continue;
- }
- keys.add(key.substring(0, key.indexOf("]") + 1));// Get all bean[X]
- }
- return keys;
- }
- /**
- * Get array length
- *
- * @param request
- * @param prefix
- * @return
- *
- * @author wangkai
- * @date Feb 14, 2013
- *
- */
- public static int getArrayLength(final HttpServletRequest request,
- String prefix) {
- return getArrayKeys(request, prefix).size();
- }
- }
复制代码- package com.jfinal.core;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import javax.servlet.http.HttpServletRequest;
- import com.jfinal.plugin.activerecord.Model;
- /**
- * Model Tools
- *
- * @author wangkai
- * @date Feb 14, 2013 1:43:37 PM
- *
- */
- public class ModelUtils {
- /**
- * 将页面中的数组对象转换成List<Model>
- *
- * @param <T>
- * @param request
- * @param modelClass
- * @param prefix
- * @return
- *
- * @author wangkai
- * @date Feb 14, 2013
- *
- */
- @SuppressWarnings("unchecked")
- public static <T> List<T> batchInjectModel(
- final HttpServletRequest request,
- Class<? extends Model> modelClass, String prefix) {
- List<T> modelList = new ArrayList<T>();
- int size = getArrayLength(request, prefix);
- for (int i = 0; i < size; i++) {
- modelList.add((T) ModelInjector.inject(modelClass, prefix + "[" + i
- + "]", request, false));
- }
- return modelList;
- }
- /**
- * Get array key beans[0].ID -->beans[0] beans[1].ID -->beans[1]
- *
- * @param request
- * @param prefix
- * @return
- *
- * @author wangkai
- * @date Feb 14, 2013
- *
- */
- @SuppressWarnings("unchecked")
- public static Set<String> getArrayKeys(final HttpServletRequest request,
- String prefix) {
- Set<String> keys = new HashSet<String>();
- String arrayPrefix = prefix + "[";
- String key = null;
- Enumeration<String> names = request.getParameterNames();
- while (names.hasMoreElements()) {
- key = names.nextElement();
- if (!key.startsWith(arrayPrefix)) {
- continue;
- }
- if (key.indexOf("]") == -1) {
- continue;
- }
- keys.add(key.substring(0, key.indexOf("]") + 1));// Get all bean[X]
- }
- return keys;
- }
- /**
- * Get array length
- *
- * @param request
- * @param prefix
- * @return
- *
- * @author wangkai
- * @date Feb 14, 2013
- *
- */
- public static int getArrayLength(final HttpServletRequest request,
- String prefix) {
- return getArrayKeys(request, prefix).size();
- }
- }
复制代码- <#list beans as x>
- <tr>
- <td style="width:40px;text-align:center" nowrap>${x_index+1}</td>
- <td style="width:120px" nowrap>
- <input type="hidden" name="beans[${x_index}].ID" value="${(x.ID)!}"/>
- <input type="text" name="beans[${x_index}].NAME" value="${(x.NAME)!}"/>
- </td>
- <td style="width:150px" nowrap>
- <input type="text" name="beans[${x_index}].CODE" value="${(x.CODE)!}"/>
- </td>
- <td style="width:120px" nowrap>
- <input type="text" name="beans[${x_index}].VALUE" value="${(x.VALUE)!}"/>
- </td>
- <td>
- <input type="text" name="beans[${x_index}].REMARK" value="${(x.REMARK)!}" style="width:99%;"/>
- </td>
- </tr>
- </#list>
复制代码 |
|