2011-01-24 4 views
2

JSP 응용 프로그램을 디자인 중이며 사용자 입력을 인쇄하고 싶습니다. 나는 <c:out> 태그와 같은 것이 필요하지만 사용자가 어떤 포맷팅을 할 수있게합니다.<c:out> 태그 대신 BBcode 또는 Wiki와 같은 더 많은 서식을 지정할 수 있습니다.

시작하려면 \n<br />으로 번역하고 싶지만 아직까지는 모든 XML을 <c:out>이 이스케이프 처리해야합니다. 그런 다음 BBcode 나 Wiki 구문과 같은 더 형식을 허용하는 것이 좋을 것이라고 깨달았습니다.

JSP 태그 라이브러리가 있습니까?

답변

1

http://www.java2s.com/Code/Java/JSP/Createyourowntagacustomtagbody.htm

XML 파일 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> 

그리고 당신이 쓰는 당신이 원하는 것의 작은 자바 코드!

위의 태그에 해당하는 간단한 자바 코드 예 :

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; 
}