我写的这个框架大致模仿webwork的原理。我只用过webwork一个框架,当然模仿它了^^
主要是为了学习,苦思冥想写出来了。把它帖出来,大家多给我一些意见。
在我的框架里面出现大量的sophie是我的名字,呵呵,女性写的代码命名也比较女性化吧*^_^*
Sophie的web框架
主要思路
Init配置文件-》dispatcher调度-》invocation执行action=》result处理结果,显示页面
主要功能部分
1. 调度器,在启动服务的时候调用init方法,初始化action的配置文件。保存servlet的内容到SophieContext 中dispatcher
2. 和resultConfiguration模型,把配置文件中的action信息,封装到一个configration类中。多个action信息组成一个map。每个action的result信息,封装到resultConfiguration中,多个result组成一个map.configuration
3. 类。逻辑的东西可以写在action中。Action类从servlet中分离出来。专注与逻辑本身。提供给框架的使用者。action
4. 保存context的类。里面有个静态的map。SophieContext
5. 执行actionSophieInvocation
6. 处理action执行的结果,根据resultConfiguration里的配置,转向显示页面。sophieResult
7. 标签,可以在页面上用标签调用actionActionTag action
代码详细说明
1)SophieDispatcher是一个servlet,init()方法中初始化一些配置文件。
service()方法中执行action
SophieDispatcher 代码
- package com.sophie.dispatcher;
-
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
-
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import com.sophie.context.ConfigUtil;
- import com.sophie.context.Configuration;
- import com.sophie.context.SophieContext;
- import com.sophie.parameter.Parameter;
-
- public class SophieDispatcher extends HttpServlet {
- @Override
- protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
-
-
-
- Map parametersMap=request.getParameterMap();
- setParamtersMap(request, parametersMap);
- SophieContext.setRequest(request);
- SophieContext.setResponse(response);
-
-
- String actionName=getActionName(request);
-
-
- SophieInvocation invocation=new SophieInvocation(actionName);
- String resultString=invocation.execute();
- ResultDispatcher resultDispatcher=new ResultDispatcher(invocation.getResultConfigMap(),resultString);
- resultDispatcher.Dispatcher();
- }
-
- @Override
-
-
-
- public void init(ServletConfig config) throws ServletException {
- ServletContext context=config.getServletContext();
-
-
-
-
-
-
-
-
-
-
- Map configMap=new HashMap();
- configMap=ConfigUtil.initConfigurationMap();
-
- SophieContext.setServletContext(context);
- SophieContext.setConfigurationMap(configMap);
- System.out.println("===init====");
- }
-
-
- private void setParamtersMap(HttpServletRequest request,Map parametersMap){
- Parameter parm=new Parameter();
- Set entrySet=parametersMap.entrySet();
- Map map=new HashMap();
- for (Iterator iter = entrySet.iterator(); iter.hasNext();) {
- Map.Entry entry=(Map.Entry)iter.next();
- parm.setName((String)entry.getKey());
- parm.setValue((String[])entry.getValue());
- map.put((String)entry.getKey(), parm);
- }
- SophieContext.setRequestParametersMap(map);
- }
-
-
- private String getActionName(HttpServletRequest request){
- String uri=request.getRequestURI();
- String actionName=uri.substring(uri.lastIndexOf("/")+1, uri.indexOf(".action"));
- return actionName;
- }
-
-
-
- }
2)SophieContext 保存servlet中的信息,如servletContext,requet,response
java 代码
- package com.sophie.context;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import com.sophie.parameter.Parameter;
-
- public class SophieContext {
-
-
-
- private static Map context=new HashMap();
-
-
-
- private SophieContext(){
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static Map getContext() {
- return context;
- }
-
-
- public static HttpServletRequest getRequest(){
- return (HttpServletRequest)getContext().get(SophieStatic.HTTP_REQUEST);
- }
-
- public static void setRequest(HttpServletRequest request){
- getContext().put(SophieStatic.HTTP_REQUEST, request);
- }
-
-
- public static HttpServletResponse getResponse(){
- return (HttpServletResponse)getContext().get(SophieStatic.HTTP_RESPONSE);
- }
-
- public static void setResponse(HttpServletResponse response){
- getContext().put(SophieStatic.HTTP_RESPONSE, response);
- }
-
-
-
-
- public static void setServletContext(ServletContext servletContext){
- getContext().put(SophieStatic.SERVLET_CONTEXT, servletContext);
- }
-
-
- public static ServletContext getServletContext(){
- return (ServletContext)getContext().get(SophieStatic.SERVLET_CONTEXT);
- }
-
-
-
-
- public static void setConfigurationMap(Map configMap){
- getContext().put(SophieStatic.SOPHIE_ACTION_CONFIG_MAP, configMap);
- }
-
- @SuppressWarnings("unchecked")
- public static Map getConfigurationMap(){
- return (Map)getContext().get(SophieStatic.SOPHIE_ACTION_CONFIG_MAP);
- }
-
-
-
-
- public static void setRequestParametersMap(Map parametersMap){
- getContext().put(SophieStatic.PARAMETERS_MAP, parametersMap);
- }
-
- @SuppressWarnings("unchecked")
-
-
-
- public static Map getRequestParametersMap(){
- return (Map)getContext().get(SophieStatic.PARAMETERS_MAP);
- }
-
- }
3)Configuration, ResultConfiguration两个类是两个model,封装备置文件中action的属性信息
java 代码
-
-
-
-
- package com.sophie.context;
-
- import java.util.Map;
-
- public class Configuration {
-
- private String actionName;
-
- private String className;
-
- private String methodName;
-
- private Map resultMap;
-
-
-
- }
4)配置文件action.properties
java 代码
- test|com.sophie.action.TestAction||success--/hello.jsp
- testInclude|com.sophie.action.TestAction|testParameter|success--/includePage.html
5)action的执行 这里用反射
java 代码
-
-
-
-
- package com.sophie.dispatcher;
-
- import java.io.IOException;
- import java.util.Map;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import com.sophie.context.ResultConfiguration;
- import com.sophie.context.SophieContext;
- import com.sophie.result.SophieResult;
-
- public class ResultDispatcher {
- private Map resultMap;
- private String resultString;
-
-
- private String include;
-
- public ResultDispatcher(Map resultMap,String resultString) {
- this.resultMap=resultMap;
- this.resultString=resultString;
- }
-
-
- public void Dispatcher(){
-
- HttpServletRequest request=SophieContext.getRequest();
- HttpServletResponse response=SophieContext.getResponse();
- ResultConfiguration resultConfig=resultMap.get(resultString);
-
- if(null==resultConfig.getResultType() || "".equals(resultConfig.getResultType() )){
- SophieResult dispatcher=new SophieResult();
- dispatcher.setLocation(resultConfig.getPageUrl());
- dispatcher.setInclude(include);
- try {
- dispatcher.service(request, response);
- } catch (ServletException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
-
- public void setInclude(String include) {
- this.include = include;
- }
-
- }
7) result类 处理输出
java 代码
-
-
-
-
- package com.sophie.result;
-
- import java.io.IOException;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class SophieResult extends HttpServlet{
-
- private String location;
- private String include;
-
- public void setLocation(String location) {
- this.location = location;
- }
-
- @Override
- protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- if(null!=location){
- RequestDispatcher dispatcher=request.getRequestDispatcher(location);
- if(null!=include && "include".equals(include))
- dispatcher.include(request, response);
- else
- dispatcher.forward(request, response);
- }
-
- }
-
- public void setInclude(String include) {
- this.include = include;
- }
- }