输出编码的特殊字符,如!

时间:2021-02-17 01:06:51

In spring framework we have something like below to escape html characters

在spring框架中,我们有一些类似于下面的东西来转义html字符。

<context-param>
    <param-name>defaultHtmlEscape</param-name>
    <param-value>true</param-value>
</context-param>

I also want to add a custom class which can also escape characters like

我还想添加一个自定义类,它也可以转义字符,比如。

!"#$%&*+/:;=?@[]^`{|}~\

is there a class in Spring framework to do this?(I think, NO) If not, I can have utility class which can convert all the characters.

Spring框架中有这样的类吗?(我想,不)如果不行,我可以有一个可以转换所有字符的实用程序类。

Used the utility class like a filter to encode the values(input validation)

使用像过滤器这样的实用程序类对值进行编码(输入验证)

I want to know If I can use the same filter to encode for output as well? issue is output still shows the same as entered value instead of encoding like &amp;LT; for <. Any suggestions are greatly appreciated!

我想知道是否可以用同样的过滤器来编码输出?问题是输出仍然显示与输入值相同,而不是像&<。非常感谢您的建议!

2 个解决方案

#1


4  

Maybe you can use Apache commons for this. It have utils for both escape and unescape

也许你可以使用Apache commons。它既能逃脱也能逃脱。

#2


1  

You can add a custom class and use StringEscapeUtils function.

您可以添加自定义类并使用StringEscapeUtils函数。

Sample code:

示例代码:

import org.apache.commons.lang.StringEscapeUtils;

public class CustomClass
{

    public static void main(String args[]){

        String testStr = "< > \" &";

        System.out.println("Original : " + testStr);

        System.out.println("Escaped : " + StringEscapeUtils.escapeHtml(testStr));

    }   
}

Reference: http://www.mkyong.com/java/how-to-escape-special-characters-in-java/

参考:http://www.mkyong.com/java/how-to-escape-special-characters-in-java/

#1


4  

Maybe you can use Apache commons for this. It have utils for both escape and unescape

也许你可以使用Apache commons。它既能逃脱也能逃脱。

#2


1  

You can add a custom class and use StringEscapeUtils function.

您可以添加自定义类并使用StringEscapeUtils函数。

Sample code:

示例代码:

import org.apache.commons.lang.StringEscapeUtils;

public class CustomClass
{

    public static void main(String args[]){

        String testStr = "< > \" &";

        System.out.println("Original : " + testStr);

        System.out.println("Escaped : " + StringEscapeUtils.escapeHtml(testStr));

    }   
}

Reference: http://www.mkyong.com/java/how-to-escape-special-characters-in-java/

参考:http://www.mkyong.com/java/how-to-escape-special-characters-in-java/