English 中文(简体)
使用String replace()或replaceAll()配置动态电子邮件内容
原标题:Configuring dynamic email content with String replace() or replaceAll()

I am using Spring MVC for my web application and I am using my applicationContext.xml file to configure my emails which I am injecting into my controllers in my spring-servlet.xml file.

我需要发送的一些电子邮件需要根据发送给的客户进行定制。一旦将电子邮件文本注入控制器并发送,就需要填写电子邮件中的某些信息(名字、姓氏、电话号码等)。

下面的bean中显示了一个示例

<bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage">
   <property name="from" value="from@no-spam.com" />
   <property name="to" value="to@no-spam.com" />
   <property name="subject" value="Testing Subject" />
   <property name="text">
      <value>
         Dear %FIRST_NAME%

                 Blah Blah Blah Blah Blah...
                 We Understand that we can reach you at the following information

                 Phone:%PHONE%
                 Address:%ADDRESS%
      </value>
   </property>
</bean>

这将是一个自定义的电子邮件消息,我将定义并注入到我的控制器中。然后,我的控制器中的代码将根据从客户那里收集的输入填写值,因此控制器将具有类似于以下的代码

    //SimpleMailMessage property is injected into controller
    private SimpleMailMessage simpleMailMessage;

    //Getters and Setters for simpleMailMessage;


    MimeMessage message = mailSender.createMimeMessage();

   try{
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(simpleMailMessage.getFrom());
        helper.setTo(simpleMailMessage.getTo());
        helper.setSubject(simpleMailMessage.getSubject());

                String text = simpleMailMessage.getText();
                text.replace("%FIRST_NAME%",model.getFirstName());
                text.replace("%PHONE%",model.getPhone());
                text.replace("%ADDRESS%",model.getAddress());
        helper.setText(simpleMailMessage.getText());
     }
         catch (MessagingException e) {
    throw new MailParseException(e);
     }
     mailSender.send(message);**strong text**

我遇到的问题是,当我尝试替换诸如%FIRST_NAME%%PHONE%%ADRESS%> ,它没有替换它。我不确定这是因为我使用了replace()错误,还是因为它因为注入了值而对它进行了不同的处理。我还尝试过使用replaceAll(),但这也不起作用。如果有人对此有更好的想法,请告诉我。

非常感谢。

最佳回答

Don tabes, in Java Strings are immutable. i.e. 您可以修改这些内容,而是从旧版本中产生新的变化(见http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replace All%28java.lang.String,%20java.lang.String%29” rel=“nofollow” replaceAll( 并注意到收益价值)。

因此,replace()不会更改被调用的字符串,而是会返回一个新的字符串并进行替换。您可以使用返回值,因此只需将这些调用链接在一起:

String newString = oldString.replace(..).replace(...);

如果您需要做大量的模板工作,您可能会对Apache VelocityFreemark。它们是专门构建的模板引擎,可以通过更多选项(例如提供循环、格式、条件等)来完成您正在做的事情

问题回答

我建议不要通过构建自己的模板系统来重新发明轮子。使用Apache Velocity或其他库-它们提供了比任何自制解决方案更多的功能、更强大、更高的性能。

Spring对Velocity有很好的支持,我在许多Spring MVC应用程序中使用了Velocity模板(用于电子邮件模板和web模板),没有出现任何问题。

我想在这里介绍一下Rythm模板引擎。以下是关于这项工作的几点:

  • Razor like synatx, very clean and friendly to Java programmer
  • Compiled to Java byte code thus is very fast. 2 to 3 times faster than velocity
  • It covers all features provided with Velocity and much more. See the full feature set demonstration hosted at google application engine
  • The API is simplest among other Java template engines (velocity, freemarker ...)

示例1,使用模板文件渲染,并按位置传递参数:

String result = Rythm.render("/path/to/my/template.txt", foo, bar, ...);

示例2,使用模板文件渲染,并按名称传递参数:

Map<String, Object> args = new HashMap<String, Object>();
args.put("foo", foo);
args.put("bar", bar);
...
String result = Rythm.render("/path/to/my/template.txt", args);

示例3,使用字符串内容进行渲染,并按位置传递参数:

String result = Rythm.render("@args User user;User name is @user.name", user);

示例4,在字符串插值模式

String result = Rythm.render("User name is @name", user.name);

链接:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签