<c:out>标签的替代方案,允许更多格式化,如BBcode或Wiki </c:out>

时间:2011-01-24 15:44:14

标签: java jsp mediawiki bbcode

我正在设计JSP应用程序,我想打印用户输入。我需要<c:out>标签之类的东西,然后需要用户进行格式化。

首先,我想将\n翻译为<br />,但我仍然需要提供<c:out>提供的所有XML转义功能。然后我意识到允许更多格式化会很好,比如BBcode或Wiki语法。

是否有任何JSP标记库,允许这样做?

3 个答案:

答案 0 :(得分:1)

为什么不使用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代码!

e.g。对应于以上简单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;
}

答案 1 :(得分:0)

你有没有想过在你需要的地方嵌入像CKEditor这样的东西?

http://ckeditor.com/demo

这是一个很棒的工具,但请确保许可证符合您的要求:

http://ckeditor.com/license

轻量级替代品是TinyMCE:

http://tinymce.moxiecode.com/

答案 2 :(得分:0)

你也可以考虑一个类似wiki的编辑器,就像stackoverflow中使用的那样:

http://wmd-editor.com/