Struts2 - How can I get the result of a JSP page as a string in an action class (for emails)

前端 未结 2 1002
我寻月下人不归
我寻月下人不归 2021-01-20 15:06

I want to achieve the 2 things at the same time.

I have a regular jsp page in Struts2. xx/yy/zz/email.jsp




<         


        
相关标签:
2条回答
  • 2021-01-20 15:27

    I had this problem with using a mailing servlet use this to get the result of the jsp as a string:

    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response) {
    private final StringWriter sw = new StringWriter();
    
    @Override
    public PrintWriter getWriter() throws IOException {
        return new PrintWriter(sw);
    }
    
    @Override
    public String toString() {
        return sw.toString();
    }
    };
    request.getRequestDispatcher("email.jsp").include(request,
        responseWrapper);
    String result = responseWrapper.toString();
    

    Set the email to give html content:

    Email.send('me@me.com', 'you@you.com', 'My Subject', result);
    
    0 讨论(0)
  • 2021-01-20 15:35

    This is not the answer to your question but I suggest you consider using a template engine to compose your emails, such as Velocity or Jelly

    http://velocity.apache.org/

    http://commons.apache.org/proper/commons-jelly/

    This way you can compose your templates as HTML or XML and inject there any relevant data your logic may have retrieved (and even some logic integrated in their own template languages, such as if-then-else or while-loop structures).

    In the very worst case that is completely necessary to render the full JSP, you could hack something on

    RequestDispatcher::include

    You could execute this method from your MyEmailAction, but passing it a hacked ServletResponse. This would be some class written by you implementing ServletResponse... but writing the result in some ByteArrayOutputStream. After the page is rendered (on your fake ServletResponse) you could just retrieve it from there (actually using the servlet container as your own template engine)

    0 讨论(0)
提交回复
热议问题