java - 将值从操作发送到未从操作重定向的 jsp

标签 java jsp email struts2 ognl

我正在使用 JSP 页面从我的操作类作为电子邮件模板发送

aaa.jsp接受用户的email_id,然后重定向到邮件操作,将hashcode发送给用户,然后重定向到yyy.jsp以输入发送到用户提供的emil_idhashcode。我使用 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/

相关文章:

java - 如何组织和命名包

java - 将数组的随机字母保存为字符串

c# - Hector 中的 KeySpace 和 KeySpace 定义有什么区别?

java - 保持oracle连接存活,访问oracle

ruby - 在没有电子邮件的情况下在 ruby​​ 上使用电子邮件

mysql - 根据连接表的结果更新 MySQL 表

javascript - mailto : supported? 有多好

java - 具有条件状态更改的状态模式

jsp - jsp中的格式编号

java - JSP/JSTL 在表格中自动换行