Oma1ly 2016-04-05 01:39 采纳率: 50%
浏览 1499
已采纳

java web项目 传值问题Spring webwork 框架

现有一项目使用的Spring webwork 框架 数据库使用jdbc 现在前端有数据 。大概60个字段的数据 ,pojo里有此对象
现在需要前端传值到后台 希望可以action 可以接受 对象(不想传输/接收60个字段那样pojo里的对象就没有意义了)请问使用什么方式才能传过来对象呢 ! 求助各位大神 新手好痛苦 补充一下前端使用的EXTJS2

  • 写回答

2条回答 默认 最新

  • viewgroup 2016-04-05 02:03
    关注

    使用映射,主要就是方法映射,只要传过来的字段和你的pojo对象里面的字段一样,就可以set这个字段。/**

    • Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com). *
    • 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 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 inject(Class<?> modelClass, HttpServletRequest request, boolean skipConvertError) {
      String modelName = modelClass.getSimpleName();
      return (T)inject(modelClass, StrKit.firstCharToLowerCase(modelName), request, skipConvertError);
      }

      @SuppressWarnings({ "rawtypes", "unchecked" })
      public static final 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);
      else
          injectCommonModel(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();
      for (Method method : methods) {
      String methodName = method.getName();
      if (methodName.startsWith("set") == false) // only setter method
      continue;

          Class<?>[] types = method.getParameterTypes();
          if (types.length != 1)                      // only one parameter
              continue;
      
          String attrName = methodName.substring(3);
          String value = request.getParameter(modelName + "." + 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) {
      Table table = TableMapping.me().getTable(model.getClass());

      String modelNameAndDot = modelName + ".";
      
      Map<String, String[]> parasMap = request.getParameterMap();
      for (Entry<String, String[]> e : parasMap.entrySet()) {
          String paraKey = e.getKey();
          if (paraKey.startsWith(modelNameAndDot)) {
              String paraName = paraKey.substring(modelNameAndDot.length());
              Class colType = table.getColumnType(paraName);
              if (colType == null)
                  throw new ActiveRecordException("The model attribute " + paraKey + " is not exists.");
              String[] paraValue = e.getValue();
              try {
                  // Object value = Converter.convert(colType, paraValue != null ? paraValue[0] : null);
                  Object value = paraValue[0] != null ? TypeConverter.convert(colType, paraValue[0]) : null;
                  model.set(paraName, value);
              } catch (Exception ex) {
                  if (skipConvertError == false)
                      throw new RuntimeException("Can not convert parameter: " + modelNameAndDot + paraName, ex);
              }
          }
      }
      

      }
      }

    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化