问题描述
我想同时实现这两件事.
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 页面的结果作为操作类中的字符串获取(用于电子邮件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!