博客信息

struts实现crud

发布时间:『 2019-02-25 22:43』  博客类别:SSH  阅读(862)

1、定义baseAction,存放结果码常量,请求、响应、上下文、公用的传值

2、Struts标签的使用

s:iterator

S:action

S:url

S:form

s:textfield

S:select

S:radio

S:param

s:textarea


相关代码

public class Clazz {

	private Integer cid;
	private String cname;
	private String cteacher;

	public Integer getCid() {
		return cid;
	}

	public void setCid(Integer cid) {
		this.cid = cid;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public String getCteacher() {
		return cteacher;
	}

	public void setCteacher(String cteacher) {
		this.cteacher = cteacher;
	}

	public Clazz(Integer cid, String cname, String cteacher) {
		super();
		this.cid = cid;
		this.cname = cname;
		this.cteacher = cteacher;
	}

	public Clazz() {
		super();
	}

	@Override
	public String toString() {
		return "Clazz [cid=" + cid + ", cname=" + cname + ", cteacher=" + cteacher + "]";
	}

}


public class Student {

	private Integer sid;
	private String sname;
	private String spin;

	// radio,checkbox,select
	private String sex;
	private String mark;
	private Integer cid;
	
	private String cname;

	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
		this.spin = PinYinUtil.toPinyin(this.sname);
	}

	public String getSpin() {
		return spin;
	}

	public void setSpin(String spin) {
		this.spin = spin;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public Integer getCid() {
		return cid;
	}

	public void setCid(Integer cid) {
		this.cid = cid;
	}

	public Student(Integer sid, String sname, String spin, String sex, String mark, Integer cid) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.spin = spin;
		this.sex = sex;
		this.mark = mark;
		this.cid = cid;
	}

	public Student() {
		super();
	}

	@Override
	public String toString() {
		return "Student [sid=" + sid + ", sname=" + sname + ", spin=" + spin + ", sex=" + sex + ", mark=" + mark
				+ ", cid=" + cid + ", clazzName=" + cname + "]";
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public Student(Integer sid, String sname, String spin, String sex, String mark, Integer cid, String cname) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.spin = spin;
		this.sex = sex;
		this.mark = mark;
		this.cid = cid;
		this.cname = cname;
	}

}


public class ClazzDAO extends EntityBaseDao<Clazz> {

	public ClazzDAO() {
		super();
	}

	public List<Clazz> list() throws InstantiationException, IllegalAccessException, SQLException {
		String sql = "select * from t_struts_class where 1=1";
		return super.executeQuery(sql, new PageBean(), Clazz.class);
	}
	
	public static void main(String[] args) {
		try {
			System.out.println(new ClazzDAO().list());
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


public class StudentDAO extends EntityBaseDao<Student> {

	public void add(Student student) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
		String sql = "insert into t_struts_student(sid,sname,spin,sex,mark,cid) values(?,?,?,?,?,?)";
		super.executeUpdate(sql, new String[] {"sid","sname","spin","sex","mark","cid"}, student);
	}

	public void edit(Student student) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
		String sql = "update t_struts_student set sname=?,spin=?,sex=?,mark=?,cid=? where sid=?";
		super.executeUpdate(sql, new String[] {"sname","spin","sex","mark","cid","sid"}, student);
	}

	public void del(Student student) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
		String sql = "delete from t_struts_student where sid=?";
		super.executeUpdate(sql, new String[] {"sid"}, student);
	}

	public Student load(Student student) throws InstantiationException, IllegalAccessException, SQLException {
		String sql = "select s.*, c.cname from t_struts_student s inner join t_struts_class c on c.cid = s.cid where s.sid="+student.getSid();
		return super.executeQuery(sql, new PageBean(), Student.class).get(0);
	}

	public List<Student> list(Student student, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException {
		String sql = "select s.*, c.cname from t_struts_student s inner join t_struts_class c on c.cid = s.cid where true ";

		if (StringUtils.isNotBlank(student.getSname())) {
			sql += " and (s.sname like '%" + student.getSname().trim()
					+ "%' or s.spin like '%" + student.getSname().trim() + "%')";
		}
		if (null != student.getCid() && !new Integer(-1).equals(student.getCid())) {
			sql += " and s.cid = " + student.getCid();
		}
		System.out.println(sql);

		return super.executeQuery(sql, pageBean, Student.class);
	}

}


/**
 * 通用的action
 * 	1、处理传值
 * 	2、结果码的配置
 * 	3、想客户端返回提示信息
 * @author Administrator
 *
 */
public class BaseAction implements ServletRequestAware,ServletResponseAware{
	protected HttpServletResponse response;
	protected HttpServletRequest request;
	protected HttpSession session;
	protected ServletContext application;
	
	protected final String SUCCCESS = "succcess";
	protected final String FAIL = "fail";
	protected final String LIST = "list";
	protected final String PREADD = "preAdd";
	protected final String PREEDIT = "preEdit";
	protected final String TOLIST = "toList";
	protected final String DETAIL = "detail";
	
	protected Object result;
//	执行成功(msgInfo:错误信息,handle:解决办法)
	protected Object msg;
	protected int code;
	
	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	public Object getMsg() {
		return msg;
	}

	public void setMsg(Object msg) {
		this.msg = msg;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.session = request.getSession();
		this.application = request.getServletContext();
	}
	

}


public class ClzAction extends BaseAction{
	public String execute() {
		try {
			this.result = new ClazzDAO().list();
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}


public class StudentAction extends BaseAction implements ModelDriven<Student>{
	private Student student = new Student();
	private StudentDAO studentDao=new StudentDAO();
	
	public String list() {
		try {
			this.result = this.studentDao.list(student, new PageBean());
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			e.printStackTrace();
		}
		return this.LIST;
	}
	public String preAdd() {
		return PREADD;
	}
	public String add() {
		try {
			this.studentDao.add(student);
		} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			e.printStackTrace();
		}
		return TOLIST;
	}
	public String preEdit() {
		try {
			this.result = this.studentDao.load(student);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			e.printStackTrace();
		}
		return PREEDIT;
	}
	public String edit() {
		try {
			this.studentDao.edit(student);
		} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			e.printStackTrace();
		}
		return TOLIST;
	}
	public String del() {
		try {
			this.studentDao.del(student);
		} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			e.printStackTrace();
		}
		return TOLIST;
	}

	@Override
	public Student getModel() {
		return student;
	}

}


小李飞刀_struts小李飞刀_struts小李飞刀_struts小李飞刀_struts



关键字:     struts  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有