码迷,mamicode.com
首页 > 编程语言 > 详细

spring在方法体中装配对象

时间:2015-01-06 19:43:59      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

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在方法体中装配对象

标签:

原文地址:http://www.cnblogs.com/dashen/p/4206490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!