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

I want to achieve two 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 might 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 get another action that sends the result of the page xx / yy / zz / myEmail.action as an 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: HOW CAN I GET THE RESULT OF THE RENDERED JSP AS A STRING?

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

I know I can use another speed page (vm) which has the same html as jsp but with speed markups. But then, when I need to make changes to this template, I have to do it in both places.

I think I could also use the url to get the result, but I prefer not to use that way as this is another request to the server.

thank

+3


source to share


2 answers


I'm having a problem using a mailing list servlet to get the jsp result 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 address to serve html content:

Email.send('me@me.com', 'you@you.com', 'My Subject', result);

      

+4


source


This is not an answer to your question, but I suggest you use a templating engine to compose your emails like Velocity or Jelly

http://velocity.apache.org/

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

This way you can create your templates as HTML or XML and inject whatever relevant data your logic might receive (and even some logic built into your own templating languages, like if-then-else or while-loop structures) ...



In the worst case, which is necessary for a full JSP, you can hack something

RequestDispatcher :: include

You can execute this method from MyEmailAction, but passing the hacked ServletResponse to it. It will be some class you wrote while implementing ServletResponse ... but writing the result in some ByteArrayOutputStream

. After the page is rendered (on your fake ServletResponse) you can simply fetch it from there (actually using the servlet container as your own templating engine)

+1


source







All Articles