2011-10-23 5 views
1

다음과 같은 문자열이 있습니다.<h : outputText> 고정 길이의 콘텐츠를 표시하는 방법은 무엇입니까?

<p><strong>Make sure you’re paying the right price for your household and trade services with pricing calculators from Australia’s largest online services marketplace, ServiceSeeking.com.au </strong></p>. 

이 문자열의 일부를 <h:outputText escape="false"> (내용, HTML 태그 없음)으로 표시하려고합니다. substring()를 시도했지만 html 태그가 끝나는 곳을 모르므로 양식이 손상되었습니다. 나는 약 100자를 얻고 싶다. 어떻게해야합니까?

답변

1

는 내가 100 % 정확하게 질문을 이해 있는지 확실하지 않습니다하지만 당신은 모든 문자열을 표시하는 경우의 형식으로 roughtly 있습니다 당신의 예는 다음은 HTML 않고 그들을 표시하고 100 자로 단축이 방법으로 할 수 있습니다

<h:outputText value="#{textExtractorBean.extractedText}"/> 

을 그리고 이것은 빈입니다 :

class TextExtractorBean{ 
... 
    getExtractedText(){ 
    Pattern pattern = Pattern.compile("<([a-z])+>"); 
    Matcher matcher = pattern.matcher(text); 

    int firstIdxAfterOpeningTags = 0; 
    while(matcher.find()){ 
     firstIdxAfterOpeningTags = matcher.end(); 
    } 

    pattern = Pattern.compile("</([a-z])+>"); 
    matcher = pattern.matcher(text); 

    int firstIdxBeforeClosingTags = text.length(); 
    if(matcher.find()){ 
     firstIdxBeforeClosingTags = matcher.start(); 
    } 

    String extractedText = text.substring(firstIdxAfterOpeningTags, 
      firstIdxBeforeClosingTags); 
    String shortenedText = extractedText.length() > 0 ? extractedText 
      .substring(0,100) : extractedText; 
    return shortenedText; 
    } 
... 
} 
,

여기서 텍스트 변수에 귀하의 예와 같은 문자열이 포함되어 있습니다.

+0

감사합니다. – xuanhung2401

0

당신이 할 수있는 다음과 같은 JSF HTML 태그와 출력 텍스트 :

<h:outputText escape="false" value="Your content here" /> 
관련 문제