在上次的基础上优化功能:
目录
1.让中央控制器动态加载存储子控制器
package com.ruojuan.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import com.ruojuan.web.BookAction;
/**
* 中央控制器动态加载子控制器
*
* 中央控制器
* 主要职能:接受浏览器请求,找到对应的处理人
* @author liuruojuan
*
* 时间:2022年6月24日下午6:21:20
*/
//@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet{
// private Map<String, Action> actions = new HashMap<String, Action>();
/*
* 通过建模可以知道configModel对象包含config.xml所有子控制器信息
* 同时为了解中央控制器能够动态加载保存子控制器的信息,那么我们只需要引入configModel对象即可
*/
private ConfigModel configModel;
//程序启动时,只会加载一次
@Override
public void init() throws ServletException {
// actions.put("/book", new BookAction());
// actions.put("/book", new BookAction());
try {
//配置地址
//getInitParameter的作用是拿到web.xml中的servlet信息配置参数
String configLocation = this.getInitParameter("configLocation");
if(configLocation==null||"".equals(configLocation)) {
configModel = ConfigModelFactory.bulid();
}
else {
configModel = ConfigModelFactory.bulid(configLocation);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String uri = request.getRequestURI();
//要拿到/book,就是最后一个/到最后一个,的位子
uri = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
// Action action = actions.get(uri);
// 相比于上一种从map集合获取子控制器,当前需要获取config.xml中的全路径名,然后反射实例化
ActionModel actionModel = configModel.pop(uri);
if(actionModel == null) {
throw new RuntimeException("action 配置错误");
}
String type = actionModel.getType();
try {
//type是action子控制器全路径名
Action action = (Action)Class.forName(type).newInstance();
//action是bookAction
if(action instanceof ModelDriven) {
ModelDriven md = (ModelDriven) action;
//model指的是bookAction中的book实例
Object model = md.getModel();
//要给model中的属性赋值,要接受前端jsp参数 req.getParameterMap()
//PropertyUtils.getProperty(bean, name);
//将前端所有参数值封装进实体类
BeanUtils.populate(model, request.getParameterMap());
System.out.println(model);
}
//正式调用方法前,book中的属性要被赋值
//action.execute(request, response);
String result = action.execute(request, response);
ForwardModel forwardModel = actionModel.pop(result);
// if(forwardModel == null) {
// throw new RuntimeException("froward config error");
// }
//bookList.jsp /index.jsp
String path = forwardModel.getPath();
//拿到是否需要转发配置
boolean redirect = forwardModel.isRedirect();
if(redirect) {
//${pageContext.request.contextPath}
response.sendRedirect(request.getServletContext().getContextPath()+path);
}
else {
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.参数传递封装优化
假设有很多的属性,这样的方式不可取
String bid = request.getParameter("bid");
String bname = request.getParameter("bname");
String price = request.getParameter("price");
Book book = new Book();
book.setBid(Integer.valueOf(bid));
book.setBname(bname);
book.setPrice(Float.valueOf(price));
//bookDao.add(book);
jsp界面传值:
<h3>参数的传递封装优化</h3>
<a href="${pageContext.request.contextPath }/order.action?methodName=add&bid=12&bname=liaoliu&price=88">增加</a>
<a href="${pageContext.request.contextPath }/order.action?methodName=del">删除</a>
<a href="${pageContext.request.contextPath }/order.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/order.action?methodName=list">查看</a>
<a href="${pageContext.request.contextPath }/order.action?methodName=load">加载</a>
book实体类:
package com.ruojuan.entity;
public class Book {
private int bid;
private String bname;
private float price;
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Book() {
}
public Book(int bid, String bname, float price) {
this.bid = bid;
this.bname = bname;
this.price = price;
}
@Override
public String toString() {
return "book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
}
}
BookAction
package com.ruojuan.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ruojuan.entity.Book;
import com.ruojuan.framework.Action;
import com.ruojuan.framework.ActionSupport;
import com.ruojuan.framework.ModelDriven;
public class BookAction extends ActionSupport implements ModelDriven<Book>{
private Book book = new Book();
private String list(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用list方法");
return "success";
}
private void load(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用load方法");
}
private void edit(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用edit方法");
}
private void del(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用del方法");
}
private String add(HttpServletRequest request, HttpServletResponse response) {
Book book = new Book();
//bookDao.add(book);
System.out.println("在同一个servlet中调用add方法");
return "failed";
}
@Override
public Book getModel() {
return book;
}
}
ActionSupport
package com.ruojuan.framework;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ActionSupport implements Action {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) {
//未来区分当前请求的目的,增删改查的目的,就从前台讲要调用的方法名传递到后台
String methodName = request.getParameter("methodName");
//methodName可能是add/del/edit/list/load/xxx/...
//前台传递什么方法,就调用当前类的方法
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
return (String) m.invoke(this, request,response);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Action
package com.ruojuan.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器:
* 对应请求的控制人
* @author liuruojuan
*
* 时间:2022年6月24日下午6:22:51
*/
public interface Action {
String execute(HttpServletRequest request, HttpServletResponse response) ;
}
3.对于方法的可执行结果转发,重定向优化
//bookList.jsp /index.jsp
String path = forwardModel.getPath();
//拿到是否需要转发配置
boolean redirect = forwardModel.isRedirect();
if(redirect) {
//${pageContext.request.contextPath}
response.sendRedirect(request.getServletContext().getContextPath()+path);
}
else {
request.getRequestDispatcher(path).forward(request, response);
}
4.框架配置文件可变
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>mvc</display-name>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>com.ruojuan.framework.DispatcherServlet</servlet-class>
<init-param>
<param-name>configLocation</param-name>
<param-value>/luoluo</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
package com.ruojuan.framework;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 23种设计模式之工厂模式
* sessionfactory
* ConfigModelFactory就是用来生产configmodel对象
* 生产出来的ConfigModel对象就包含config.xml中的配置内容
*
* 此地生产configmodel有配置信息?
* 1.解析config.xml中的配置信息
* 2.将对应的配置信息分别加载进行不同的模型对象中
*
* @author liuruojuan
*
* 时间:2022年6月14日下午5:54:09
*/
public class ConfigModelFactory {
public static ConfigModel bulid(String path) throws Exception {
InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
SAXReader sr = new SAXReader();
Document read = sr.read(in);
List<Element> actionEles = read.selectNodes("/config/action");
ConfigModel configModel = new ConfigModel();
for (Element actionEle : actionEles) {
ActionModel actionModel = new ActionModel();
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
//将forwardmodel赋值并且添加actionmodel中
List<Element> forwardEles = actionEle.selectNodes("forward");
for (Element element : forwardEles) {
ForwardModel forwardModel = new ForwardModel();
forwardModel.setName(element.attributeValue("name"));
forwardModel.setPath(element.attributeValue("path"));
//redirect:只能是false|true,允许空,默认值为false
forwardModel.setRedirect("true".equals(element.attributeValue("redirect")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
public static ConfigModel bulid() throws Exception {
String defaultPath = "/config.xml";
return bulid(defaultPath);
}
}