实例介绍
【实例简介】
一款简单的挂号系统
【实例截图】



【核心代码】
package org.com.action;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.com.dao.PatientDao;
import org.com.dao.UserDao;
import org.com.model.PageBean;
import org.com.model.Patient;
import org.com.model.User;
import org.com.util.DateUtil;
import org.com.util.DbUtil;
import org.com.util.JsonUtil;
import org.com.util.ResponseUtil;
import org.com.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;
public class PatientAction extends ActionSupport implements ServletRequestAware{
private Patient patient;//patient对象
private String page;//分页数据
private String rows;//分页数据
private String delIds;//批量删除数据的序列号
private String patientId;//修改患者资料传递的患者编号
//查询条件的变量
private String s_patientName;
private String s_sex;
private String s_bbirthday;
private String s_ebirthday;
private String s_userId;
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getRows() {
return rows;
}
public void setRows(String rows) {
this.rows = rows;
}
public Patient getPatient() {
return patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
public String getDelIds() {
return delIds;
}
public void setDelIds(String delIds) {
this.delIds = delIds;
}
public String getPatientId() {
return patientId;
}
public void setPatientId(String patientId) {
this.patientId = patientId;
}
public String getS_patientName() {
return s_patientName;
}
public void setS_patientName(String s_patientName) {
this.s_patientName = s_patientName;
}
public String getS_sex() {
return s_sex;
}
public void setS_sex(String s_sex) {
this.s_sex = s_sex;
}
public String getS_bbirthday() {
return s_bbirthday;
}
public void setS_bbirthday(String s_bbirthday) {
this.s_bbirthday = s_bbirthday;
}
public String getS_ebirthday() {
return s_ebirthday;
}
public void setS_ebirthday(String s_ebirthday) {
this.s_ebirthday = s_ebirthday;
}
public String getS_userId() {
return s_userId;
}
public void setS_userId(String s_userId) {
this.s_userId = s_userId;
}
DbUtil dbUtil=new DbUtil();
UserDao userDao=new UserDao();
PatientDao patientDao=new PatientDao();
HttpServletRequest request;
//当前用户
User currentUser;
@Override
public String execute() throws Exception {
// 获取Session
HttpSession session=request.getSession();
//链接connection
Connection con=null;
PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));//分页pageBean
if(patient==null){
patient=new Patient();
}
//session中获取当前用户的ID 只查询当前用户增加的患者 只有管理员才全部查询
currentUser=(User) session.getAttribute("currentUser");
if(currentUser.getRole()!=0){
//非管理员用户赋值,显示所属用户患者,管理员不赋值查询所有结果
patient.setUserId(currentUser.getUserId());
}
//将查询条件赋值给patient对象
if(s_patientName!=null){
patient.setPatientName(s_patientName);
patient.setSex(s_sex);
if(StringUtil.isNotEmpty(s_userId)){
patient.setUserId(Integer.parseInt(s_userId));
}
}
try{
con=dbUtil.getCon();
JSONObject result=new JSONObject();
JSONArray jsonArray=JsonUtil.formatRsToJsonArray(patientDao.patientList(con, pageBean,patient,s_bbirthday,s_ebirthday));
int total=patientDao.patientCount(con,patient,s_bbirthday,s_ebirthday);
result.put("rows", jsonArray);
result.put("total", total);
ResponseUtil.write(ServletActionContext.getResponse(), result);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
//保存患者数据(新增和修改)
public String save()throws Exception{
//判断patientId是否为空
if(StringUtil.isNotEmpty(patientId)){
patient.setPatientId(Integer.parseInt(patientId));
}
Connection con=null;
try{
con=dbUtil.getCon();
int saveNums=0;
JSONObject result=new JSONObject();
if(StringUtil.isNotEmpty(patientId)){
//不为空,修改患者信息
saveNums=patientDao.patientModify(con, patient);
}else{
//patientId为空,新增患者
saveNums=patientDao.patientAdd(con, patient);
}
if(saveNums>0){
result.put("success", "true");
}else{
result.put("success", "true");//业务逻辑,需要返回success,但返回的是错误message
result.put("errorMsg", "保存失败");
}
ResponseUtil.write(ServletActionContext.getResponse(), result);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
//删除数据
public String delete()throws Exception{
Connection con=null;
try{
con=dbUtil.getCon();
JSONObject result=new JSONObject();
int delNums=patientDao.patientDelete(con, delIds);
if(delNums>0){
result.put("success", "true");
result.put("delNums", delNums);
}else{
result.put("errorMsg", "删除失败");
}
ResponseUtil.write(ServletActionContext.getResponse(), result);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
//下拉框赋值 如果是管理员可以查询所有用户,如果是用户只能显示当前用户
public String userNameComboList()throws Exception{
Connection con=null;
// 获取Session,判断当前用户类型
HttpSession session=request.getSession();
currentUser=(User) session.getAttribute("currentUser");
if(currentUser.getRole()!=0){
//普通用户显示当前用户
//除了管理员以外的账号只查询该账号的患者
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("userId", "");
jsonObject.put("userName", "请选择...");
jsonArray.add(jsonObject);
jsonObject.put("userId", currentUser.getUserId());
jsonObject.put("userName", currentUser.getUserName());
jsonArray.add(jsonObject);
ResponseUtil.write(ServletActionContext.getResponse(), jsonArray);
}else{
//管理员用户
try{
con=dbUtil.getCon();
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("userId", "");
jsonObject.put("userName", "请选择...");
jsonArray.add(jsonObject);
jsonArray.addAll(JsonUtil.formatRsToJsonArray(userDao.userList(con)));
ResponseUtil.write(ServletActionContext.getResponse(), jsonArray);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
//下拉框赋值 如果是管理员可以查询所有用户的患者,如果是用户只能显示当前用户患者
public String patientNameComboList()throws Exception{
Connection con=null;
if(patient==null){
patient=new Patient();
}
// 获取Session,判断当前用户类型
HttpSession session=request.getSession();
currentUser=(User) session.getAttribute("currentUser");
con=dbUtil.getCon();
//无论当前什么用户,都需要在combobox中添加如下的jsonObject
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("patientId", "");
jsonObject.put("patientName", "请选择...");
jsonArray.add(jsonObject);
if(currentUser.getRole()!=0){
try{
//普通用户显示当前用户
//除了管理员以外的账号只查询该账号的患者
//设置非管理员用户id给patient
patient.setUserId(currentUser.getUserId());
jsonArray.addAll(JsonUtil.formatRsToJsonArray(patientDao.patientList(con,null, patient, null, null)));
ResponseUtil.write(ServletActionContext.getResponse(), jsonArray);
}catch(Exception e){
e.printStackTrace();
}
}else{
//管理员用户
try{
jsonArray.addAll(JsonUtil.formatRsToJsonArray(patientDao.patientList(con,null,patient,null,null)));
ResponseUtil.write(ServletActionContext.getResponse(), jsonArray);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
//输出excel表格(xls)
public void exportPatient() throws Exception{
// 创建Excel的工作书册 Workbook,对应到一个excel文档
HSSFWorkbook wb = new HSSFWorkbook();
// 创建Excel的工作sheet,对应到一个excel文档的tab
HSSFSheet sheet = wb.createSheet("sheet1");
// 设置excel每列宽度
sheet.setColumnWidth(0, 4000);
sheet.setColumnWidth(1, 3500);
// 创建字体样式
HSSFFont font = wb.createFont();
font.setFontName("Verdana");
font.setBoldweight((short) 100);
font.setFontHeight((short) 300);
font.setColor(HSSFColor.BLUE.index);
// 创建单元格样式
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// 设置边框
style.setBottomBorderColor(HSSFColor.RED.index);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setFont(font);// 设置字体
style.setWrapText(true);// 自动换行
// 创建Excel的sheet的一行
HSSFRow row = sheet.createRow(0);
row.setHeight((short) 500);// 设定行的高度
HSSFCell cell=null;
// 创建一个Excel的单元格
String[] cellTitle = {"编号", "患者姓名", "性别","出生日期", "身份证号", "联系电话","家庭住址","所属用户"};
for (int i = 0; i < cellTitle.length; i ) {
cell = row.createCell(i);
// 给Excel的单元格设置样式和赋值
cell.setCellStyle(style);
cell.setCellValue(cellTitle[i]);
}
// 获取Session
HttpSession session=request.getSession();
//链接connection
Connection con=null;
if(patient==null){
patient=new Patient();
}
//session中获取当前用户的ID 只查询当前用户增加的患者 只有管理员才全部查询
currentUser=(User) session.getAttribute("currentUser");
if(currentUser.getRole()!=0){
//非管理员用户赋值,显示所属用户患者,管理员不赋值查询所有结果
patient.setUserId(currentUser.getUserId());
}
//将查询条件赋值给patient对象
if(s_patientName!=null){
patient.setPatientName(s_patientName);
patient.setSex(s_sex);
if(StringUtil.isNotEmpty(s_userId)){
patient.setUserId(Integer.parseInt(s_userId));
}
}
ResultSet rs=null;
try{
con=dbUtil.getCon();
rs=patientDao.patientList(con, null,patient,s_bbirthday,s_ebirthday);
int rowIndex=1;
while(rs.next()){
row = sheet.createRow(rowIndex );
cell = row.createCell(0);
cell.setCellValue(rs.getInt("patientId"));
cell = row.createCell(1);
cell.setCellValue(rs.getString("patientName"));
cell = row.createCell(2);
cell.setCellValue(rs.getString("sex"));
cell = row.createCell(3);
cell.setCellValue(rs.getString("birthday"));
cell = row.createCell(4);
cell.setCellValue(rs.getString("idNumber"));
cell = row.createCell(5);
cell.setCellValue(rs.getString("tel"));
cell = row.createCell(6);
cell.setCellValue(rs.getString("patientDesc"));
cell = row.createCell(7);
cell.setCellValue(rs.getString("userName"));
}
String exportFileName = "patient.xls";
ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" new String((exportFileName).getBytes(), "ISO8859-1"));//设定输出文件头
ServletActionContext.getResponse().setContentType("application/vnd.ms-excel;charset=UTF-8");// 定义输出类型
OutputStream out = ServletActionContext.getResponse().getOutputStream();
wb.write(out);
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
}
标签: 系统
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论