velocity tools1.x(4)中的handleRequest解析

我们现在使用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 Templates 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?


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欧米优

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值