字符串截取
今在jsp页面中截取一段字符显示.项目用的struts2标签
经过反复的测试,struts2皆不支持标签的嵌套:
单独支持:
但不支持这种写法:
其<s:textfield>标签格式化显示日期,如下写法:
话说:条条大道通罗马,只能换用EL表达式试试了,结果是一试就通,EL函数很给力啊!!!
但注意的是[size=medium]:[color=red]单独用需要"$",而嵌套来用嵌套部分的不需要"$",写了反而有问题的.[/color][/size]
截取方法的总结:
今在jsp页面中截取一段字符显示.项目用的struts2标签
经过反复的测试,struts2皆不支持标签的嵌套:
单独支持:
<s:property value='#attr.todo.description.indexOf('2')'/> <s:property value='#attr.todo.description.length()'/> 但不支持这种写法:
<s:property value="#attr.todo.description.substring(<s:property value='#attr.todo.description'/>,
<s:property value='#attr.todo.description.indexOf('2')'/>,
<s:property value='#attr.todo.description.length()'/>)">
</s:property>其<s:textfield>标签格式化显示日期,如下写法:
<s:textfield label="日期" name="todo.created" readonly="true">
<s:param name="value">
<s:date name="#attr.todo.created" format="yyyy-MM-dd HH:mm:ss"/>
</s:param>
</s:textfield>话说:条条大道通罗马,只能换用EL表达式试试了,结果是一试就通,EL函数很给力啊!!!
${fn:substring(todo.description,fn:indexOf(todo.description,2),fn:length(todo.description))}但注意的是[size=medium]:[color=red]单独用需要"$",而嵌套来用嵌套部分的不需要"$",写了反而有问题的.[/color][/size]
截取方法的总结:
public class StringSub {
public static void main(String[] argu)
{
String str="abcdefghmf";
System.out.println("截取前三个字符:"+str.substring(0, 3));
System.out.println("截取前三个字符以外的字符:="+str.substring(3));
System.out.println("截取后三个字符:"+str.substring(str.length()-3,str.length()));
System.out.println("截取后三个字符:"+str.substring(str.length()-3,str.length()));
System.out.println("截取字符'f'前的字符:"+str.substring(0,str.indexOf("f")));
System.out.println("截取字符'f'后(包括'f')的字符:"+str.substring(str.indexOf("f"),str.length()));
System.out.println("截取字符'f'和字符'h'之间(包括'f')的字符:"+str.substring(str.indexOf("f"),str.indexOf("h")));
System.out.println("获取取第一个字符'f'的下标:"+str.indexOf("f"));
}
}

本文探讨了在使用Struts2框架的JSP页面中,如何通过标签和EL表达式进行字符串的截取操作。详细对比了Struts2标签对于字符串截取的支持情况,特别是对于嵌套标签的限制,并展示了如何使用EL表达式实现灵活的字符串处理。同时,文章总结了字符串截取方法,提供了实用的代码示例。
1568

被折叠的 条评论
为什么被折叠?



