当前位置: 代码迷 >> Web前端 >> struts2.2-jquery-plugin学习Action的写法(5)
  详细解决方案

struts2.2-jquery-plugin学习Action的写法(5)

热度:109   发布时间:2012-09-16 17:33:16.0
struts2.2-jquery-plugin学习Action的写法(五)

(一)首先关于数据显示(分页查询)的Action的写法:

package com.newsicom.jxc.basicInfo.goodsUnits.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import com.newsicom.jxc.basicInfo.common.model.GoodsUnits;
import com.newsicom.jxc.basicInfo.common.model.ProductUnits;
import com.newsicom.jxc.basicInfo.common.util.PageBean;
import com.newsicom.jxc.basicInfo.goodsUnits.dao.IproductUnitsDAO;
import com.newsicom.jxc.basicInfo.goodsUnits.service.IGoodsUnitsService;
import com.opensymphony.xwork2.ActionSupport;

public class GoodsUnitsAction extends ActionSupport{
	//此处的goodsUnitsService不能有get方法


IGoodsUnitsService<GoodsUnits> goodsUnitsService;
	
	private String filters;//多条件查询
	private List<GoodsUnits> gridModel;//表格中的数据;
	private Integer rows=10;//页大小,ajax请求参数提供,对应表格中的rowNum;
	private Integer page=1;//当前页,ajax请求提供
	private Integer total=0;//总页数
	private Integer records=0;//总记录表格中使用

	
	public List<GoodsUnits> getGridModel() {
		return gridModel;
	}
	public void setGridModel(List<GoodsUnits> gridModel) {
		this.gridModel = gridModel;
	}
	
	public void setGoodsUnitsService(
			IGoodsUnitsService<GoodsUnits> goodsUnitsService) {
		this.goodsUnitsService = goodsUnitsService;
	}
	public Integer getRows() {
		return rows;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getTotal() {
		return total;
	}
	public void setTotal(Integer total) {
		this.total = total;
	}
	public Integer getRecords() {
		return records;
	}
	public void setRecords(Integer records) {
		this.records = records;
	}
	
	public void setFilters(String filters) {
		this.filters = filters;
	}
	
	
	//转换['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
	public static String switchSign(String op)
		{
			if("eq".equals(op))
			{
				return "=";
			}
			else if("ne".equals(op))
			{
				return "<>";
			}
			else if("lt".equals(op))
			{
				return "<";
			}
			else if("gt".equals(op))
			{
				return ">";
			}
			else
				return "like %";
				
				
		}
		
	public String execute() throws Exception
	{
		if(this.filters!=null)
		{
			JSONObject jsonFilter = (JSONObject) JSONSerializer.toJSON( filters );
			String groupOp = jsonFilter.getString("groupOp");
			System.out.println("groupOp :" + groupOp);
			JSONArray rules = jsonFilter.getJSONArray("rules");
			int rulesCount = JSONArray.getDimensions(rules)[0];
			System.out.println("Count Rules :" + rulesCount);
			//查询语句
			String sql="from ProductUnits p where ";
			
			if("AND".equals(groupOp))
			{
				
				for (int i = 0; i < rulesCount; i++) 
				{
					JSONObject rule = rules.getJSONObject(i);
					System.out.println("field :" + rule.getString("field"));
					System.out.println("op :" + rule.getString("op"));
					System.out.println("data :" + rule.getString("data"));
					if("id".equals(rule.getString("field")))
					{
					sql=sql+"p."+rule.getString("field")+switchSign(rule.getString("op"))+rule.getString("data");
					}
					else
					{
						sql=sql+"p."+rule.getString("field")+switchSign(rule.getString("op"))+"'"+rule.getString("data")+"'";
					}
					sql=sql+" and p."+rule.getString("field")+switchSign(rule.getString("op"))+"'"+rule.getString("data")+"'";
				}
				
			}
			else
			{
				for (int i = 0; i < rulesCount; i++) 
				{
					JSONObject rule = rules.getJSONObject(i);
					System.out.println("field :" + rule.getString("field"));
					System.out.println("op :" + rule.getString("op"));
					System.out.println("data :" + rule.getString("data"));
					if("id".equals(rule.getString("field")))
					{
					sql=sql+" p."+rule.getString("field")+switchSign(rule.getString("op"))+rule.getString("data");
					
					}
					else
					{
						sql=" or "+sql+rule.getString("field")+switchSign(rule.getString("op"))+"'"+rule.getString("data")+"'";
						
					}
				}
			}
			System.out.println(sql);
			PageBean<GoodsUnits> pb=this.goodsUnitsService.getT(sql,page, rows);
			total=pb.getTotalPage();
			records=pb.getTotal();
			gridModel=pb.getT();
		}
		
		else
		{
			PageBean<GoodsUnits> pb=this.goodsUnitsService.getT("from GoodsUnits",page, rows);
			total=pb.getTotalPage();
			System.out.println(total);
			records=pb.getTotal();
			System.out.println(records);
			gridModel=pb.getT();
		}
		return SUCCESS;
	}
	
}
?

?(二)关于增,删,改,编辑,的操作的Action:

package com.newsicom.jxc.basicInfo.goodsUnits.action;

import java.util.StringTokenizer;

import com.newsicom.jxc.basicInfo.common.model.GoodsUnits;
import com.newsicom.jxc.basicInfo.common.model.ProductUnits;
import com.newsicom.jxc.basicInfo.goodsUnits.dao.IproductUnitsDAO;
import com.newsicom.jxc.basicInfo.goodsUnits.service.IGoodsUnitsService;
import com.opensymphony.xwork2.ActionSupport;

public class EditGoodsUnitsAction extends ActionSupport{
//此处的googsUnitsService不能有get方法:


	private IGoodsUnitsService<GoodsUnits> goodsUnitsService;
	private String oper;// add或del或edit
	private String id;
	private String cd;//编码
	private String name;//名称
	private String byname;//别名
	private String memCode;//助记符
	private String memo;//备注

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public String getCd() {
		return cd;
	}
	public void setCd(String cd) {
		this.cd = cd;
	}
	public String getByname() {
		return byname;
	}
	public void setByname(String byname) {
		this.byname = byname;
	}
	public String getMemCode() {
		return memCode;
	}
	public void setMemCode(String memCode) {
		this.memCode = memCode;
	}
	public String getMemo() {
		return memo;
	}
	public void setMemo(String memo) {
		this.memo = memo;
	}
	

	public void setGoodsUnitsService(
			IGoodsUnitsService<GoodsUnits> goodsUnitsService) {
		this.goodsUnitsService = goodsUnitsService;
	}

	@Override
	public String execute() throws Exception {
		if ("add".equals(oper)) {
			this.add();
		} else if ("del".equals(oper)) {
			this.delete();
		} else if (oper.equalsIgnoreCase("edit")) {
			this.edit();
		}
		return null;
	}

	private void add() {
		GoodsUnits goodsUnits = new GoodsUnits();
		goodsUnits.setCd(cd);
		goodsUnits.setName(name);
		goodsUnits.setByname(byname);
		goodsUnits.setMemCode(memCode);
		goodsUnits.setMemo(this.memo);
		goodsUnitsService.create(goodsUnits);
	}

	private void delete() {
		StringTokenizer ids = new StringTokenizer(id, ",");
		while (ids.hasMoreTokens()) {
			int removeId = Integer.parseInt(ids.nextToken());
			Integer i = new Integer(removeId);
			goodsUnitsService.deleteByID(i);
		}
	}

	private void edit() {
		Integer integer = Integer.parseInt(id);
		GoodsUnits goodsUnits = (GoodsUnits) goodsUnitsService.findById(
				GoodsUnits.class, integer);
		goodsUnits.setCd(cd);
		goodsUnits.setName(name);
		goodsUnits.setByname(byname);
		goodsUnits.setMemCode(memCode);
		goodsUnits.setMemo(this.memo);
		goodsUnitsService.update(goodsUnits);
	}
}

?关于Action的配置请查看配置文件那篇文章.

  相关解决方案