?? scheduleaction.java
字號:
package com.demo.struts2.actions;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.demo.hibernate.beans.Schedule;
import com.demo.hibernate.dao.ScheduleDAO;
import com.demo.struts2.common.PageAction;
import com.demo.struts2.util.Constants;
import com.demo.struts2.util.Pager;
import com.demo.struts2.util.RegExpression;
public class ScheduleAction extends PageAction {
private static final long serialVersionUID = 1L;
private ScheduleDAO scheduleDAO;
protected String id = null;
protected String username = null;
protected String year = null;
protected String month = null;
protected String day = null;
protected String plan = null;
// 新增或修改時執行表單驗證
public void validate() {
// 清除錯誤消息
clearErrorsAndMessages();
// 取得請求參數
String queryString = getRequestPath();
if (queryString.indexOf("scheduleadd!insert") != -1
|| queryString.indexOf("scheduleedit!update") != -1) {
// 檢查表單字段year
Pattern p_year = Pattern.compile(RegExpression.REG_year);
Matcher m_year = p_year.matcher(year);
if (!m_year.find()) {
addFieldError("year", getText("schedule.error.year"));
}
// 檢查表單字段month
Pattern p_month = Pattern.compile(RegExpression.REG_month);
Matcher m_month = p_month.matcher(month);
if (!m_month.find()) {
addFieldError("month", getText("schedule.error.month"));
}
// 檢查表單字段day
Pattern p_day = Pattern.compile(RegExpression.REG_day);
Matcher m_day = p_day.matcher(day);
if (!m_day.find()) {
addFieldError("day", getText("schedule.error.day"));
}
// 檢查表單字段plan
if (plan == null || plan.equals("")) {
addFieldError("plan", getText("schedule.error.plan"));
}
}
}
// 請求scheduleInit.do的處理函數
public String init() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// 重設分頁參數
super.pageSize = Constants.pageSize;
super.pageNo = Constants.pageNo;
// 取得當前分頁數據
super.pager = this.getScheduleDAO().findPagerByUsername(super
.getLoginUsername(), super.pageSize, super.pageNo);
// 保存分頁數據
setSession(Constants.PAGER_SCHEDULE, super.pager);
return Constants.LIST_KEY;
}
// 請求scheduleList.do的處理函數
public String list() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// 取得當前分頁數據
super.pager = this.getScheduleDAO().findPagerByUsername(super
.getLoginUsername(), super.pageSize, super.pageNo);
// 保存分頁數據
setSession(Constants.PAGER_SCHEDULE, super.pager);
return Constants.LIST_KEY;
}
// 請求scheduleAdd.do的處理函數
public String add() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// 重設各表單字段
reset();
return Constants.ADD_KEY;
}
// 重設各表單字段
private void reset() {
setId(null);
setUsername(null);
setYear(null);
setMonth(null);
setDay(null);
setPlan(null);
}
// 給表單字段賦值
private void bean2Form(Schedule schedule) {
setId(schedule.getId().toString());
setUsername(schedule.getUsername());
setYear(schedule.getYear().toString());
setMonth(schedule.getMonth().toString());
setDay(schedule.getDay().toString());
setPlan(schedule.getPlan());
}
// 請求scheduleEdit.do的處理函數
public String edit() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// id為空時返回錯誤
if (this.getId() == null) {
saveActionError("schedule.message.edit.notexist");
return Constants.LIST_KEY;
} else {
// 查詢數據表
Schedule schedule = this.getScheduleDAO().findById(id);
// 不存在時返回錯誤
if (schedule == null) {
saveActionError("schedule.message.edit.notexist");
return Constants.LIST_KEY;
} else {
// 給表單字段賦值
bean2Form(schedule);
return Constants.EDIT_KEY;
}
}
}
// 請求scheduleInsert.do的處理函數
public String insert() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// 插入數據表
Schedule schedule = new Schedule();
schedule.setUsername(super.getLoginUsername());
schedule.setYear(Integer.valueOf(this.getYear()));
schedule.setMonth(Integer.valueOf(this.getMonth()));
schedule.setDay(Integer.valueOf(this.getDay()));
schedule.setPlan(this.getPlan());
this.getScheduleDAO().insert(schedule);
// 取得緩存的分頁參數
Pager pagerSession = (Pager) getSession(Constants.PAGER_SCHEDULE);
super.pageSize = pagerSession.getPageSize();
super.pageNo = pagerSession.getPageNo();
// 查詢當前頁的數據
super.pager = this.getScheduleDAO().findPagerByUsername(super
.getLoginUsername(), super.pageSize, super.pageNo);
// 保存成功信息
saveActionMessage("schedule.message.add.success");
return Constants.LIST_KEY;
}
// 請求scheduleUpdate.do的處理函數
public String update() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// 更新數據表
Schedule schedule = new Schedule();
schedule.setId(new Integer(id));
schedule.setUsername(super.getLoginUsername());
schedule.setYear(Integer.valueOf(this.getYear()));
schedule.setMonth(Integer.valueOf(this.getMonth()));
schedule.setDay(Integer.valueOf(this.getDay()));
schedule.setPlan(this.getPlan());
this.getScheduleDAO().update(schedule);
// 給表單字段賦值
bean2Form(schedule);
// 取得緩存的分頁參數
Pager pagerSession = (Pager) getSession(Constants.PAGER_SCHEDULE);
super.pageSize = pagerSession.getPageSize();
super.pageNo = pagerSession.getPageNo();
// 查詢當前頁的數據
super.pager = this.getScheduleDAO().findPagerByUsername(super
.getLoginUsername(), super.pageSize, super.pageNo);
saveActionMessage("schedule.message.edit.success");
return Constants.LIST_KEY;
}
// 請求scheduleDelete.do的處理函數
public String delete() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// id為空時返回錯誤
if (this.getId() == null) {
saveActionError("schedule.message.edit.notexist");
} else {
// 刪除數據
scheduleDAO.delete(id);
saveActionMessage("schedule.message.delete.success");
}
// 取得當前頁的數據
super.pager = this.getScheduleDAO().findPagerByUsername(super
.getLoginUsername(), super.pageSize, super.pageNo);
return Constants.LIST_KEY;
}
public ScheduleDAO getScheduleDAO() {
return scheduleDAO;
}
public void setScheduleDAO(ScheduleDAO scheduleDAO) {
this.scheduleDAO = scheduleDAO;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String getPlan() {
return plan;
}
public void setPlan(String plan) {
this.plan = plan;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -