Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java字符串表达式解析器_Java_String_Expression_Velocity - Fatal编程技术网

java字符串表达式解析器

java字符串表达式解析器,java,string,expression,velocity,Java,String,Expression,Velocity,我被要求在字符串中包含数学表达式 说:“价格:${price},税:${price}*${tax)” 字符串在运行时给出,映射值也是 我使用了速度: 马文: <properties> <velocity.version>1.6.2</velocity.version> <velocity.tools.version>2.0</velocity.tools.version> </pro

我被要求在字符串中包含数学表达式 说:“价格:${price},税:${price}*${tax)”

字符串在运行时给出,映射值也是

我使用了速度:

马文:

    <properties>
        <velocity.version>1.6.2</velocity.version>
        <velocity.tools.version>2.0</velocity.tools.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>${velocity.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>${velocity.tools.version}</version>
        </dependency>
    </dependencies>

1.6.2
2
org.apache.velocity
速度
${velocity.version}
org.apache.velocity
速度工具
${velocity.tools.version}
爪哇:

公共类VelocityUtils{
公共静态字符串mergeTemplateIntoString(字符串模板,映射模型)
{
尝试
{
最终速度发动机ve=新速度发动机();
ve.init();
最终VelocityContext上下文=新VelocityContext();
put(“math”,新的MathTool());
put(“number”,newnumbertool());
对于(最终的Map.Entry:model.entrySet())
{
最后一个字符串macroName=entry.getKey();
put(宏名,entry.getValue());
}
最终StringWriter wr=新StringWriter();
最终字符串logStr=“”;
评估(上下文、wr、logStr、模板);
返回wr.toString();
}捕获(例外e)
{
返回“”;
}
}
}
测试等级:

public class VelocityUtilsTest
{
    @Test
    public void testMergeTemplateIntoString() throws Exception
    {
        Map<String,String> model = new HashMap<>();
        model.put("price","100");
        model.put("tax","22");
        String parsedString = VelocityUtils.mergeTemplateIntoString("price: ${price} tax: ${tax}",model);
        assertEquals("price: 100 tax: 22",parsedString);

        String parsedStringWithMath = VelocityUtils.mergeTemplateIntoString("price: $number.integer($math.div($price,2))",model);
        assertEquals("price: 50",parsedStringWithMath);

    }
}
公共类VelocityUtilsTest
{
@试验
public void testMergeTemplateIntoString()引发异常
{
映射模型=新的HashMap();
模型卖出价(“价格”、“100”);
模型卖出价(“税”、“22”);
String parsedString=VelocityUtils.mergeTemplateIntoString(“价格:${price}tax:${tax}”,模型);
资产质量(“价格:100税:22”,解析字符串);
字符串parsedStringWithMath=VelocityUtils.mergeTemplateIntoString(“价格:$number.integer($math.div($price,2))”,模型);
资产质量(“价格:50”,用数学分析);
}
}

使用SPel会更好吗?

我同意这有点离题,但我认为它还是值得回答的

使用模板引擎的整个想法是,您需要在运行时访问模板。如果是这样的话,那么当然,Velocity是一个不错的选择。然后您可以提供HTML的新版本,并且假设您没有更改所使用的变量,则不必提供应用程序本身的新版本(重新编译)


但是,如果您只是使用Velocity来节省时间,那么在这里节省的并不多:您可以使用StringTokenizer在几行代码中完成这项工作。

这个问题似乎与主题无关,因为它是关于工作代码的。关于工作代码的问题可能适合codereview.stackexchange.com。
public class VelocityUtilsTest
{
    @Test
    public void testMergeTemplateIntoString() throws Exception
    {
        Map<String,String> model = new HashMap<>();
        model.put("price","100");
        model.put("tax","22");
        String parsedString = VelocityUtils.mergeTemplateIntoString("price: ${price} tax: ${tax}",model);
        assertEquals("price: 100 tax: 22",parsedString);

        String parsedStringWithMath = VelocityUtils.mergeTemplateIntoString("price: $number.integer($math.div($price,2))",model);
        assertEquals("price: 50",parsedStringWithMath);

    }
}