Struts2标签简单使用——截取
2017-06-04 15:52
274 查看
背景:
显示全部新闻时,为方便大家获得更多资讯,不需要显示整篇新闻内容,截取部分即可。
1.截取纯文本文字前100文字字符:
2.截取含HTML标签中前100文字字符:
添加一个工具类,用来对HTML标签进行处理
package com.tools;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SuppressWarnings({"unchecked" })
public class CutHtml {
static String htmlMatch = "";
// 通过递归删除html文件中的配对的html标签
public static String removeMatchHtmlTag() {
// String html="<p></p><table><tr><td></td><td></td></tr></table>";
Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>");
Matcher m = p.matcher(htmlMatch);
if (m.find()) {
// System.out.println(htmlMatch);
htmlMatch = htmlMatch.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>","$2");
removeMatchHtmlTag();
}
return htmlMatch;
}
public static String subStringHTML(String param, int length, String endWith) {
if(length<1) {System.out.println("length must >0");return null;}
if(param.length()<length){return param;}
StringBuffer result = new StringBuffer();
StringBuffer str = new StringBuffer();
int n = 0;
char temp;
boolean isCode = false; // 是不是HTML代码
boolean isHTML = false; // 是不是HTML特殊字符,如
for (int i = 0; i < param.length(); i++) {
temp = param.charAt(i);
if (temp == ’<’) {
isCode = true;
}
else if (temp == ’&’) {
isHTML = true;
}
else if (temp == ’>’ && isCode) {
n = n - 1;
isCode = false;
}
else if (temp == ’;’ && isHTML) {
isHTML = false;
}
if (!isCode && !isHTML) {
n = n + 1;
// UNICODE码字符占两个字节
if ((temp + "").getBytes().length > 1) {
n = n + 1;
}
str.append(temp);
}
result.append(temp);
if (n >= length) {
break;
}
}
result.append(endWith);
// 取出截取字符串中的HTML标记
String temp_result = result.toString().replaceAll("(>)[^<>]*(<?)","$1$2");
// 去掉不需要结束标记的HTML标记
temp_result = temp_result
.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)[^<>]*/>","");
// 去掉成对的HTML标记
// temp_result=temp_result.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>","$2");
htmlMatch = temp_result;
temp_result = removeMatchHtmlTag();
//System.out.println("6666:" + temp_result);
// 用正则表达式取出标记
Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>");
Matcher m = p.matcher(temp_result);
List endHTML = new ArrayList();
while (m.find()) {
endHTML.add(m.group(1));
}
// 补全不成对的HTML标记
for (int i = endHTML.size() - 1; i >= 0; i--) {
result.append("</");
result.append(endHTML.get(i));
result.append(">");
}
return result.toString();
}
在实体类中针对需要截取的内容调用CutHTML();
JSP页面在该属性下直接调用getSubContent()方法,添加需要截取字符数。
其中,struts2 标签问题escape="false" 这个属性默认值为true,即不解析html代码,直接将其输出。 若想要输出html的效果,则要改为false
显示全部新闻时,为方便大家获得更多资讯,不需要显示整篇新闻内容,截取部分即可。
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标签进行处理
package com.tools;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SuppressWarnings({"unchecked" })
public class CutHtml {
static String htmlMatch = "";
// 通过递归删除html文件中的配对的html标签
public static String removeMatchHtmlTag() {
// String html="<p></p><table><tr><td></td><td></td></tr></table>";
Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>");
Matcher m = p.matcher(htmlMatch);
if (m.find()) {
// System.out.println(htmlMatch);
htmlMatch = htmlMatch.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>","$2");
removeMatchHtmlTag();
}
return htmlMatch;
}
public static String subStringHTML(String param, int length, String endWith) {
if(length<1) {System.out.println("length must >0");return null;}
if(param.length()<length){return param;}
StringBuffer result = new StringBuffer();
StringBuffer str = new StringBuffer();
int n = 0;
char temp;
boolean isCode = false; // 是不是HTML代码
boolean isHTML = false; // 是不是HTML特殊字符,如
for (int i = 0; i < param.length(); i++) {
temp = param.charAt(i);
if (temp == ’<’) {
isCode = true;
}
else if (temp == ’&’) {
isHTML = true;
}
else if (temp == ’>’ && isCode) {
n = n - 1;
isCode = false;
}
else if (temp == ’;’ && isHTML) {
isHTML = false;
}
if (!isCode && !isHTML) {
n = n + 1;
// UNICODE码字符占两个字节
if ((temp + "").getBytes().length > 1) {
n = n + 1;
}
str.append(temp);
}
result.append(temp);
if (n >= length) {
break;
}
}
result.append(endWith);
// 取出截取字符串中的HTML标记
String temp_result = result.toString().replaceAll("(>)[^<>]*(<?)","$1$2");
// 去掉不需要结束标记的HTML标记
temp_result = temp_result
.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)[^<>]*/>","");
// 去掉成对的HTML标记
// temp_result=temp_result.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>","$2");
htmlMatch = temp_result;
temp_result = removeMatchHtmlTag();
//System.out.println("6666:" + temp_result);
// 用正则表达式取出标记
Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>");
Matcher m = p.matcher(temp_result);
List endHTML = new ArrayList();
while (m.find()) {
endHTML.add(m.group(1));
}
// 补全不成对的HTML标记
for (int i = endHTML.size() - 1; i >= 0; i--) {
result.append("</");
result.append(endHTML.get(i));
result.append(">");
}
return result.toString();
}
在实体类中针对需要截取的内容调用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
相关文章推荐
- Struts2 标签 迭代标签和控制标签的简单使用
- Struts2之如何使用简单的UI标签
- struts2中的if/else,iterator标签以及ognl简单使用
- Struts2与JFreeChat简单结合使用
- freemarker中使用struts2标签
- struts2 标签的使用之二 s:iterator
- Struts2中iterator标签的使用
- struts2的checkbox和checkboxlist两个标签的使用
- Struts2标签使用
- Struts2页面开发中常用标签使用说明
- struts2中s:select标签的使用
- js中使用struts2标签
- struts2标签使用
- Struts2页面开发中常用标签使用规范
- Struts2中数据标签使用示例
- struts2 标签的使用之一 s:if
- Struts2 标签使用小结(转帖整理)
- Struts2使用过滤器和拦截器进行简单权限校验
- 使用struts2中的datetimepicker标签
- python中使用状态机的简单例子---截取'/x00'之前的字符串