2014-10-01 55 views
1

我使用JSP頁面來作爲電子郵件模板從我的動作類發送重定向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("[email protected]", result1,"AssignMail"); 
    TARGET = "success"; 

} 

我打電話sendMailNewcheckAndMail用於發送郵件。我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)從checkAdnMailxxx.jsp

這個動作類被重定向到yyy.jsp爲進入hashcode應該發送到用戶的電子郵件地址。

參考:Struts2 - How can I get the result of a JSP page as a string in an action class (for emails)

+0

請參閱http://stackoverflow.com/q/13623220/1700321。 – 2014-10-01 12:16:39

回答

1

可以傳遞使用請求屬性的值。例如

request.setAttribute("hashcode", hashcode); 

然後在JSP你在Struts標籤引用一個請求對象或者只使用JSP EL

<s:property value="#request.hashcode"/> 

${hashcode} 

的最後一個值是沒有逃脫,如果代碼中有錯誤的字符,請小心謹慎。

+0

當我使用$ {hashcode}時,它顯示jsp頁面中的錯誤爲「根據標記文件中的TLD或Attributive指令,屬性值不接受任何表達式」.... 沒有爲我工作它顯示空值.. – cc4code 2014-10-01 13:21:06

+0

不要在JSP標籤中使用JSP EL,因爲第二你可以使它工作。 – 2014-10-01 13:28:14

+0

感謝您的幫助,它爲我工作.. – cc4code 2014-10-02 05:40:54