MVC模式下Ajax前后交互总结

form表单提交(通过表单来提交,ModelAttribute("form"),传的值必须在表单中,注意赋值时的id冲突问题。)

controller层

@RequestMapping(value = "queryAllRatIngIndex", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
 @ResponseBody

 public Object queryRatingIndex(@ModelAttribute("form") RateIndex rateindex, HttpServletRequest request, HttpServletResponse response) {

  String uuid = UUID.randomUUID().toString();
  Log.info(logger, uuid, "查询所有指标项", JSONObject.fromObject(rateindex).toString());
  String result = "";
  try {
   result = rateindexService.queryRateIndexByConditions(uuid, logger, rateindex);
  } catch (Exception e) {
   e.printStackTrace();
   Log.error(logger, uuid, "查询所有指标出错了", e, "查询参数", rateindex);
   return null;
  }
  return result;

 }

Service层

@Override
 public String queryRateIndexByConditions(String uuid, Logger logger, RateIndex rateindex) throws Exception {
  if(0 ==rateindex.getPageSize()){
   rateindex.setPageSize(Integer.MAX_VALUE);
  }
  rateindex.setStart(rateindex.getCurrentPage() * rateindex.getPageSize());
  PageModel<RateIndex> pageModel = new PageModel<RateIndex>();
  Log.info(logger, uuid, "service查询指标项", "参数", JSONObject.fromObject(rateindex).toString());
  int allRow = indexDao.queryRateRows(uuid, logger, rateindex);
  List<RateIndex> rateIndexList = indexDao.queryRateIndex(uuid, logger, rateindex);
  pageModel.setAllRow(allRow);
  pageModel.setPageSize(rateindex.getPageSize());
  pageModel.setCurrentPage(rateindex.getCurrentPage());
  if (null == rateIndexList || rateIndexList.size() == 0) {
   pageModel.setTotalPage(0);
  } else {
   pageModel.setTotalPage(PageModel.countTatalPage(rateindex.getPageSize(), allRow));
   pageModel.setList(rateIndexList);
  }
  return JSONObject.fromObject(pageModel).toString();

 }

dao层

@Override
 public List<RateIndex> queryRateIndex(String uuid, Logger logger, RateIndex rateindex) throws Exception {
  return super.getSqlSession().selectList("rateIndexMapper.queryRateIndex", rateindex);

 }

xml

<mapper namespace="rateIndexMapper" >
  <resultMap id="BaseResultMap" type="com.zysk.exp.rateindex.pojo.RateIndex" >
     <id column="ID" property="id" jdbcType="VARCHAR" />
    <result column="Name" property="name" jdbcType="VARCHAR" />
    <result column="Memo" property="memo" jdbcType="VARCHAR" />
    <result column="CreatorID" property="creatorid" jdbcType="VARCHAR" />
    <result column="CreateDate" property="createdate" jdbcType="VARCHAR" />
    <result column="DataStatus" property="datastatus" jdbcType="VARCHAR" />
    <result column="indexType" property="indextype" jdbcType="VARCHAR" />
    <result column="formulas" property="formulas" jdbcType="VARCHAR" />
    <result column="result_sql" property="resultSql" jdbcType="VARCHAR" />
    <result column="f_indexID" property="fIndexid" jdbcType="VARCHAR" />
    <result column="weight" property="weight" jdbcType="INTEGER" />
    <result column="date_scope" property="dateScope" jdbcType="INTEGER" />
    <result column="bottom_flag" property="bottomFlag" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
      ID, Name, Memo, CreatorID,  date_format(CreateDate,'%Y-%c-%d') as  CreateDate, DataStatus, indexType, formulas, result_sql, 
    f_indexID, weight,date_scope, bottom_flag
  </sql>

  <select id="queryRateIndex"   parameterType="com.zysk.exp.rateindex.pojo.RateIndex"  resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from i_rate_index
     <trim prefix="WHERE" prefixOverrides="AND |OR ">
   <if test="id != null  and  id !='' ">and id = #{id}</if>
   <if test="name != null  and name !='' ">and   name like CONCAT('%','${name}','%' ) </if>
   <if test="createdate != null  and  createdate !='' ">and CreateDate = #{createdate}</if>

  </trim>
  order by FIELD (DataStatus,1,0,2),CreateDate desc,bottom_flag asc
  limit ${start},${pageSize}

  </select>

前台ajax请求方式一GET

var url = "<%=contextPath%>/ratingIndex/queryRatIngIndexWeightByID?RadID="+id;
   $.ajax({ 
    type : 'get',
    url:url,
    async : true, 
    cache : false,
    dataType : 'json', 
    success : function(data){
     if(data){
      if(data.outputHead.resultID==0){
       weightSum = data.outputData;
      }
     }
    },
    error : function(data){
     alert("error"); 
    }
   });

猜你喜欢

转载自blog.csdn.net/weixin_36810906/article/details/80274575
今日推荐