actiontool.java

来自「这是一个自己开发的java框架 类似于 struts 用于喜欢研究框架的朋友们」· Java 代码 · 共 54 行

JAVA
54
字号
package tools;

import javax.servlet.http.HttpServletRequest;

import action.MyAction;
import config.MyBean;
import config.MyConfig;
import config.MyForward;
import config.MyMap;
import exception.ActionHasError;
import exception.FormHasError;
import exception.ForwardHasError;

public class ActionTool {
	//得到转发地址
	public static String getSendAddress(MyConfig config,String forward)throws ForwardHasError{
		boolean flag = false;
		MyForward[] myforward = config.getMyforward();
		for(int i=0;i<myforward.length;i++){
			if(myforward[i].getName().equals(forward)){
				flag = true;
				return myforward[i].getPath();		
			}
		}
		if(!flag){
			throw new ForwardHasError();
		}
		return null;
	}
	//得到action对象
	public static MyAction getMyAction(HttpServletRequest request,MyConfig config)throws Exception{
		String uri = request.getRequestURI();
		String path = UtilTool.getRequestPath(uri);
		
		String actionClass = getActionClass(config,path);
		MyAction action = (MyAction)Class.forName(actionClass).newInstance();
		return action;
	}
	static String getActionClass(MyConfig config, String thePath) throws ActionHasError{
		boolean flag = false;
		MyMap[] mymap = config.getMymap();
		for (int i = 0; i < mymap.length; i++) {
			if (thePath != null && thePath.equals(mymap[i].getPath())) {
				return mymap[i].getType();
			} 
		}
		if(!flag){
			System.out.println("没有action");
			throw new ActionHasError();
		}
		return null;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?