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

帅比萌擦擦* 提交于 2019-12-20 02:37:11

问题


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

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

<html>
<head>
</head>
<body>
    <s:property value="email"/>
</body>
</html>

The url of this page could be xx/yy/zz/myEmail.action, while some action class will handle it...

public class MyEmailAction {
    private String email;
    public String execute(){
        this.email = 'abc@xyz.com''
    }
    //setter and getter for 'email'
    ......
}

Now, I would like to have another action, which sends the result of the page of xx/yy/zz/myEmail.action as email.

public class MyEmailAction {
    private String email;
    public String execute(){
        this.email = 'abc@xyz.com''
    }

    public String send() {
        this.email = 'abc@xyz.com''
        Map mapping;
        //put this.email into the mapping
        String result = getResultOfJSP('../../../xx/yy/zz/email.jsp', mapping);
        Email.send('me@me.com', 'you@you.com', 'My Subject', result);
    }
    //setter and getter for 'email'
    ......
}

So the question is that: HOW CAN I GET THE RESULT OF A RENDERED JSP AS A STRING?

This reason why I want this is obviously that I want to manage this template in one place (email.jsp).

I know I could use another velocity (vm) page which has exact the same html as the jsp but with velocity markups instead. But then whenever I need to make a change to this template, I have to do it on both places.

I think I could also use URL to grab the result, but I prefer not to use this way as it's another request to the server.

Thanks


回答1:


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);



回答2:


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)



来源:https://stackoverflow.com/questions/25936387/struts2-how-can-i-get-the-result-of-a-jsp-page-as-a-string-in-an-action-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!