zoukankan      html  css  js  c++  java
  • spring在方法体中装配对象

    package com.paypalm.dp.spring.controller;

    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;

    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import net.sf.json.JSONObject;

    import org.apache.commons.lang.StringUtils;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.ServletRequestDataBinder;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;

    import com.paypalm.dp.spring.constance.Constance;
    import com.paypalm.dp.spring.service.SuperService;
    import com.paypalm.dp.spring.utils.DataGrid;
    import com.paypalm.dp.spring.utils.Dom4jUtils;
    import com.paypalm.dp.spring.utils.ParamsAndField;
    import com.paypalm.dp.spring.utils.ParamsAndFieldRelation;
    import com.paypalm.dp.spring.utils.SetSql;

    @Controller
    @RequestMapping("super")
    public class SuperController<T>{
    @Resource
    private SuperService superService;
    // 实体对象
    protected T entity;

    //在执行action的具体方法之前之前去执行此方法
    public T prepare(String tableName){
    Class clazz;
    try {
    clazz = Class.forName(Constance.pojoPath+tableName);//里面是类的完整名字,反射即可得到对象
    // 利用反射新建enity对象
    entity = (T) clazz.newInstance();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return entity;
    }

    @RequestMapping(params="add")
    public void add(String tableName, HttpServletRequest request){
    T t = prepare(tableName);
    ServletRequestDataBinder binder = new ServletRequestDataBinder(t);
    binder.bind(request);
    this.superService.addObject(tableName, t);
    }

    @RequestMapping(params="update")
    public void update(String tableName, HttpServletRequest request){
    T t = prepare(tableName);
    ServletRequestDataBinder binder = new ServletRequestDataBinder(t);
    binder.bind(request);
    this.superService.updateObject(tableName, t);
    }

    @RequestMapping(params="delete")
    public void delete(String tableName, Object id){
    this.superService.deleteObject(tableName, id);
    }

    @RequestMapping(params="query")
    @ResponseBody
    public void query(Integer rows,Integer page, String tableName,HttpServletRequest request){
    ParamsAndField relation = readXmlAndAssign(tableName,request);
    Map map = new HashMap();
    map.put("tableName", tableName);
    map.put("params", SetSql.getSelectCondition(relation.getParamsAndFieldRelation()));
    map.put("order", SetSql.getOrderCondition(relation.getMap()));
    //注意获取数量不需要页码限制
    Long num = this.superService.queryObjectListNum(map);
    System.out.println(map.get("params"));
    map.put("spage",(page-1)*rows);
    map.put("epage", page*rows);
    List list = this.superService.queryObjectList(map);
    DataGrid dataGrid = new DataGrid(list, num);
    HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    try {
    response.getWriter().print(JSONObject.fromObject(dataGrid));
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    * 解析查询条件
    * 若为空字段且不是必须的可不加入查询条件中
    * 否则则加入
    * 错误:若是int或是其他字段,则会报异常,不知怎么处理
    * @return
    */
    public static ParamsAndField readXmlAndAssign(String tableName,HttpServletRequest request){
    ParamsAndField relation = Dom4jUtils.getSqlParams(tableName);
    for (Iterator iterator = relation.getParamsAndFieldRelation().iterator(); iterator.hasNext();) {
    ParamsAndFieldRelation paramsAndFieldRelation = (ParamsAndFieldRelation) iterator
    .next();
    String val = request.getParameter(paramsAndFieldRelation.getShow());
    if("0".equals(paramsAndFieldRelation.getRequired()) && StringUtils.isEmpty(val)){
    iterator.remove();
    }
    else{
    paramsAndFieldRelation.setValue(val);
    }
    }
    return relation;
    }
    }

  • 相关阅读:
    Spring(一)Spring的基本应用
    flask摘记
    python摘记
    String Algorithm
    leetcode -- hard part
    leetcode -- medium part
    leetcodo--Easy part
    unix网络编程
    SQL
    计算机网络知识
  • 原文地址:https://www.cnblogs.com/dashen/p/4206490.html
Copyright © 2011-2022 走看看