| public class PageHandler { protected HttpServletRequest request; www.hot007.com protected HttpServletResponse response; protected JspWriter out; protected PageContext pageContext; protected HttpSession session = null; protected ServletContext application = null; protected ServletConfig config = null; protected String event_action = null; //页面事件 protected String event_params = null; //页面参数 //取得操作页面的基本组件 public PageHandler(PageContext page) { this.pageContext = page; this.request = (HttpServletRequest) pageContext.getRequest(); this.response = (HttpServletResponse) pageContext.getResponse(); this.pageContext = page; out = pageContext.getOut(); application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); try{ request.setCharacterEncoding("gb2312");//设定页面编码 } catch(Exception e) { e.printStackTrace(); 复制于jc567.cn } } //初始化页面的参数,具体的页面处理器类可以重写这 //个方法进行页面初始化 protected void onLoad() throws Exception { } //根据页面指定的事件进行处理 private final void eventBind() throws Exception { //event_action从从页面的名为event_action的hidden字段取得,它意为事件的称, //当此事件触发时,他会寻找在"页面处理器类中"与event_action同名的方法加 // 以调用。 if (event_action != null && !event_action.equals(Format.Empty)) { event_params = request.getParameter("parameters"); //事件参数参数,从页面 //的名为parameters的hidden字段取得 if (paramTypes[0] == null) { paramTypes[0] = Class.forName("java.lang.String"); } Object paramValues[] = new Object[1]; paramValues[0] = event_params; Method method = null; try { method = this.getClass().getDeclaredMethod(event_action, paramTypes); 007网络教程网 method.setAccessible(true); } catch (Exception e) { throw new UserException("系统缺少对您的请求的处理机制: event_action); } if (method != null) { method.invoke(this, paramValues); //调用web时间 } } } //处理页面 public void process() throws Exception { try { event_action = request.getParameter("action"); //得页面事件 onLoad();//页面加载时的初始化 eventBind();//处理事件 } catch (Exception e) { e.printStackTrace(); /////////////// Format.alert(out, "发生了未知错误:" Format.getString(e.getMessage())); } } } |