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

2023-10-15Java开发问题
1

本文介绍了Struts2 - 如何将 JSP 页面的结果作为操作类中的字符串获取(用于电子邮件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想同时实现这两件事.

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

我在 Struts2 中有一个常规的 jsp 页面.xx/yy/zz/email.jsp

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

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

这个页面的url可以是xx/yy/zz/myEmail.action,而一些action类会处理它...

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'
    ......
}

现在,我想做另一个动作,将 xx/yy/zz/myEmail.action 页面的结果作为电子邮件发送.

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'
    ......
}

所以问题是:如何将渲染的 JSP 的结果作为字符串获取?

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

我想要这个的原因显然是我想在一个地方管理这个模板(email.jsp).

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

我知道我可以使用另一个速度 (vm) 页面,它与 jsp 具有完全相同的 html,但使用速度标记.但是每当我需要对这个模板进行更改时,我都必须在两个地方都这样做.

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.

我想我也可以使用 URL 来获取结果,但我不喜欢使用这种方式,因为它是对服务器的另一个请求.

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.

谢谢

推荐答案

我在使用邮件 servlet 时遇到了这个问题,使用它来获取 jsp 的结果作为字符串:

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

设置邮箱给html内容:

Set the email to give html content:

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

这篇关于Struts2 - 如何将 JSP 页面的结果作为操作类中的字符串获取(用于电子邮件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13