我正在设计 JSP 应用程序,我想打印用户输入。我需要像<c:out>
标签这样的东西,但需要一个允许用户进行一些格式化的东西。
首先,我想翻译\n
成,<br />
但我仍然需要提供所有 XML 转义<c:out>
。然后我意识到允许更多的格式化会很好,比如 BBcode 或 Wiki 语法。
是否有任何允许这样做的 JSP 标记库?
为什么不使用 xml 创建自己的标签
http://www.java2s.com/Code/Java/JSP/Createyourowntagacustomtagbody.htm
XML 文件:
<tag>
<description>
Escapes a String to HTML, either writing it to the response
or exporting it to a scoped variable.
The string may be the value attribute or the tag body.
</description>
<name>escapeHTML</name>
<tag-class>nl.strohalm.cyclos.taglibs.EscapeHTMLTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description>
The string value to be escaped. If not set, the tag body will be used instead.
</description>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Object</type>
</attribute>
<attribute>
<description>If set to true, will only replace line breaks for br tags, ignoring other processing. Defaults to false</description>
<name>brOnly</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>boolean</type>
</attribute>
<attribute>
<description>
A context variable name. If provided, instead of writing the escaped value to the response
output, it will be set on the scope determined by the scope attribute, under this variable name
</description>
<name>var</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>
The scope name for the variable to be exported.
May be one of: page, request, session or application.
Defaults to page.
</description>
<name>scope</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
然后你写一点你想要实现的java代码!
例如对应于简单java代码示例上面的标签:
public static String escape(final String string, boolean brOnly) {
String out = string;
if (!brOnly) {
out = StringEscapeUtils.escapeHtml(out);
}
out = StringUtils.replace(out, "\n", "<br />");
out = StringUtils.replace(out, "\\n", "<br />");
return out;
}
你有没有想过在你需要的地方嵌入像 CKEditor 这样的东西?
这是一个很棒的工具,但请确保许可证符合您的要求:
TinyMCE 是一个轻量级的替代方案:
您还可以考虑使用类似 wiki 的编辑器,例如 stackoverflow 中使用的编辑器: