📄 frontcontroller.java
字号:
package com.strutslet.core;import javax.servlet.*;import javax.servlet.http.*;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import com.strutslet.model.ActionModel;import com.strutslet.util.ConfigUtil;import com.strutslet.util.Constant;import java.io.*;import java.util.Map;import java.util.logging.Logger;/** * 前端控制器 * @author dennis */public class FrontController extends HttpServlet { private static final Log log = LogFactory.getLog(FrontController.class); @Override public void init() throws ServletException { ServletContext context=getServletContext(); String config_file =getServletConfig().getInitParameter("config"); String dispatcher_name=getServletConfig().getInitParameter("dispatcher"); if (config_file == null || config_file.equals("")) config_file = "/WEB-INF/strutslet-config.xml"; if(dispatcher_name==null||dispatcher_name.equals("")) dispatcher_name=Constant.DEFAULT_DISPATCHER; try { Map<String, ActionModel> resources = ConfigUtil.newInstance() .parse(config_file, context); context.setAttribute(Constant.ACTIONS_ATTR, resources); log.info("初始化strutslet配置文件成功"); } catch (Exception e) { log.error("初始化strutslet配置文件失败"); e.printStackTrace(); } try{ Class c = Class.forName(dispatcher_name); Dispatcher dispatcher = (Dispatcher) c.newInstance(); context.setAttribute(Constant.DISPATCHER_ATTR, dispatcher); log.info("初始化Dispatcher成功"); }catch(Exception e) { log.error("初始化Dispatcher失败"); e.printStackTrace(); } } /** * Handle GET requests by forwarding to a common method */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); // getServletConfig().getInitParameter("") } /** * Handle POST requests by forwarding to a common method */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } /** * Process a request by forwarding to the appropriate dispatcher */ protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = getServletContext(); //获取action的path String reqURI = request.getRequestURI(); int i=reqURI.lastIndexOf("."); String contextPath=request.getContextPath(); String path=reqURI.substring(contextPath.length(),i); request.setAttribute(Constant.REQUEST_ATTR, path); Dispatcher dispatcher = (Dispatcher) context.getAttribute(Constant.DISPATCHER_ATTR); // make sure we don't cache dynamic data response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); // use the dispatcher to find the next page String nextPage = dispatcher.getNextPage(request, context); // forward control to the view RequestDispatcher forwarder = request.getRequestDispatcher("/" + nextPage); forwarder.forward(request, response); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -