package com.foxconn.web.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.foxconn.web.form.BasicForm;
import com.foxconn.util.Exception.FoxconnException;
/**
* @author User
*/
public abstract class BasicAction extends Action
{
public Logger logger = Logger.getLogger(BasicAction.class);
/**
* @throws FoxconnException
*
*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
)
throws IOException,ServletException, FoxconnException
{
ActionForward forward = null;
HttpSession session = request.getSession(false);
if(session == null) session = request.getSession(true);
System.out.println("mapping path:" + mapping.getPath());
BasicForm baseForm = (BasicForm)form;
String action = baseForm.getAction();
if(action == null) action = "";
try
{
System.out.println("...ready to call [performAction]:action is " + action + " ...");
forward = performAction(mapping, form, request, response, session, action);
if (forward == null)
// System.out.println("foward is:"+forward);
// System.out.println("mappings:"+mapping.getInputForward());
forward = mapping.getInputForward();
System.out.println("...successfully called [performAction] and return " + forward.getName() + "...");
}
catch(FoxconnException e)
{
e.printStackTrace();
forward = mapping.getInputForward();
}
return forward;
}
public abstract ActionForward performAction(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
HttpSession session, String action)
throws IOException, ServletException, FoxconnException;
public abstract void InitMenu(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
HttpSession session, String action)
throws IOException,ServletException,FoxconnException;
}
使用该action:
package com.foxconn.web.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.foxconn.web.bean.EmpBean;
import com.foxconn.web.de.Delegate;
import com.foxconn.util.Exception.FoxconnException;
import com.foxconn.web.form.ListEmpForm;
public class ListEmpAction extends BasicAction {
//Logger logger = Logger.getLogger(ListEmpAction.class);
/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward performAction(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
HttpSession session,
String action)
throws IOException, ServletException, FoxconnException {
ListEmpForm empForm = (ListEmpForm) form;
Delegate delegeate = Delegate.getInstance();
empForm.setEmpList(delegeate.getEmpInHmd());
if(action.equals("addemp")){
return mapping.findForward("addEmp");
}
else if(action.equals("delEmp")){
EmpBean empbean = new EmpBean();
empbean.setEmpNo(empForm.getId());
delegeate.delEmp(empbean);
empForm.setEmpList(delegeate.getEmpInHmd());
return (mapping.getInputForward());
}
else if(action.equals("modifyjs")){
System.out.println("the id is:"+empForm.getId());
EmpBean empbean = new EmpBean();
empbean.setEmpNo(empForm.getId());
EmpBean bean = delegeate.getEmpByEmpNo(empbean);
session.setAttribute("empbean", bean);
return mapping.findForward("mod");
}
else return mapping.getInputForward();
}
public void InitMenu(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
HttpSession session,
String action
) throws IOException, ServletException {
// TODO Auto-generated method stub
}
}
不知不觉又贴了这么多代码上来,我看过一个网上媒体商店的code 没有用taglib
然后控制好像也不是很好使用 以上通过扩展action 我觉得从控制转向上好用多了
问题就是JSP页面得写一些脚本,而在JSP写脚本不正是趋势吗?:)比如Ajax
^_^