12 Replies Latest reply on May 1, 2007 9:47 PM by Glenn Marcus

    Renderer is not resolving a variable

    Glenn Marcus Newbie

      I need to send an email to a user when they register on my website that includes a link back to the website to confirm their email address. I want to dynamically build the URL depending on the server it is deployed to.

      I am trying to use a variable defined in components.xml:

      <factory name="basePath" value="#{facesContext.externalContext.request.scheme}://#{facesContext.externalContext.request.serverName}:#{facesContext.externalContext.request.serverPort}#{facesContext.externalContext.request.contextPath}/"/>
      

      NOTE: This text is all one line in components.xml. The forum is wrapping the text here for display purposes.

      I have a SLSB that is invoking Renderer to send an email to the user:
       public String register()
       {
       List existing = em.createQuery("select u.username from User u where u.username=#{user.username}")
       .getResultList();
      
       if ( existing.size()==0 )
       {
       // save the user
       user.setValidated(false);
       em.persist(user);
       log.info("Registered new user #{user.username}");
       FacesMessages.instance().add("An account has been created for #{user.username}.");
      
       // setup a verification token
       Token verificationToken = new Token(user.getUsername());
       em.persist(verificationToken);
      
       // place the token hash into the session for use by email verification
       Contexts.getSessionContext().set("outtoken", verificationToken.getHash());
       log.info("Created a new Token for #{user.username}: #0", verificationToken.getHash());
      
       // send an email verification
       try
       {
       renderer.render("/emailverification.xhtml");
       FacesMessages.instance().add("An email has been sent to #{user.email}. Please follow the instructions sent to you via email to complete the registration process.");
       }
       catch (Exception e)
       {
       log.error("Error sending mail", e);
       FacesMessages.instance().add("There was a problem sending your verification email. Please try again later.");
       }
      
       return "success";
       }
       else
       {
       FacesMessages.instance().add("User #{user.username} already exists");
       return null;
       }
       }
      


      emailverification.xhtml
      <m:message xmlns="http://www.w3.org/1999/xhtml"
       xmlns:m="http://jboss.com/products/seam/mail"
       xmlns:s="http://jboss.com/products/seam/taglib"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html">
      
      
       <m:from name="Support" address="no-reply@example.com" />
       <m:to name="#{user.name}">#{user.email}</m:to>
       <m:subject>Email Verification</m:subject>
       <m:body>
       <p><h:outputText value="Dear #{user.name}" />,</p>
       <p>Thank you for registering for a new account.
       In order to use your account,
       you must complete your registration by clicking on the link below.</p>
       <p><a href="#{basepath}/validate.seam?tk=#{outtoken}">Click here</a> to complete your registration.</p>
       <p>Regards,</p>
       <p>Glenn</p>
       </m:body>
      </m:message>
      


      Here is a sample of the email that is generated:
      Dear Bob,
      
      Thank you for registering for a new account. In order to use your account,
      you must complete your registration by clicking on the link below.
      
      /validate.seam?tk=gnqerr
      
      Regards,
      
      Glenn
      



      The raw email message shows that the href is not set correctly:
      From: Support <no-reply@example.com>
      To: Bob <me@glennmarcus.com>
      Subject: Email Verification
      Mime-Version: 1.0
      Content-Type: multipart/mixed; boundary="----=_Part_28_7998763.1178031447360"
      Date: Tue, 1 May 2007 10:57:27 -0400 (EDT)
      
      ------=_Part_28_7998763.1178031447360
      Content-Type: text/html; charset=ISO-8859-1
      Content-Transfer-Encoding: 7bit
      Content-Disposition: inline
      
      
       <p>Dear Bob,</p>
       <p>Thank you for registering for a new account.
       In order to use your account,
       you must complete your registration by clicking on the link below.</p>
       <p><a href="/validate.seam?tk=gnqerr">/validate.seam?tk=gnqerr</a></p>
       <p>Regards,</p>
       <p>Glenn</p>
      ------=_Part_28_7998763.1178031447360--
      



      The other variables #{user.name}, #{user.email}, #{outtoken} ) all get resolved correctly. The #{basepath} does not.

      1) Why doesn't #{basepath} resolve?
      2) Is there a better way to dynamically build an href in an email message being sent by Renderer?

      Thanks,
      Glenn