package com.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.alibaba.fastjson.JSONArray;
import com.dto.Doc;
import com.dto.Doctor;
import com.service.DoctorService;
import com.util.PageUtils;
public class Action {
private DoctorService service = new DoctorService();
private List<Doctor> list;
private PageUtils pu;
private Integer cpage;
private Doctor doctor;
private Doc d;
//默认调用的方法
public String execute(){
//获得集合
if(cpage == null){
cpage = 1;
}
Integer pageSize = 3;
int count = service.getCount();
System.out.println(count);
pu = new PageUtils(cpage, pageSize, count);
list = service.getList(pu);
return "tolist";
}
public void show() throws IOException{
doctor = service.show(doctor);
System.out.println(doctor);
String jsonString = JSONArray.toJSONString(doctor);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.getWriter().write(jsonString);
}
public void doUpd() throws IOException{
System.out.println(d);
int i = service.doUpd(d);
System.out.println(d);
System.out.println(i);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.getWriter().print(i);
}
public void doAdd() throws IOException{
System.out.println(d);
int i = service.doAdd(d);
System.out.println(d);
System.out.println(i);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.getWriter().print(i);
}
public String del(){
int i = service.del(d);
System.out.println(i);
return "todoctor";
}
public List<Doctor> getList() {
return list;
}
public void setList(List<Doctor> list) {
this.list = list;
}
public PageUtils getPu() {
return pu;
}
public void setPu(PageUtils pu) {
this.pu = pu;
}
public Integer getCpage() {
return cpage;
}
public void setCpage(Integer cpage) {
this.cpage = cpage;
}
public Doctor getDoctor() {
return doctor;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
public Doc getD() {
return d;
}
public void setD(Doc d) {
this.d = d;
}
}
package com.dao;
import java.util.List;
import com.dto.Doc;
import com.dto.Doctor;
import com.util.JdbcUtils;
import com.util.PageUtils;
public class DoctorDao {
public List<Doctor> getList(PageUtils pu) {
// TODO Auto-generated method stub
String sql = " select a.*,b.deptname,b.deptcontent,b.deptdate from t_doctor a left join t_dept b on a.did = b.did limit "+pu.getStartIndex()+","+pu.getPageSize();
System.out.println(sql);
return JdbcUtils.getList(Doctor.class, sql);
}
public int getCount() {
// TODO Auto-generated method stub
String sql = " select count(*) from t_doctor ";
System.out.println(sql);
return JdbcUtils.getCount(sql);
}
public Doctor show(Doctor doctor) {
// TODO Auto-generated method stub
String sql = " select a.*,b.deptname,b.deptcontent,b.deptdate from t_doctor a left join t_dept b on a.did = b.did where a.id="+doctor.getId();
System.out.println(sql);
return (Doctor) JdbcUtils.getOneObject(Doctor.class, sql);
}
public int doUpd(Doc d) {
// TODO Auto-generated method stub
String sql = " update t_doctor set sex='"+d.getSex()+"',birth='"+d.getBirth()+"',tel='"+d.getTel()+"',ems='"+d.getEms()+"',email='"+d.getEmail()+"',datea='"+d.getDatea()+"',content='"+d.getContent()+"',address='"+d.getAddress()+"',name='"+d.getName()+"',did="+d.getDid()+" where id="+d.getId();
System.out.println(sql);
return JdbcUtils.executeSQL(sql);
}
public int doAdd(Doc d) {
// TODO Auto-generated method stub
String sql = " insert into t_doctor set id=null,name='"+d.getName()+"',sex='"+d.getSex()+"',birth='"+d.getBirth()+"',hobby='"+d.getHobby()+"',tel='"+d.getTel()+"',ems='"+d.getEms()+"',email='"+d.getEmail()+"',datea='"+d.getDatea()+"',content='"+d.getContent()+"',address='"+d.getAddress()+"',did='"+d.getDid()+"' ";
System.out.println(sql);
return JdbcUtils.executeSQL(sql);
}
public int del(Doc d) {
// TODO Auto-generated method stub
String sql = " delete from t_doctor where id ="+d.getId();
return JdbcUtils.executeSQL(sql);
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'upd.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript"> var id = ${param.id}; $.ajax({ url:"doctor!show", type:"post", data:{"doctor.id":id}, dataType:"json", success:function (msg){ $("[name='d.id']").val(msg.id); $("[name='d.name']").val(msg.name); $("[name='d.sex']").val(msg.sex); $("[name='d.birth']").val(msg.birth); var hobby = msg.hobby.split(","); for ( var i in hobby) { $("[name='d.hobby'][value="+hobby[i].trim()+"]").attr("checked",true); } $("[name='d.tel']").val(msg.tel); $("[name='d.ems']").val(msg.ems); $("[name='d.email']").val(msg.email); $("[name='d.datea']").val(msg.datea); $("[name='d.content']").val(msg.content); $("[name='d.address']").val(msg.address); $("[name='d.did']").val(msg.did); } }); function doUpd(){ $.ajax({ url:"doctor!doUpd", type:"post", dataType:"text", data:$("form").serialize(), success:function (msg){ if(msg>0){ location="doctor"; }else{ alert("修改失败"); } } }); } </script>
</head>
<body>
<form>
<input name="d.id" type="hidden"><br>
name:<input name="d.name"><br>
sex:<input name="d.sex"><br>
birth:<input name="d.birth"><br>
hobby:<input name="d.hobby" type="checkbox" value="睡觉">睡觉
<input name="d.hobby" type="checkbox" value="唱歌">唱歌<br>
tel:<input name="d.tel"><br>
ems:<input name="d.ems"><br>
email:<input name="d.email"><br>
datea:<input name="d.datea"><br>
content:<input name="d.content"><br>
address:<input name="d.address"><br>
did:<input name="d.did"><br>
<input type="button" value="修改" onclick="doUpd()">
</form>
</body>
</html>
认认真真地做一种事业,然后凭自己的兴趣读世上一切有趣的书
——黄永玉《沿着塞纳河到翡冷翠》