📄 addressbookaction.java
字号:
package addressbook.action;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
import addressbook.db.*;
import addressbook.bean.*;
/**
* @version 2007-3-14
* @author Xiaofei
*/
public class AddressBookAction extends DispatchAction {
//浏览记录
protected ActionForward unspecified(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String target = "view";
PageBean pageBean = new PageBean();
int page = 1;
try{
page = Integer.parseInt(request.getParameter("jumpPage"));
}catch(NumberFormatException nfe){}
pageBean.setCurPage(page);
new RecordDAO().query(pageBean,"");
request.setAttribute("pageBean",pageBean);
return mapping.findForward(target);
}
//查询记录
public ActionForward select(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String target = "select";
PageBean pageBean = new PageBean();
int page = 1;
try{
page = Integer.parseInt(request.getParameter("jumpPage"));
}catch(NumberFormatException nfe){}
pageBean.setCurPage(page);
//构造查询条件
String conditions;
String keyword = request.getParameter("keyword");
if((keyword!=null)&&(!keyword.trim().equals(""))){
String field = request.getParameter("field");
conditions = " where "+field+" like '%"+keyword.trim()+"%' ";
}else{
conditions = "";
}
new RecordDAO().query(pageBean,conditions);
if(pageBean.getData().size()>0){
request.setAttribute("haveRecords",Boolean.TRUE);
request.setAttribute("pageBean",pageBean);
}else{
request.setAttribute("haveRecords",Boolean.FALSE);
}
return mapping.findForward(target);
}
//添加记录
public ActionForward append(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String target = "prompt";
Record record = (Record)form;
new RecordDAO().append(record);
request.setAttribute("actionOk",record.getName()+" 添加成功");
return mapping.findForward(target);
}
//删除记录
public ActionForward delete(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String target = "prompt";
int id = Integer.parseInt(request.getParameter("id"));
new RecordDAO().delete(id);
request.setAttribute("actionOk","记录删除成功");
return mapping.findForward(target);
}
//准备编辑
public ActionForward edit(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String target = "edit";
int id = Integer.parseInt(request.getParameter("id"));
request.setAttribute("record",new RecordDAO().getRecordById(id));
return mapping.findForward(target);
}
//更改记录
public ActionForward update(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String target = "prompt";
Record record = (Record)form;
new RecordDAO().update(record);
request.setAttribute("actionOk",record.getName()+" 修改成功");
return mapping.findForward(target);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -