首页>代码>SSH整合maven+jquery+easyUI+mysql5.5图书管理实现增删改查及模糊查询>/BooksManager/src/main/java/org/bookmanager/action/bookAction.java
package org.bookmanager.action;

import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.bookmanager.model.Brydm;
import org.bookmanager.model.Btscg;
import org.bookmanager.service.BrydmService;
import org.bookmanager.service.BtscgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
 
/**
 * 商品操作-控制层
 *
 */
@Controller
@Scope("prototype")
public class bookAction extends ActionSupport {
 
    private static final long serialVersionUID = 1L;
     
    private BtscgService btscgService;
    private BrydmService brydmService;
    
    public BtscgService getBtscgService() {
		return btscgService;
	}

	public void setBtscgService(BtscgService btscgService) {
		this.btscgService = btscgService;
	}

	public BrydmService getBrydmService() {
		return brydmService;
	}

	public void setBrydmService(BrydmService brydmService) {
		this.brydmService = brydmService;
	}

	Logger logger = Logger.getLogger(bookAction.class);
     
    private String id;
    private String no;
    private String name;
    private String pid;
    private Date pdate;
     
    /**
     * 新增图书
     *
     * @return
     */
    public String save() {       
    	String id = UUID.randomUUID().toString().replace("-", "");
    	System.out.println(JSONObject.toJSONString(logger));
    	Btscg btscg = new Btscg(id,no , name, pid, pdate);
    	System.out.println(JSONObject.toJSONString(btscg));
    	btscgService.save(btscg);        
    	PrintWriter writer = null;
    	try {
			HttpServletResponse response = ServletActionContext.getResponse();		
//			response.setContentType("application/json;charset=utf-8");
			response.setContentType("text/html;charset=utf-8");
			writer = response.getWriter();
			response.setDateHeader("Expires", 0);
			Map<String,Object> out = new HashMap<String,Object>();
			out.put("success", true);
			String object = JSONObject.toJSONString(out);
			writer.write(object);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				writer.close();
			}
		}
		return null;
    }
    
    /**
     * 查询全部图书
     *
     * @return
     */
    public List<Btscg> findall() {
		PrintWriter writer = null;
    	try {
			HttpServletResponse response = ServletActionContext.getResponse();		
//			response.setContentType("application/json;charset=utf-8");
			response.setContentType("text/html;charset=utf-8");
			writer = response.getWriter();
			response.setDateHeader("Expires", 0);
			List<Btscg> out = btscgService.queryAll();
			for(int i=0;i<out.size();i++){
			String pid = out.get(i).getPid();
			String pname = "未知";
			Brydm brydm = new Brydm();
			brydm.setPid(pid);
			List<Brydm> pResultList = brydmService.queryByParam(brydm);
			for(int j=0;j<pResultList.size();j++){
				Brydm pNameMap = (Brydm) pResultList.get(j);
				if(pid !=null){
					if(pid.equals(pNameMap.getPid())){
						pname = pNameMap.getPname();
					}
				}
			}
			out.get(i).setPid(pname);
			}
			String object = JSONObject.toJSONString(out);
			writer.write(object);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				writer.close();
			}
		}
		return null;
    }
    
    /**
     * 按条件查询图书
     *
     * @return
     */
    public List<Btscg> find() {   
		PrintWriter writer = null;
    	Btscg btscg = new Btscg(id, no, name, pid, pdate);
    	logger.info("btscg = "+JSONObject.toJSONString(btscg));
    	try {
			HttpServletResponse response = ServletActionContext.getResponse();		
//			response.setContentType("application/json;charset=utf-8");
			response.setContentType("text/html;charset=utf-8");
			writer = response.getWriter();
			response.setDateHeader("Expires", 0);
			List<Btscg> out = btscgService.queryByParam(btscg);
			for(int i=0;i<out.size();i++){
				String pid = out.get(i).getPid();
				String pname = "未知";
				Brydm brydm = new Brydm();
				brydm.setPid(pid);
				List<Brydm> pResultList = brydmService.queryByParam(brydm);
				for(int j=0;j<pResultList.size();j++){
					Brydm pNameMap = (Brydm) pResultList.get(j);
					if(pid !=null){
						if(pid.equals(pNameMap.getPid())){
							pname = pNameMap.getPname();
						}
					}
				}
				out.get(i).setPid(pname);
				}
			String object = JSONObject.toJSONString(out);
	    	logger.info("object = "+object);
			writer.write(object);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				writer.close();
			}
		}
		return null;
    }
    
    /**
     * 更新图书
     *
     * @return
     */
    public String update() {    
    	Btscg btscg = new Btscg(id, no, name, pid, pdate);
    	btscgService.update(btscg);        
    	PrintWriter writer = null;
    	try {
			HttpServletResponse response = ServletActionContext.getResponse();		
//			response.setContentType("application/json;charset=utf-8");
			response.setContentType("text/html;charset=utf-8");
			writer = response.getWriter();
			response.setDateHeader("Expires", 0);
			Map<String,Object> out = new HashMap<String,Object>();
			out.put("success", true);
			String object = JSONObject.toJSONString(out);
			writer.write(object);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				writer.close();
			}
		}
		return null;    
    }
 
    /**
     * 删除图书
     *
     * @return
     */
    public String delete() {  
    	btscgService.delete(id);        
    	PrintWriter writer = null;
    	try {
			HttpServletResponse response = ServletActionContext.getResponse();		
//			response.setContentType("application/json;charset=utf-8");
			response.setContentType("text/html;charset=utf-8");
			writer = response.getWriter();
			response.setDateHeader("Expires", 0);
			Map<String,Object> out = new HashMap<String,Object>();
			out.put("success", true);
			String object = JSONObject.toJSONString(out);
			writer.write(object);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				writer.close();
			}
		}
		return null;    
    }

    /**
     * 获得全部采购人
     *
     * @return
     */
    public List<Btscg> getPids() {   
		PrintWriter writer = null;
    	try {
			HttpServletResponse response = ServletActionContext.getResponse();		
//			response.setContentType("application/json;charset=utf-8");
			response.setContentType("text/html;charset=utf-8");
			writer = response.getWriter();
			response.setDateHeader("Expires", 0);
			List<Brydm> out = brydmService.queryAll();
			String object = JSONObject.toJSONString(out);
			writer.write(object);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				writer.close();
			}
		}
		return null;
    }
    
    /**
     * 分页查询图书(未完成)
     *
     * @return
     */
    public List<Btscg> pageFind() {   
		PrintWriter writer = null;
    	try {
			HttpServletResponse response = ServletActionContext.getResponse();		
			response.setContentType("application/json;charset=utf-8");
			writer = response.getWriter();
			response.setDateHeader("Expires", 0);
			List<Brydm> out = brydmService.queryAll();
			String object = JSONObject.toJSONString(out);
			writer.write(object);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				writer.close();
			}
		}
		return null;
    }
    
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

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

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public Date getPdate() {
		return pdate;
	}

	public void setPdate(String pdate) throws ParseException {
    	SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		this.pdate = sdf.parse(sdf.format(new Date(pdate)));
	}
	
//    @Override
//    public void validate() {
//         
//        if(pname == null || "".equals(pname.trim())) {
//            this.addFieldError("pname", "商品名称不能为空");
//        }
//         
//    }
     
}
最近下载更多
muoutang  LV11 2023年7月8日
poipoiiop  LV8 2023年1月3日
陈俊翰  LV7 2022年12月28日
齐吊大神  LV6 2022年12月20日
3305787467  LV12 2022年5月22日
辛星咖啡馆  LV2 2022年5月16日
微信网友_5911441934356480  LV1 2022年4月11日
lt33333  LV7 2022年4月9日
cx1111  LV1 2021年6月23日
shishizhouzhou  LV12 2021年6月10日
最近浏览更多
quartz  LV7 3月13日
WBelong  LV7 3月8日
1437047954 3月7日
暂无贡献等级
周青松  LV3 2023年12月10日
weilaizhisheng  LV21 2023年12月6日
新工艺  LV7 2023年9月22日
aaaaooa  LV4 2023年9月7日
8战魂5无双8  LV43 2023年8月19日
muoutang  LV11 2023年7月8日
kaisong 2023年6月24日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友