Struts2 - 如何将JSP页面的结果作为动作类中的字符串(对于电子邮件) [英] Struts2 - How can I get the result of a JSP page as a string in an action class (for emails)
问题描述
我想同时实现两件事。
我在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>
此页面的网址可能是xx / yy / zz / myEmail.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'
......
}
所以问题在于:我如何获得一个RENDERED 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页面的结果作为动作类中的字符串(对于电子邮件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!