IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 自定义mvc -> 正文阅读

[开发测试]自定义mvc

打包action和model

action里面的内容

Action类? 接口? public String? execute(String? methodName,?HttpServletRequest req,HttpServletResponse ?resp)throws Exception ;

ActionServlet类? 继承??HttpServlet{

doPost方法

String  url=req.getRequestURL();//得到请求路径
int  start=url.lastIndexOf("/");
int end=url.lastIndexOf(" . ");
if(end!=-1){
url=url.substring(start,end);
}else{
url=url.substring(start);
}

Action  action-this.getAtion(url);

this.handleModel(action,req);

String  methodName=this.getMethodName(req);


String   forward=action.execute(methodName,req,resp);
this.tp(url,forward,req,resp);//会有异常


类中封装方法



通过建模得到对应的action类
private  Action  getAction(String url){
    Action  action=null;
    try{
         ConfigModel  createConfig=ConfigModel.createConfigModel("/config.xml");
         ActionModel  actionModel=createConfig.get(url);
         String   type=actionModel.getType();//得到type
          Class   c=Class.forName(type);
          Object   obj=c.newInstance();//初始化
          action=(Action)obj;//返回  
          } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return action;
}


获取对应的方法名

private   String   getMethodName(HttpServletRequest  req){

     String   parameter=req.getParameter("methodName");
     return  parameter;

}



给对应的子控制器赋值


private  void   handleModel(Action  action,HttpServletRequest  req){

if(action   instanceof   ModelDriver){//如果action属于ModelDriver
            ModelDriver  md=(ModelDriver)action;
           Object   model=md.getModel();//拿到一个模型
          BeanUtils.populate(model,req.getParameterMap());//jar包里面的方法自动赋值
      }

}


负责页面跳转


private  void   tp(String  url,String  path,HttpServletRequest req, HttpServletResponse  resp){
           ConfigModel  configModel=ConfigModel.createConfigModel("/config.xml");
           
            ActionModel  actionModel=condigModel.get(url);
             ForwardModel  forwardModel=actionModel.get(path);
            if(null!=forwardModel){//不为空才跳转
                      if(forwardModel.isRedirect()){ //是否重定向
                   resp.sendRedirect(req.getContextPath()+forwardModel.getPath());
                 }else{
                    req.getRequestDispatcher(forwardModel.getPath()).forward(req, resp);

                 }
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



}

DispatherServlet? 实现? ? Action? {

运行Action方法

根据方法名得到方法体

public? ?String? ?executeString methodName,HttpServletRequest req, HttpServletResponse resp) throws Exception {

? ? //根据传递过来的方法名获取方法对象

? ?Method? ?method=this.getClass().getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);

//运行方法? ?this谁调用就是谁

Object? obj=method.invoke(this,req,resp);

return? obj.toString();

}

}

接口? ?ModelDriver<T>{

public? T? getModel();//所有servlet必须实现这个

}

model

? ActionModel

package com.zking.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActionModel implements Serializable{

	private String path;
	private String type;
	private Map<String, ForwardModel> forwardModels=new HashMap<>();
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	//action中放入forward
	public void put(ForwardModel forwardModel) {
		//根据传递过来的forward的name来判断该值是否已经存在
		if(this.forwardModels.containsKey(forwardModel.getName())) {
			throw new RuntimeException("该键位["+forwardModel.getName()+"]已经存在");
		}
		//否则的话,说明这个name是唯一的,可以放入到map中
		this.forwardModels.put(forwardModel.getName(), forwardModel);
			
	}
	
	public ForwardModel get(String name) {
		if(null==this.forwardModels.get(name)) {
			throw new RuntimeException("该键位["+name+"]不存在");
		}
		return this.forwardModels.get(name);
	}
	@Override
	public String toString() {
		return "ActionModel [path=" + path + ", type=" + type + ", forwardModels=" + forwardModels + "]";
	}
	
	
	
	
	
}

ConfigModel

package com.zking.model;

import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ConfigModel implements Serializable{
	private Map<String, ActionModel> actionModels=new HashMap<String, ActionModel>();
	private static final String PATH="/config.xml";
	
	public void put(ActionModel actionModel) {
		if(this.actionModels.containsKey(actionModel.getPath())) {
			//键位重复了
			throw new RuntimeException("键位["+actionModel.getPath()+"]已存在");
		}
		this.actionModels.put(actionModel.getPath(), actionModel);
	}
	
	public ActionModel get(String path) {
		ActionModel ac = this.actionModels.get(path);
		if(null==ac) {
			throw new RuntimeException("键位["+path+"]不存在");
		}
		return ac;
	}
	
	
	/**
	 * 往模型中放入内容
	 * 只要用户调用该方法,就能获取到xml中所有的内容
	 * 需要返回一个ConfigModel给用户
	 * 
	 * 
	 */
	//封装构造方法
	private ConfigModel() {
		
	}
	//编写一个方法帮助用户获取ConfigModel
	
	public static  ConfigModel createConfigModel(String path) throws Exception {
		//创建ConfigModel
		ConfigModel configModel=new ConfigModel();
		//先解析
		InputStream is=null;
		if(null==path) {
			is=ConfigModel.class.getResourceAsStream(PATH);
		}else {
			is=ConfigModel.class.getResourceAsStream(path);
		}
		
		SAXReader sax=new SAXReader();
		Document document = sax.read(is);
		//后赋值
		List<Element> actionElements = document.selectNodes("/config/action");
		//循环遍历所有的action
		for (Element actionElement : actionElements) {
			ActionModel actionModel=new ActionModel();
			actionModel.setPath(actionElement.attributeValue("path"));
			actionModel.setType(actionElement.attributeValue("type"));
			List<Element> forwardElements = actionElement.selectNodes("forward");
			//循环遍历所有的action找到对应的forward
			for (Element forwardElement : forwardElements) {
				ForwardModel forwardModel=new ForwardModel();
				forwardModel.setName(forwardElement.attributeValue("name"));
				forwardModel.setPath(forwardElement.attributeValue("path"));
				forwardModel.setRedirect(forwardElement.attributeValue("redirect"));
				//循环将forward放入到对应的action中
				actionModel.put(forwardModel);
			}
			configModel.put(actionModel);
		}
		return configModel;
	}
	
	
	

}

ForwardModel

package com.zking.model;

import java.io.Serializable;

public class ForwardModel implements Serializable{

	private String name;
	private String path;
	private boolean redirect;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public boolean isRedirect() {
		return redirect;
	}
	public void setRedirect(boolean redirect) {
		this.redirect = redirect;
	}
	public void setRedirect(String redirect) {
		this.redirect = Boolean.parseBoolean(redirect);
	}
	@Override
	public String toString() {
		return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
	}
	
}

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/stu" type="com.zking.servlet.StuServlet">
		<forward name="index" path="/index.jsp" redirect="true"/>
		<forward name="update" path="/update.jsp" redirect="true"/>
	</action>
	<action path="/studao" type="com.zking.dao.StuDao">
	
	</action>
</config>

xml配置中央控制器

访问路径为? ? *.action

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-03 11:30:55  更:2021-08-03 11:31:53 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 8:27:55-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码