我正在使用 JSP 页面从我的操作类作为电子邮件模板发送
即aaa.jsp
接受用户的email_id,然后重定向到邮件操作,将hashcode
发送给用户,然后重定向到yyy.jsp
以输入发送到用户提供的emil_id
的hashcode
。我使用 xxx.jsp
作为电子邮件模板。
我的xxx.jsp
是
<html>
<head>
</head>
<body>
<s:property value="hashcode"/>
</body>
</html>
我有一个操作类,它向用户发送一封电子邮件,其中包含用于注册目的的身份验证代码。
checkAndMail
用于检查该邮件是否已经注册,并将xxx.jsp
页面转换为字符串
public String checkAndMail() throws Exception{
this.hashcode="9048075352";//getters and setters are used for hashxode and is of type String
/***********
code for searching whether the email is registered already
************/
HttpServletResponse response = ServletActionContext.getResponse();
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();
}
};
setHashcode(hashcode);
request.getRequestDispatcher("aa/bb/xxx.jsp").include(request,
responseWrapper);
String result1 = responseWrapper.toString();
sendMailNew("christinchacko@gmail.com", result1,"AssignMail" );
TARGET = "success";
}
我从 checkAndMail
调用 sendMailNew
来发送邮件。我用于发送邮件的 sendMailNew
方法是
public synchronized void sendMailNew(String mailTo,String Content,String type) throws Exception
{
String TEXT="";
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.user", USER);
props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.starttls.enable", STARTTLS);
props.put("mail.smtp.debug", DEBUG);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.fallback", "false");
try {
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
//message.setText(TEXT);
if(type.equals("AssignMail"))
{
message.setContent(Content, "text/html");
}
message.setSubject(SUBJECT);
message.setFrom(new InternetAddress(FROM));
message.addRecipients(RecipientType.TO, mailTo);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(HOST, USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
throw new Exception("Exception in Mail.sendMail" + e.getMessage());
}
}
我的问题是如何将一个值(这里参数是hashcode
)从checkAdnMail
传递到xxx.jsp
此操作类被重定向到 yyy.jsp
以输入应发送到用户电子邮件地址的哈希码
。
引用:Struts2 - How can I get the result of a JSP page as a string in an action class (for emails)
最佳答案
您可以使用请求属性传递值。例如
request.setAttribute("hashcode", hashcode);
然后在 JSP 中,您必须在 struts 标记中引用请求对象,或者只使用 JSP EL
<s:property value="#request.hashcode"/>
或
${hashcode}
最后一个值没有转义,如果代码中有错误的字符,请小心。
关于java - 将值从操作发送到未从操作重定向的 jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26141077/