1. 本章需要完成的内容
- 完成请求处理器接口RequestProcessor的编写
- 完成ResultRender渲染器接口的编写
- 完成DefultResultRender默认渲染器类的编写
- 完成InternalErrorResultRender内部异常渲染器类的编写
- 完成JsonResultRender Json渲染器类的编写
- 完成ResourceNotFoundResultRender 资源未找到时使用的渲染器类编写
- 完成ViewResultRender 页面渲染器类的编写
- 完成DispatcherServlet http请求拦截器的编写
- 完成RequestProcessorChain 责任链类的编写
2. 第一部分内容
- 该部分完成DispatcherServlet与RequestProcessorChain类的编写
2.1 DispatcherServlet类
2.1.1 需要完成的代码如下:
package com.wuyiccc.helloframework.mvc;
import com.wuyiccc.helloframework.aop.AspectWeaver;
import com.wuyiccc.helloframework.core.BeanContainer;
import com.wuyiccc.helloframework.injection.DependencyInjector;
import com.wuyiccc.helloframework.mvc.processor.RequestProcessor;
import com.wuyiccc.helloframework.mvc.processor.impl.ControllerRequestProcessor;
import com.wuyiccc.helloframework.mvc.processor.impl.JspRequestProcessor;
import com.wuyiccc.helloframework.mvc.processor.impl.PreRequestProcessor;
import com.wuyiccc.helloframework.mvc.processor.impl.StaticResourceRequestProcessor;
import lombok.extern.slf4j.Slf4j;
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 java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
@WebServlet("
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.myframework.mvc.processor.RequestProcessor;
import org.myframework.mvc.render.DefaultResultRender;
import org.myframework.mvc.render.InternalErrorResultRender;
import org.myframework.mvc.render.ResultRender;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
@Data
@Slf4j
public class RequestProcessorChain {
// 请求处理器迭代器
private Iterator<RequestProcessor> requestProcessorIterator;
private HttpServletRequest request;
private HttpServletResponse response;
// http请求方法
private String requestMethod;
// http请求路径
private String requestPath;
// http响应状态码
private int responseCode;
// 请求结果渲染器
private ResultRender resultRender;
public RequestProcessorChain(Iterator<RequestProcessor> requestProcessorIterator, HttpServletRequest request, HttpServletResponse response) {
this.requestProcessorIterator = requestProcessorIterator;
this.request = request;
this.response = response;
this.requestMethod = request.getMethod();
this.requestPath = request.getPathInfo();
this.responseCode = HttpServletResponse.SC_OK;
}
public void doRequestProcessorChain() {
// 1. 通过迭代器遍历注册的请求处理器实现类列表
try {
while (requestProcessorIterator.hasNext()) {
// 2. 知道某个请求处理器执行后返回为false为止
if (!requestProcessorIterator.next().process(this)) {
break;
}
}
} catch (Exception e) {
// 3. 期间如果出现异常,则交给内部异常渲染器处理
this.resultRender = new InternalErrorResultRender(e.getMessage());
log.error("doRequestProcessorChain error: ", e);
}
}
public void doRender() {
// 1. 如果请求处理器实现类均未选择合适的渲染器,则使用默认的
if (this.resultRender == null) {
this.resultRender = new DefaultResultRender();
}
// 2. 调用渲染器的render方法对结果进行渲染
try {
this.resultRender.render(this);
} catch (Exception e) {
log.error("doRender error: ", e);
throw new RuntimeException(e);
}
}
}
2.2.2 RequestProcessorChain相关代码讲解如下:
3. 第二部分需要完成的内容: 结果渲染器的编写
3.1 DefaultResultRender类的编写及讲解:
3.2 InternalErrorResultRender类的编写及讲解:
3.3 JsonResultRender类的编写及讲解:
3.4 ResourceNotFoundResultRender类的编写及讲解:
3.5 ViewResultRender类的编写及讲解:
github地址:https://github.com/wuyiccc/helloframework/