Struts2标签简单使用——截取

来源:互联网 发布:网络模块价格 编辑:程序博客网 时间:2024/05/14 19:14

背景:

     显示全部新闻时,为方便大家获得更多资讯,不需要显示整篇新闻内容,截取部分即可。

     1.截取纯文本文字前100文字字符:

<s:if test="#pc.aim.length()>100"><p><s:property value="#pc.aim.substring(0,100)" />···</p></s:if><s:else><p><s:property value="#pc.aim" /></p></s:else>


     2.截取含HTML标签中前100文字字符:

     添加一个工具类,用来对HTML标签进行处理

  1. package com.tools;      
  2.      
  3. import java.util.ArrayList;      
  4. import java.util.List;      
  5. import java.util.regex.Matcher;      
  6. import java.util.regex.Pattern;      
  7.      
  8. @SuppressWarnings({"unchecked" })      
  9. public class CutHtml {      
  10.     static String htmlMatch = "";      
  11.      
  12.     // 通过递归删除html文件中的配对的html标签      
  13.      
  14.     public static String removeMatchHtmlTag() {      
  15.         // String html="<p></p><table><tr><td></td><td></td></tr></table>";      
  16.         Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>");      
  17.         Matcher m = p.matcher(htmlMatch);      
  18.         if (m.find()) {      
  19.         //  System.out.println(htmlMatch);      
  20.             htmlMatch = htmlMatch.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>","$2");      
  21.      
  22.             removeMatchHtmlTag();      
  23.         }      
  24.      
  25.         return htmlMatch;      
  26.     }      
  27.      
  28.     public static String subStringHTML(String param, int length, String endWith) {      
  29.      
  30.         if(length<1) {System.out.println("length must >0");return null;}      
  31.               
  32.         if(param.length()<length){return param;}      
  33.               
  34.         StringBuffer result = new StringBuffer();      
  35.         StringBuffer str = new StringBuffer();      
  36.         int n = 0;      
  37.      
  38.         char temp;      
  39.      
  40.         boolean isCode = false// 是不是HTML代码      
  41.         boolean isHTML = false// 是不是HTML特殊字符,如      
  42.         for (int i = 0; i < param.length(); i++) {      
  43.             temp = param.charAt(i);      
  44.             if (temp == ’<’) {      
  45.                 isCode = true;      
  46.             }      
  47.             else if (temp == ’&’) {      
  48.                 isHTML = true;      
  49.             }      
  50.             else if (temp == ’>’ && isCode) {      
  51.                 n = n - 1;      
  52.                 isCode = false;      
  53.             }      
  54.             else if (temp == ’;’ && isHTML) {      
  55.                 isHTML = false;      
  56.             }      
  57.             if (!isCode && !isHTML) {      
  58.                 n = n + 1;      
  59.                 // UNICODE码字符占两个字节      
  60.                 if ((temp + "").getBytes().length > 1) {      
  61.                     n = n + 1;      
  62.                 }      
  63.                 str.append(temp);      
  64.             }      
  65.             result.append(temp);      
  66.             if (n >= length) {      
  67.                 break;      
  68.             }      
  69.         }      
  70.      
  71.         result.append(endWith);      
  72.         // 取出截取字符串中的HTML标记      
  73.         String temp_result = result.toString().replaceAll("(>)[^<>]*(<?)","$1$2");      
  74.      
  75.         // 去掉不需要结束标记的HTML标记      
  76.      
  77.         temp_result = temp_result      
  78.                 .replaceAll("<(AREA|BASE|BASEFONT|BODY|BR|COL|COLGROUP|DD|DT|FRAME|HEAD|HR|HTML|IMG|INPUT|ISINDEX|LI|LINK|META|OPTION|P|PARAM|TBODY|TD|TFOOT|TH|THEAD|TR|area|base|basefont|body|br|col|colgroup|dd|dt|frame|head|hr|html|img|input|isindex|li|link|meta|option|p|param|tbody|td|tfoot|th|thead|tr)[^<>]*/>","");      
  79.      
  80.         // 去掉成对的HTML标记      
  81.         // temp_result=temp_result.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>","$2");      
  82.         htmlMatch = temp_result;      
  83.         temp_result = removeMatchHtmlTag();      
  84.         //System.out.println("6666:" + temp_result);      
  85.         // 用正则表达式取出标记      
  86.      
  87.         Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>");      
  88.         Matcher m = p.matcher(temp_result);      
  89.         List endHTML = new ArrayList();      
  90.      
  91.         while (m.find()) {      
  92.             endHTML.add(m.group(1));      
  93.         }      
  94.      
  95.         // 补全不成对的HTML标记      
  96.         for (int i = endHTML.size() - 1; i >= 0; i--) {      
  97.             result.append("</");      
  98.             result.append(endHTML.get(i));      
  99.             result.append(">");      
  100.      
  101.         }      
  102.         return result.toString();      
  103.      
  104.     }   
    

     在实体类中针对需要截取的内容调用CutHTML();

    public String getSubContent(int len){              if(len<=0||len>this.content.length())                  return this.content;              else return CutHtml.subStringHTML(this.content,len, "...");           }     

     JSP页面在该属性下直接调用getSubContent()方法,添加需要截取字符数。

     <s:property value="getSubContent(70)" escape="false"/>


     其中,struts2 标签问题escape="false" 这个属性默认值为true,即不解析html代码,直接将其输出。 若想要输出html的效果,则要改为false



原创粉丝点击