简体   繁体   中英

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

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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