简体   繁体   English

Struts2 - 如何将JSP页面的结果作为动作类中的字符串获取(对于电子邮件)

[英]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. 我在Struts2中有一个常规的jsp页面。 xx/yy/zz/email.jsp 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... 这个页面的url可能是xx / yy / zz / myEmail.action,而有些动作类会处理它...

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. 现在,我想要另一个动作,它将xx / yy / zz / myEmail.action的页面结果作为电子邮件发送。

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? 所以问题在于:我如何获得一个RENDERED JSP的结果?

This reason why I want this is obviously that I want to manage this template in one place (email.jsp). 这就是我想要这个的原因显然是我想在一个地方管理这个模板(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. 我知道我可以使用另一个速度(vm)页面,该页面具有与jsp完全相同的html但是使用速度标记。 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. 我想我也可以使用URL来获取结果,但我不想使用这种方式,因为它是对服务器的另一个请求。

Thanks 谢谢

I had this problem with using a mailing servlet use this to get the result of the jsp as a string: 我有一个使用邮件servlet的问题,使用它来获取jsp的结果作为字符串:

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: 设置电子邮件以提供html内容:

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 这不是您问题的答案,但我建议您考虑使用模板引擎撰写电子邮件,例如Velocity或Jelly

http://velocity.apache.org/ http://velocity.apache.org/

http://commons.apache.org/proper/commons-jelly/ 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). 通过这种方式,您可以将模板组合为HTML或XML,并向其中注入您的逻辑可能检索到的任何相关数据(甚至可以将某些逻辑集成在他们自己的模板语言中,例如if-then-else或while-loop结构)。

In the very worst case that is completely necessary to render the full JSP, you could hack something on 在最糟糕的情况下,渲染完整的JSP是完全必要的,你可以破解一些东西

RequestDispatcher::include RequestDispatcher的::包括

You could execute this method from your MyEmailAction, but passing it a hacked ServletResponse. 您可以从MyEmailAction执行此方法,但将其传递给被攻击的ServletResponse。 This would be some class written by you implementing ServletResponse... but writing the result in some ByteArrayOutputStream . 这将是您实现ServletResponse编写的一些类...但是将结果写入一些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) 在页面呈现之后(在您的假ServletResponse上),您可以从那里检索它(实际上使用servlet容器作为您自己的模板引擎)

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

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