简体   繁体   English

在自定义JSP模板中使用Struts 2 taglib实施邮件模板

[英]Implementing mail templates using Struts 2 taglib in custom JSP template

I'm building an app that leverages Struts2 as the primary framework, but one of the requirements is that the app sends automated emails. 我正在构建一个使用Struts2作为主要框架的应用程序,但其中一项要求是该应用程序发送自动电子邮件。

Since Struts2 doesn't include much helpfulness out-of-the-box in terms of implementing mail templates, I decided to just use normal JSP, like this: 由于Struts2在实现邮件模板方面没有开箱即用的功能,因此我决定只使用普通的JSP,如下所示:

public static String parseJSPTemplate(String jspPath, HttpServletRequest req) throws ServletException, IOException {
    RequestDispatcher jsp = req.getRequestDispatcher(jspPath);
    EmailTemplateHandler tpl = new EmailTemplateHandler();
    jsp.forward(req, tpl);
    return tpl.toString();
}

So, this processes the JSP in a normal way and returns a string representation of the output with variables replaced, etc. 因此,这将以常规方式处理JSP,并返回输出的字符串表示形式,其中包含替换的变量等。

One would think that including the Struts2 taglib per usual (eg <%@ taglib prefix="s" uri="/struts-tags" %> ) would allow the use of tags like <s:url /> ; 人们会认为 ,通常包含Struts2 <%@ taglib prefix="s" uri="/struts-tags" %> (例如<%@ taglib prefix="s" uri="/struts-tags" %> )将允许使用<s:url />类的标签; however, it seems that this is not the case. 但是,似乎并非如此。 Instead, the result of the call to the taglib is blank. 相反,对taglib的调用结果为空白。 No errors are displayed nor exceptions thrown. 不显示错误,也不会引发异常。

My guess is that something else needs to be injected into the request before Struts2 will properly respond to calls, but I'm not sure what this would be. 我的猜测是在Struts2正确响应调用之前,需要向请求中注入其他内容,但是我不确定这会是什么。

Anyone have an idea on how to get this working? 有人对如何使它起作用有想法吗? Other taglibs like JSTL-core work fine. 像JSTL-core这样的其他标记库也可以正常工作。

Struts tags are working if the ActionContext is available. 如果ActionContext可用,Struts标记将起作用。 Normally it's created by the Dispatcher in prepare operations. 通常,它是由Dispatcher在准备操作中创建的。 So, the struts2 filter should be mapped to accept DispatcherType.FORWARD requests to be invoked on your forward. 因此,应该将struts2过滤器映射为接受DispatcherType.FORWARD请求,以便在您的转发中调用该请求。 You can also get the action context via ActionContext.getContext() in the JSP for debugging. 您还可以通过JSP中的ActionContext.getContext()获取操作上下文以进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM