Java字符串模板替换(模板渲染)
java渲染字符串模板,也就是说在java字符串模板中设置变量字符串,使⽤变量去渲染指定模板中设置好的变量字符串。下⾯介绍4种替换模板⽅式:
1、使⽤内置String.format
String message = String.format("您好%s,晚上好!您⽬前余额:%.2f元,积分:%d", "张三", 10.155, 10);
System.out.println(message);
//您好张三,晚上好!您⽬前余额:10.16元,积分:10
2、使⽤内置MessageFormat
String message = MessageFormat.format("您好{0},晚上好!您⽬前余额:{1,number,#.##}元,积分:{2}", "张三", 10.155, 10);
System.out.println(message);
/
/您好张三,晚上好!您⽬前余额:10.16元,积分:10
3、使⽤⾃定义封装
。。。。。
private static Matcher m = Patternpile("\\$\\{\\w+\\}").matcher(template);
。。。。。public static String processTemplate(String template, Map<String, Object> params){
StringBuffer sb = new StringBuffer();
while (m.find()) {
String param = m.group();
Object value = (param.substring(2, param.length() - 1));
m.appendReplacement(sb, value==null ? "" : String());
}
m.appendTail(sb);
String();
}
public static void main(String[] args){
Map map = new HashMap();
map.put("name", "张三");
map.put("money", String.format("%.2f", 10.155));
map.put("point", 10);
message = processTemplate("您好${name},晚上好!您⽬前余额:${money}元,积分:${point}", map);
System.out.println(message);
//您好张三,晚上好!您⽬前余额:10.16元,积分:10
}
4、使⽤模板引擎freemarker
⾸先引⼊freemarker.jar,这⾥以2.3.23版本为例,如果使⽤maven的配置l
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
try {
map = new HashMap();
map.put("name", "张三");
map.put("money", 10.155);
map.put("point", 10);
Template template = new Template("strTpl", "您好${name},晚上好!您⽬前余额:${money?string(\"#.##\")}元,积分:${point}", new Configuration(new Versio    StringWriter result = new StringWriter();
template.process(map, result);
System.out.String());
//您好张三,晚上好!您⽬前余额:10.16元,积分:10
}catch(Exception e){
e.printStackTrace();
}
plate.Configuration;
plate.Template;
plate.Version;
import java.io.StringWriter;
MessageFormat;
import java.util.HashMap;
import java.util.Map;
import Matcher;
import Pattern;
/**
* Created by cxq on 2018-01-07.
*/
public class Tpl {
public static Configuration cfg;
static {
cfg = new Configuration(new Version("2.3.23"));
}
public static void main(String[] args) {
Object[] obj = new Object[]{"张三", String.format("%.2f", 10.155), 10};
System.out.println(processFormat("您好%s,晚上好!您⽬前余额:%s元,积分:%d", obj));
System.out.println(processMessage("您好{0},晚上好!您⽬前余额:{1}元,积分:{2}", obj));
Map map = new HashMap();
map.put("name", "张三");
map.put("money", String.format("%.2f", 10.155));
replaceall()map.put("point", 10);
System.out.println(processTemplate("您好${name},晚上好!您⽬前余额:${money}元,积分:${point}", map));
System.out.println(processFreemarker("您好${name},晚上好!您⽬前余额:${money}元,积分:${point}", map));
}
/**
* String.format渲染模板
* @param template 模版
* @param params  参数
* @return
*/
public static String processFormat(String template, params) {
if (template == null || params == null)
return null;
return String.format(template, params);
}
/**
* MessageFormat渲染模板
* MessageFormat渲染模板
* @param template 模版
* @param params  参数
* @return
*/
public static String processMessage(String template, params) {
if (template == null || params == null)
return null;
return MessageFormat.format(template, params);
}
/**
* ⾃定义渲染模板
* @param template 模版
* @param params  参数
* @return
*/
public static String processTemplate(String template, Map<String, Object> params) {        if (template == null || params == null)
return null;
StringBuffer sb = new StringBuffer();
Matcher m = Patternpile("\\$\\{\\w+\\}").matcher(template);
while (m.find()) {
String param = m.group();
Object value = (param.substring(2, param.length() - 1));
m.appendReplacement(sb, value == null ? "" : String());
}
m.appendTail(sb);
String();
}
/**
* Freemarker渲染模板
* @param template 模版
* @param params  参数
* @return
*/
public static String processFreemarker(String template, Map<String, Object> params) {        if (template == null || params == null)
return null;
try {
StringWriter result = new StringWriter();
Template tpl = new Template("strTpl", template, cfg);
tpl.process(params, result);
String();
} catch (Exception e) {
return null;
}
}
}
综合,完整⽰例:
plate.Configuration;
plate.Template;
plate.Version;
import java.io.StringWriter;
MessageFormat;
import java.util.HashMap;
import java.util.Map;
import Matcher;
import Pattern;
/**
* Created by cxq on 2018-01-07.
*/
public class Tpl {
public static Configuration cfg;
static {
cfg = new Configuration(new Version("2.3.23"));
}
public static void main(String[] args) {
Object[] obj = new Object[]{"张三", String.format("%.2f", 10.155), 10};
System.out.println(processFormat("您好%s,晚上好!您⽬前余额:%s元,积分:%d", obj));
System.out.println(processMessage("您好{0},晚上好!您⽬前余额:{1}元,积分:{2}", obj));
Map map = new HashMap();
map.put("name", "张三");
map.put("money", String.format("%.2f", 10.155));
map.put("point", 10);
System.out.println(processTemplate("您好${name},晚上好!您⽬前余额:${money}元,积分:${point}", map));        System.out.println(processFreemarker("您好${name},晚上好!您⽬前余额:${money}元,积分:${point}", map));    }
/**
* String.format渲染模板
* @param template 模版
* @param params  参数
* @return
*/
public static String processFormat(String template, params) {
if (template == null || params == null)
return null;
return String.format(template, params);
}
/**
* MessageFormat渲染模板
* @param template 模版
* @param params  参数
* @return
*/
public static String processMessage(String template, params) {
if (template == null || params == null)
return null;
return MessageFormat.format(template, params);
}
/**
* ⾃定义渲染模板
* @param template 模版
* @param params  参数
* @return
*/
public static String processTemplate(String template, Map<String, Object> params) {
if (template == null || params == null)
return null;
StringBuffer sb = new StringBuffer();
Matcher m = Patternpile("\\$\\{\\w+\\}").matcher(template);
while (m.find()) {
String param = m.group();
Object value = (param.substring(2, param.length() - 1));
m.appendReplacement(sb, value == null ? "" : String());
}
m.appendTail(sb);
m.appendTail(sb);
String();
}
/**
* Freemarker渲染模板
* @param template 模版
* @param params  参数
* @return
*/
public static String processFreemarker(String template, Map<String, Object> params) {        if (template == null || params == null)
return null;
try {
StringWriter result = new StringWriter();
Template tpl = new Template("strTpl", template, cfg);
tpl.process(params, result);
String();
} catch (Exception e) {
return null;
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。