目录
一、让中央控制器动态加载存储子控制器
package com.zxw.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.zxw.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 {
//配置地址
String configLocation = this.getInitParameter("configLocation");
if(configLocation == null || "".equals(configLocation))
configModel = ConfigModelFactoy.build();
else
configModel = ConfigModelFactoy.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.acion?methodName=list
String uri = req.getRequestURI();
//要拿到/book 就是最后一个/到最后一个.的位置
uri = uri.substring(uri.lastIndexOf("/")
, uri.lastIndexOf("."));
// Action action = actions.get(uri);
//相比于上一种从map集合获取子控制器、当前需要获取comfig.xml中的全路径名、然后反射实例化
ActionModel actionModel = configModel.pop(uri);
if(actionModel == null) {
throw new RuntimeException("action 配置错误");
}
String type = actionModel.getType();
// Action action;
try {
//type是action子控制器的路劲名
Action action = (Action)Class.forName(type).newInstance();
//action 是 bookAction
if(action instanceof ModelDriven) {
ModelDriven md = (ModelDriven) action;
//指的是BookAction中的book实例
Object model = md.getModel();
//要给model中的属性赋值、要接受前端jsp参数
// PropertyUtils.getProperty(bean, name)
//将前端所有参数值封装进实体类
BeanUtils.populate(model, req.getParameterMap());
System.out.println(model);
}
//正式调用方法之前、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");
// }
//bookList.jsp/ index.jsp
String path = forwardModel.getPath();
//是否拿到需要转发的配置
boolean redirect = forwardModel.isRedirect();
if(redirect)
resp.sendRedirect(req.getServletContext().getContextPath());
else
req.getRequestDispatcher(path).forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、参数传递封装类
三、转发、重定向优化
package com.zxw.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zxw.entity.Book;
import com.zxw.framework.Action;
import com.zxw.framework.ActionSupport;
import com.zxw.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) {
// 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));
System.out.println("在同一个servlet中调用add方法");
return "failed";
}
@Override
public Book getModel() {
// TODO Auto-generated method stub
return book;
}
}
建一个模型驱动接口,使BookAction实现该接口,在中央控制器中将所有要接收的参数封装到模型接口中,从而达到简便的效果
ModelDriven类(驱动接口类):
package com.zxw.framework;
/**
* 模型驱动接口、接受前台JSP传递的参数、并且封装到实体类中
*
* @param <T>
*/
public interface ModelDriven<T> {
//拿到将要封装的类实例 ModelDriven.getModel() --> new Book();
T getModel();
}
DispatcherServlet 中央控制器类:
@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 ModelDriven) {
ModelDriven md=(ModelDriven) action;
// model指的是bookAction中的book实例
Object model = md.getModel();
// 要给model中的属性赋值,要接收前端jsp参数 req.getParameterMap()
// PropertyUtils.getProperty(bean, name)
// 将前端所有的参数值封装进实体类
BeanUtils.populate(model, req.getParameterMap());
System.out.println(model);
}
// 正式调用方法前,book中的属性要被赋值
action.execute(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
效果
三、转发、重定向优化
//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);
}
四、框架配置文件可变
web.xml