打包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
|