我们现在使用velocity tools时都是利用override其中的handleRequest方法来完成的,例如:
public class SampleServlet extends VelocityViewServlet {
/**
*
*/
private static final long serialVersionUID = -6658627452205989765L;
public Template handleRequest(HttpServletRequest request,
HttpServletResponse response, Context context) {
String[] name = { "czy", "xxx", "fdsf" };
context.put("theList", name);
Template template = null;
try {
template = getTemplate("sample.vm");
} catch (ResourceNotFoundException rnfe) {
// couldn't find the template
} catch (ParseErrorException pee) {
// syntax error : problem parsing the template
} catch (Exception e) {
}
return template;
}
}
看了源代码,发现注释部分有这么一句话
This will be removed in VelocityTools 2.0.
很明显,在velocity tools2.0中,该方法将被移除。
那么,由哪个来代替呢?
This was a common extension point, but has been deprecated. It has no single replacement. Instead, you should override fillContext
to add custom things to the Context
or override a getTemplate
method to change how Template
s are retrieved
似乎就是利用重写fillContext方法和getTemplate方法来代替上面的方法。
看下源码中获取的顺序:
/**
* Handles with both GET and POST requests
*
* @param request HttpServletRequest object containing client request
* @param response HttpServletResponse object for the response
*/
protected void doRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
Context context = null;
try
{
// first, get a context
context = createContext(request, response);
fillContext(context, request);
// set the content type
setContentType(request, response);
// get the template
Template template = handleRequest(request, response, context);
// bail if we can't find the template
if (template == null)
{
velocity.warn("VelocityViewServlet: couldn't find template to match request.");
return;
}
// merge the template and context
mergeTemplate(template, context, response);
}
catch (Exception e)
{
// log the exception
velocity.error("VelocityViewServlet: Exception processing the template: "+e);
// call the error handler to let the derived class
// do something useful with this failure.
error(request, response, e);
}
finally
{
// call cleanup routine to let a derived class do some cleanup
requestCleanup(request, response, context);
}
}
不难发现,先是调用fillContext(context, request);填充数据对象
再调用Template template = handleRequest(request, response, context);获取模板
最后mergeTemplate(template, context, response);来合并模板
那getTemplate()方法重载了有什么用?
protected Template handleRequest(HttpServletRequest request,
HttpServletResponse response,
Context ctx)
throws Exception
{
return getTemplate(request, response);
}
个人见解,只是在handleRequest方法中调用到了getTemplate
那么,将重写一个方法变为两个方法有什么好处?
猜测,将数据绑定和模板绑定分离
最后,我在网上发现了这段让人吐血的话:
I think you've got a pretty good case for keeping handleRequest
around. I already gave it a reprieve in 2.0 and pushed back the
threatened removal to 2.1, but now you've got me reconsidering that.
I think your use-cases are stronger rationale for keeping it than my
thin reasons for being rid of it.
At this point, i would say keep using handleRequest. I think i'll
de-deprecate it. :)
意思就是说。。。2.1又会将这么个方法加回来,really?