承接上文,ResponseWrapper一共处理了3中返回类型。
ModelAndView、String、JSONMap
public void doResponse(Object result) throws ServletException, IOException {
if (result instanceof ModelAndView)
dealView((ModelAndView) result);
else if (result instanceof String)
dealString(result.toString());
else if (result instanceof JSONMap)
dealJSON((JSONMap) result);
else
throw new ServletException("不支持的返回类型 " + result.getClass().getName());
}
ModelAndView处理所有jsp页面
private void dealView(ModelAndView mav) throws ServletException {
mav.fillRequest(request);
JSPWrapper.processJsp(mav.getViewName(), request, response);
if (logger.isDebugEnabled())
logger.debug("Forward to " + mav.getViewName());
}
String则会输出字符串
private void dealString(String str) throws ServletException, IOException {
String text = str.toString();
response.setContentType(STR.CONTENT_TYPE_TEXT);
writeResponse(response, request, text);
if (logger.isDebugEnabled())
logger.debug("[TEXT]" + text);
}
JSONMap会输出json
private void dealJSON(JSONMap result) throws ServletException, IOException {
JSONWrapper jsonWrapper = new JSONWrapper();
String json = jsonWrapper.write(result);
String jsonp = request.getParameter("callback");
boolean isEmpty = StringUtils.isEmpty(jsonp);
if (isEmpty)
response.setContentType(STR.CONTENT_TYPE_JSON);
else {
json = jsonp + "(" + json + ")";
response.setContentType(STR.CONTENT_TYPE_TEXT);
}
writeResponse(response, request, json);
if (logger.isDebugEnabled()) {
if (json.length() > maxPrintLog)
json = json.substring(0, maxPrintLog) + "...";
if (isEmpty)
logger.debug("[JSON]" + json);
else
logger.debug("[JSONP]" + json);
}
}
如果返回值为Void,那就是自定义的response,不需要ResponseWrapper处理。
至此,一个基本的开发框架就出来了,通过扫描所有类文件,自动查找Action,然后通过匹配输入的链接,执行找到的函数,按返回值输出结果,感觉怎么样?
下一节,将会说明已定义的service、dao怎么利用框架实自动注入值。