2.还是先来看下两个模型:ActionForward和ActionModel,没什么东西,属性以及相应的getter,setter方法:
/** * 类说明:转向模型 * @author dennis * * */ public class ActionForward { private String name; //forward的name private String viewUrl; //forward的url public static final ActionForward SUCCESS=new ActionForward("success"); public static final ActionForward FAIL=new ActionForward("fail"); public ActionForward(String name){ this.name=name; } public ActionForward(String name, String viewUrl) { super(); this.name = name; this.viewUrl = viewUrl; } //...name和viewUrl的getter和setter方法 } 我们看到ActionForward预先封装了SUCCESS和FAIL对象。 public class ActionModel { private String path; // action的path private String className; // action的class private Map<String, ActionForward> forwards; // action的forward public ActionModel(){} public ActionModel(String path, String className, Map<String, ActionForward> forwards) { super(); this.path = path; this.className = className; this.forwards = forwards; } //...相应的getter和setter方法 } 3。知道了两个模型是什么样,也应该可以猜到我们的配置文件大概是什么样的了,与struts的配置文件格式类似: <?xml version="1.0" encoding="UTF-8"?> <actions> <action path="/login" class="com.strutslet.demo.LoginAction"> <forward name="success" url="hello.jsp"/> <forward name="fail" url="fail.jsp"/> </action> </actions> path是在应用中将被调用的路径,class指定了调用的哪个action,forward元素指定了转向,比如我们这里如果是success就转向hello.jsp,失败的话转向fail.jsp,这里配置了demo用到的LoginAction。 4。Dispacher接口,主要是getNextPage方法,此方法负责获得下一个页面将导向哪里,提供给前端控制器转发。 public interface Dispatcher { public void setServletContext(ServletContext context); public String getNextPage(HttpServletRequest request,ServletContext context); } 5。5。原先书中实现了一个WorkFlow的Dispatcher,按照顺序 调用action,实现工作流调用。而我们所需要的是根据请求的path调用相应的action,执行action的execute方法返回一个 ActionForward,然后得到ActionForward的viewUrl,将此viewUrl提供给前端控制器转发,看看它的 getNextPage方法: public String getNextPage(HttpServletRequest request, ServletContext context) { setServletContext(context); Map<String, ActionModel> actions = (Map<String, ActionModel>) context .getAttribute(Constant.ACTIONS_ATTR); //从ServletContext得到所有action信息 String reqPath = (String) request.getAttribute(Constant.REQUEST_ATTR);//发起请求的path ActionModel actionModel = actions.get(reqPath); //根据path得到相应的action String forward_name = ""; ActionForward actionForward; try { Class c = Class.forName(actionModel.getClassName()); //每个请求对应一个action实例 Action action = (Action) c.newInstance(); actionForward = action.execute(request, context); //执行action的execute方法 forward_name = actionForward.getName(); } catch (Exception e) { log.error("can not find action "+actionModel.getClassName()); e.printStackTrace(); } actionForward = actionModel.getForwards().get(forward_name); if (actionForward == null) { log.error("can not find page for forward "+forward_name); return null; } else return actionForward.getViewUrl(); //返回ActionForward的viewUrl }6。前端控制器(FrontController),它的任务我们已经很清楚,初始化配置文件;存储所有action到 ServletContext供整个框架使用;得到发起请求的path,提供给Dispachter查找相应的action;调用Dispatcher, 执行getNextPage方法得到下一个页面的url并转发:
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"; //默认是/WEB-INF/下面的strutslet-config 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); //存储在ServletContext中 log.info("初始化strutslet配置文件成功"); } catch (Exception e) { log.error("初始化strutslet配置文件失败"); e.printStackTrace(); } //实例化Dispacher try{ Class c = Class.forName(dispatcher_name); Dispatcher dispatcher = (Dispatcher) c.newInstance(); context.setAttribute(Constant.DISPATCHER_ATTR, dispatcher); //放在ServletContext log.info("初始化Dispatcher成功"); }catch(Exception e) { log.error("初始化Dispatcher失败"); e.printStackTrace(); } ..... doGet()和doPost方法我们都让它调用process方法: 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);//调用Dispatcher的getNextPage // forward control to the view RequestDispatcher forwarder = request.getRequestDispatcher("/" + nextPage); forwarder.forward(request, response); //转发页面 } 7。最后,web.xml的配置就非常简单了,配置前端控制器,提供启动参数(配置文件所在位置,为空就查找/WEB-INF/下面的strutslet-config.xml文件),我们把所有以action结尾的请求都交给FrontController处理: <servlet> <servlet-name>StrutsletController</servlet-name> <servlet-class>com.strutslet.core.FrontController</servlet-class> <!-- <init-param> <param-name>config</param-name> <param-value>/WEB-INFstrutslet-config.xml</param-value> </init-param> --> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StrutsletController</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> 最后,让我们看看整个框架图: