?? noticeaction.java
字號:
package com.demo.struts2.actions;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.demo.hibernate.beans.Notice;
import com.demo.hibernate.dao.NoticeDAO;
import com.demo.struts2.common.PageAction;
import com.demo.struts2.util.Constants;
import com.demo.struts2.util.Pager;
public class NoticeAction extends PageAction {
private static final long serialVersionUID = 1L;
private NoticeDAO noticeDAO;
protected String id = null;
protected String sender = null;
protected String title = null;
protected String content = null;
protected String sendtime = null;
// 新增或修改時執行表單驗證
public void validate() {
// 清除錯誤消息
clearErrorsAndMessages();
// 取得請求參數
String queryString = getRequestPath();
if (queryString.indexOf("noticeadd!insert") != -1
|| queryString.indexOf("noticeedit!update") != -1) {
// 檢查表單字段title
if (title == null || title.equals("")) {
addFieldError("title",getText("notice.error.title"));
}
// 檢查表單字段content
if (content == null || content.equals("")) {
addFieldError("content", getText("notice.error.content"));
}
}
}
// 請求noticeInit.do的處理函數
public String init() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// 重設分頁參數
super.pageSize = Constants.pageSize;
super.pageNo = Constants.pageNo;
// 取得當前分頁數據
super.pager = this.getNoticeDAO().findPager(super.pageSize, super.pageNo);
// 保存分頁數據
setSession(Constants.PAGER_NOTICE, super.pager);
return Constants.LIST_KEY;
}
// 請求noticeList.do的處理函數
public String list() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// 取得當前分頁數據
super.pager = this.getNoticeDAO().findPager(super.pageSize, super.pageNo);
// 保存分頁數據
setSession(Constants.PAGER_NOTICE, super.pager);
return Constants.LIST_KEY;
}
// 請求noticeAdd.do的處理函數
public String add() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// 重設各表單字段
reset();
return Constants.ADD_KEY;
}
// 重設各表單字段
private void reset() {
setId(null);
setSender(null);
setTitle(null);
setContent(null);
setSendtime(null);
}
// 給表單字段賦值
private void bean2Form(Notice notice) {
setId(notice.getId().toString());
setSender(notice.getSender());
setTitle(notice.getTitle());
setContent(notice.getContent());
setSendtime(notice.getSendtime());
}
// 請求noticeEdit.do的處理函數
public String edit() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// id為空時返回錯誤
if (this.getId() == null) {
saveActionError("notice.message.edit.notexist");
return Constants.LIST_KEY;
} else {
// 查詢數據表
Notice notice = this.getNoticeDAO().findById(id);
// 不存在時返回錯誤
if (notice == null) {
saveActionError("notice.message.edit.notexist");
return Constants.LIST_KEY;
} else {
// 給表單字段賦值
bean2Form(notice);
return Constants.EDIT_KEY;
}
}
}
// 請求noticeInsert.do的處理函數
public String insert() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// 插入數據表
Notice notice = new Notice();
notice.setSender(super.getLoginUsername());
notice.setTitle(this.getTitle());
notice.setContent(this.getContent());
notice.setSendtime(this.getSendtime());
this.getNoticeDAO().insert(notice);
// 取得緩存的分頁參數
Pager pagerSession = (Pager) getSession(Constants.PAGER_NOTICE);
super.pageSize = pagerSession.getPageSize();
super.pageNo = pagerSession.getPageNo();
// 查詢當前頁的數據
super.pager = this.getNoticeDAO().findPager(super.pageSize, super.pageNo);
// 保存成功信息
saveActionMessage("notice.message.add.success");
return Constants.LIST_KEY;
}
// 請求noticeUpdate.do的處理函數
public String update() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// 更新數據表
Notice notice = new Notice();
notice.setId(new Integer(id));
notice.setSender(super.getLoginUsername());
notice.setTitle(this.getTitle());
notice.setContent(this.getContent());
notice.setSendtime(this.getSendtime());
this.getNoticeDAO().update(notice);
// 給表單字段賦值
bean2Form(notice);
// 取得緩存的分頁參數
Pager pagerSession = (Pager) getSession(Constants.PAGER_NOTICE);
super.pageSize = pagerSession.getPageSize();
super.pageNo = pagerSession.getPageNo();
// 查詢當前頁的數據
super.pager = this.getNoticeDAO().findPager(super.pageSize, super.pageNo);
saveActionMessage("notice.message.edit.success");
return Constants.LIST_KEY;
}
// 請求noticeDelete.do的處理函數
public String delete() throws Exception {
// 清除錯誤消息
clearErrorsAndMessages();
// id為空時返回錯誤
if (this.getId() == null) {
saveActionError("notice.message.edit.notexist");
} else {
// 刪除數據
this.getNoticeDAO().delete(id);
saveActionMessage("notice.message.delete.success");
}
// 取得當前頁的數據
super.pager = this.getNoticeDAO().findPager(super.pageSize, super.pageNo);
return Constants.LIST_KEY;
}
public NoticeDAO getNoticeDAO() {
return noticeDAO;
}
public void setNoticeDAO(NoticeDAO noticeDAO) {
this.noticeDAO = noticeDAO;
}
public String getSendtime() {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sendtime = f.format(new Date());
return sendtime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public void setSendtime(String sendtime) {
this.sendtime = sendtime;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -