📄 hteaaction.java
字号:
package math.htea.controller;
import java.util.List;
import javax.activation.DataSource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import math.htea.dao.HteaDAO;
import math.htea.model.Htea;
import math.news.DAO.NewsDAO;
import math.util.Pager;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class HteaAction extends Action {
private org.apache.commons.logging.Log __log = LogFactory.getFactory()
.getInstance(this.getClass());
private javax.sql.DataSource ds = null;
// ִ��
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ds = getDataSource(request);
ActionForward myforward = null;
String myaction = mapping.getParameter();
if (isCancelled(request)) {
if (__log.isInfoEnabled()) {
__log.info(" [Htea] " + mapping.getAttribute()
+ " - action was cancelled");
}
return mapping.findForward("cancel");
}
if (__log.isInfoEnabled()) {
__log.info(" [Htea] action: " + myaction);
}
if ("".equalsIgnoreCase(myaction)) {
myforward = mapping.findForward("error");
} else if ("HTEAADD".equalsIgnoreCase(myaction)) {
myforward = performHteaAdd(mapping, form, request, response);
} else if ("HTEASELECT".equalsIgnoreCase(myaction)) {
myforward = performHteaList(mapping, form, request, response);
} else if ("HTEACHECKOUT".equalsIgnoreCase(myaction)) {
myforward = performHteaCheck(mapping, form, request, response);
} else if ("HTEADELETE".equalsIgnoreCase(myaction)) {
myforward = performHteaDelete(mapping, form, request, response);
}
else if ("HTEAGET".equalsIgnoreCase(myaction)) {
myforward = performHteaSelect(mapping, form, request, response);
} else if ("HTEASAVE".equalsIgnoreCase(myaction)) {
myforward = performHteaSave(mapping, form, request, response);
}
/*
* else if ("HTEADELETE".equalsIgnoreCase(myaction)) { myforward =
* performHteaDelete(mapping, form, request, response); } else if
* ("CHECKOUT".equalsIgnoreCase(myaction)) { myforward =
* performHteaCheck(mapping, form, request, response); } else if
* ("HteaSHOW".equalsIgnoreCase(myaction)) { myforward =
* performHteaShow(mapping, form, request, response); }
*/
return myforward;
}
private ActionForward performHteaAdd(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Htea htea = new Htea();
HteaForm hteaForm = (HteaForm) actionForm;
BeanUtils.populate(htea, BeanUtils.describe(hteaForm));
HteaDAO hteaDAO = new HteaDAO(ds);
if (hteaDAO.headInsert(htea))
return mapping.findForward("success");
else
return mapping.findForward("error");
}
private ActionForward performHteaList(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
try {
HttpSession session = request.getSession();
String condition;
HteaDAO hteaDAO = new HteaDAO(ds);
int offset;
int length = 10;
String pagerOffset = request.getParameter("pager.offset");
if (pagerOffset == null || pagerOffset.equals("")) {
offset = 0;
HteaForm hteaForm = (HteaForm) actionForm;
String s = hteaForm.getHtea_title();
if (s == null) {
s = "";
}
condition =s;;
session.setAttribute("CONDITION", condition);
} else {
offset = Integer.parseInt(pagerOffset);
condition = (String) session.getAttribute("CONDITION");
if (condition == null) {
condition = "";
}
}
List newslist = hteaDAO.list(offset, length, condition);
int size = hteaDAO.getSize("htea", "where htea_title like '%" + condition + "%'");
String url = request.getContextPath() + mapping.getPath() + ".do";
String pagerHeader = Pager.generate(offset, size, length, url);
request.setAttribute("pagerHeader", pagerHeader);
request.setAttribute("LIST", newslist);
return mapping.findForward("success");
} catch (Exception e) {
generalError(request, e);
return mapping.findForward("error");
}
}
private ActionForward performHteaCheck(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
try {
HttpSession session = request.getSession();
// HteaAddForm hteaform = new HteaAddForm();
HteaDAO hteaDAO = new HteaDAO(ds);
int id = 0;
String hteaId = request.getParameter("id");
if (hteaId != null && !hteaId.equals("")) {
id = Integer.parseInt(hteaId);
} else {
return mapping.findForward("error");
}
Htea htea = hteaDAO.checkout(id);
HteaForm hteaForm = (HteaForm) actionForm;
BeanUtils.populate(hteaForm, BeanUtils.describe(htea));
return mapping.findForward("check");
} catch (Exception e) {
generalError(request, e);
return mapping.findForward("error");
}
}
private ActionForward performHteaDelete(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
try {
HteaDAO hteaDAO = new HteaDAO(ds);
int i = Integer.parseInt(request.getParameter("id"));
hteaDAO.delete(i);
return mapping.findForward("hteadelete");
} catch (Exception e) {
generalError(request, e);
return mapping.findForward("error");
}
}
private ActionForward performHteaSelect(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
try {
HteaDAO hteaDAO = new HteaDAO(ds);
String str_id = request.getParameter("id");
int id = 0;
if (str_id != null && str_id.trim().length() > 0) {
id = Integer.parseInt(str_id);
} else {
return mapping.findForward("error");
}
Htea htea = hteaDAO.select(id);
HteaForm hteaForm = (HteaForm) actionForm;
BeanUtils.populate(hteaForm, BeanUtils.describe(htea));
return mapping.findForward("hteaselect");
} catch (Exception e) {
generalError(request, e);
e.printStackTrace();
return mapping.findForward("error");
}
}
private ActionForward performHteaSave(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
try {
HteaDAO hteaDao = new HteaDAO(ds);
HteaForm hteaForm = (HteaForm) actionForm;
Htea htea = new Htea();
int id = hteaForm.getHtea_id();
htea = hteaDao.select(id);
BeanUtils.populate(htea, BeanUtils.describe(hteaForm));
hteaDao.update(htea);
return mapping.findForward("update");
}
catch (Exception e) {
generalError(request, e);
e.printStackTrace();
return mapping.findForward("error");
}
}
private void sqlNullError(HttpServletRequest request, String objName) {
ActionErrors errors = new ActionErrors();
errors.add("Htea", new ActionError("error.Htea.null"));
saveErrors(request, errors);
if (__log.isErrorEnabled()) {
__log.error(" [Htea] has not found - " + objName);
}
}
private void generalError(HttpServletRequest request, Exception e) {
ActionErrors aes = new ActionErrors();
aes.add(aes.GLOBAL_ERROR, new ActionError("error.general", e
.getMessage()));
saveErrors(request, aes);
e.printStackTrace();
if (__log.isErrorEnabled()) {
__log.error(" [Htea] Error - " + e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -