一步一步写一个java web开发框架(5)

一步一步写一个java web开发框架(1)

一步一步写一个java web开发框架(2)

一步一步写一个java web开发框架(3)

一步一步写一个java web开发框架(4)

承接上文,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怎么利用框架实自动注入值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值