et.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.edainfo.db.DAO;
import net.edainfo.util.ActionBase;
import net.edainfo.util.EdaGlobals;
import net.edainfo.util.FormParameter;
import net.edainfo.util.page.PageList;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
/**
* @author slf
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ExampleAction extends ActionBase {
public ActionForward executeAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response, Map params)
throws Exception {
DynaValidatorForm theForm = (DynaValidatorForm) form;
String next = "";
conn = this.getConnection();
String operation = (String) theForm.get("operation");
DAO dao = new DAO(conn, params.get(EdaGlobals.DATABASE_TYPE_KEY)
+ "");
if (operation == null || operation.equals(""))
operation = "list";
if (operation.equals("list")) {
ExampleModel model = new ExampleModel((Map) params
.get(EdaGlobals.DATA_BASE_KEY));
String pageNum = (String) theForm.get("pageNum");
if (pageNum == null || pageNum.equals("")) {
pageNum = "1";
}
theForm.set("pageNum" ,pageNum);
int pageSize = model.getPageSize("fore");
int viewPage = model.getViewPage("fore");
PageList lists = dao.select(model, "", new ArrayList(), "", Integer
.parseInt(pageNum), pageSize, viewPage, dao.DISTINCT_OFF);
Map parms = FormParameter.setListPage (theForm ,lists ,"example" ,
request);
request.setAttribute ("parms" ,parms);
next = "list";
} else if(operation.equals("add")) {
ExampleModel model = new ExampleModel((Map) params
.get(EdaGlobals.DATA_BASE_KEY));
model = (ExampleModel)FormParameter.getParameter (model ,theForm);
model.setId(dao.generateId());
dao.create(model);
next = "add";
} else if(operation.equals("addform")) {
next = "addform";
theForm.set("operation" ,"add");
}
return mapping.findForward(next);
}
}
然后是建立jsp页面,这里有两个页面,一个是浏览页,一个是表单页;
浏览页,listExample.jsp:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%
String linkPage = (String)request.getAttribute("linkPage");
%>
<html>
import javax.servlet.http.HttpServletResponse;
import net.edainfo.db.DAO;
import net.edainfo.util.ActionBase;
import net.edainfo.util.EdaGlobals;
import net.edainfo.util.FormParameter;
import net.edainfo.util.page.PageList;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
/**
* @author slf
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ExampleAction extends ActionBase {
public ActionForward executeAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response, Map params)
throws Exception {
DynaValidatorForm theForm = (DynaValidatorForm) form;
String next = "";
conn = this.getConnection();
String operation = (String) theForm.get("operation");
DAO dao = new DAO(conn, params.get(EdaGlobals.DATABASE_TYPE_KEY)
+ "");
if (operation == null || operation.equals(""))
operation = "list";
if (operation.equals("list")) {
ExampleModel model = new ExampleModel((Map) params
.get(EdaGlobals.DATA_BASE_KEY));
String pageNum = (String) theForm.get("pageNum");
if (pageNum == null || pageNum.equals("")) {
pageNum = "1";
}
theForm.set("pageNum" ,pageNum);
int pageSize = model.getPageSize("fore");
int viewPage = model.getViewPage("fore");
PageList lists = dao.select(model, "", new ArrayList(), "", Integer
.parseInt(pageNum), pageSize, viewPage, dao.DISTINCT_OFF);
Map parms = FormParameter.setListPage (theForm ,lists ,"example" ,
request);
request.setAttribute ("parms" ,parms);
next = "list";
} else if(operation.equals("add")) {
ExampleModel model = new ExampleModel((Map) params
.get(EdaGlobals.DATA_BASE_KEY));
model = (ExampleModel)FormParameter.getParameter (model ,theForm);
model.setId(dao.generateId());
dao.create(model);
next = "add";
} else if(operation.equals("addform")) {
next = "addform";
theForm.set("operation" ,"add");
}
return mapping.findForward(next);
}
}
然后是建立jsp页面,这里有两个页面,一个是浏览页,一个是表单页;
浏览页,listExample.jsp:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%
String linkPage = (String)request.getAttribute("linkPage");
%>
<html>
| 对此文章发表了评论 |
