目录
一、让中央控制器动态加载存储子控制器
通过上篇文章中央控制器接受浏览器的请求,在其中我们使用map集合储存子控制器
但也存在一个问题,如果有一个订单类,那我们还要加入此类
我们通过建模的知识来解决,在配置文件中操作
中央控制器:
DispatcherServlet优化
package com.zwc.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.management.RuntimeErrorException;
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.zwc.web.BookAction;
/**
* 中央控制器:
* 主要职能:接受浏览器请求,找到对应的处理人
* @author Administrator
*
*/
//@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("/order",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 req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//http://localhost:8080/mvc/book.action?methodName=list
String uri = req.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();
// type是Action子控制器的全路径名
try {
Action action = (Action) Class.forName(type).newInstance();
// action是bookaction
if(action instanceof ModelDrivetr) {
ModelDrivetr md = (ModelDrivetr) action;
// model指的是bookaction中book
Object model = md.getModel();
// 要给model中的属性赋值,要接受前端jsp参数 req》getparamenterMap()
// PropertyUtils.getProperty(bean, name)从某一个对象去某一个值
// 将前端所有参数值封装进实体类
BeanUtils.populate(model, req.getParameterMap());
}
// 正式调用方法前,book中的属性要被赋值
// action.execute(req, resp);
String result = action.execute(req, resp);
ForwardModel forwardModel = actionModel.pop(result);
// if(forwardModel==null)
// throw new RuntimeException("forward config error");
String path = forwardModel.getPath();
// 拿到是否需要转发的配置
boolean redirect = forwardModel.isRedirect();
if(redirect)
// ${req.getServletContext().getContextPath()}
resp.sendRedirect(req.getServletContext().getContextPath() + path);
else
req.getRequestDispatcher(path).forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、参数传递封装优化
package com.zwc.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zwc.entity.Book;
import com.zwc.framework.Action;
import com.zwc.framework.ActionSupport;
import com.zwc.framework.ModelDrivetr;
public class BookAction extends ActionSupport implements ModelDrivetr<Book>{
private Book book = new Book();
private String list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在同样一个Servlet中调用list方法");
return "success";
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在同样一个Servlet中调用edit方法");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在同样一个Servlet中调用del方法");
}
private String add(HttpServletRequest req, HttpServletResponse resp) {
String bid = req.getParameter("bid");
String bname = req.getParameter("bname");
String price = req.getParameter("price");
Book book = new Book();
book.setBid(Integer.valueOf(bid));
book.setBname(bname);
book.setPrice(Float.valueOf(price));
// bookdao.add(book)
System.out.println("在同样一个Servlet中调用add方法");
return "failed";
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在同样一个Servlet中调用load方法");
}
@Override
public Book getModel() {
return book;
}
}
我们要解决参数过多问题
在这里我们要建一个模型驱动接口,bookaction实现该接口在中央控制器中将所有要接收的参数封装到模型接口中,从而达到简便的效果。
ModelDriver接口类
package com.zwc.framework;
/**
* 模型驱动接口,接受前台jsp传递的参数,并且封装到实体类中
* @author Administrator
*
* @param <T>
*/
public interface ModelDrivetr<T> {
// 拿到将要封装的类实例 ModelDriver。getModel()等价于 new book()
T getModel();
}
中央控制器:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//http://localhost:8080/mvc/book.action?methodName=list
String uri = req.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();
// type是Action子控制器的全路径名
try {
Action action = (Action) Class.forName(type).newInstance();
// action是bookaction
if(action instanceof ModelDrivetr) {
ModelDrivetr md = (ModelDrivetr) action;
// model指的是bookaction中book
Object model = md.getModel();
// 要给model中的属性赋值,要接受前端jsp参数 req》getparamenterMap()
// PropertyUtils.getProperty(bean, name)从某一个对象去某一个值
// 将前端所有参数值封装进实体类
BeanUtils.populate(model, req.getParameterMap());
}
// 正式调用方法前,book中的属性要被赋值
// action.execute(req, resp);
String result = action.execute(req, resp);
ForwardModel forwardModel = actionModel.pop(result);
// if(forwardModel==null)
// throw new RuntimeException("forward config error");
String path = forwardModel.getPath();
// 拿到是否需要转发的配置
boolean redirect = forwardModel.isRedirect();
if(redirect)
// ${req.getServletContext().getContextPath()}
resp.sendRedirect(req.getServletContext().getContextPath() + path);
else
req.getRequestDispatcher(path).forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
BookAction类
三、 对于方法执行结果,转发重定向优化
Config.xml配置文件
中央控制器:
Bookaction类
Demo2jsp:
Demo3jsp:
运行结果:
点击增加标签,会调到demo3jsp
点击查询,会跳到demo2jsp
四、框架配置文件可变
当我们如果给config.xml改变名字,我们的程序就运行不了了
在我们 ConfigModelFactory中已经默认Config.xml了
我们可以在DispatcherServlet类里的init初始化里设置它的配置地址:
中央控制器:
@Override
public void init() throws ServletException {
// actions.put("/book",new BookAction());
// actions.put("/order",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();
}
}
在web.xml中配置
web.xml:
<?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>T280_mvc</display-name>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>com.zwc.framework.DispatcherServlet</servlet-class>
<init-param>
<param-name>configLocation</param-name>
<param-value>/zwc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>