日期:2014-05-16  浏览次数:20426 次

jstl表达式的使用

目录
1 STRTUS2 和 EL,JSTL 2
1.1 STRUTS2,EL,JSTL标签 2
1.1.1 struts2截取一段字符串 (struts2, fn) 2
1.1.1 Struts2 获得长度 2
1.1.2 <s:if><c:if > 2
1.1.3 Struts2拦截器 用户登录验证(2009-12-25 12:52:56) 3

1 STRTUS2 和 EL,JSTL
1.1 STRUTS2,EL,JSTL标签
1.1.1 STRUTS2截取一段字符串 (STRUTS2, FN)

Struts2 标签的特性支持大部分java方法
<s:property value=" dateshanggang.substring(0,10)" />
EX:
<s:property value="dateshanggang.substring(11,dateshanggang.length())" />
Fn:需要导入
--标签库
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
--从什么地方拆分
<c:set var="HH">${fn:substringBefore(chidaoTime/3600,'.')}</c:set>
--list大小是否给空
<c:if test="${empty list || fn:length(list) == 0}"> 没有记录哦 </c:if>
EX:
<c:if test="${empty list || fn:length(list) == 0}"> 没有记录哦 </c:if>
<c:set var="HH">${fn:substringBefore(chidaoTime/3600,'.')}</c:set>
1.1.1 STRUTS2 获得长度
<s:property value=" dateshanggang.length())" />
{empty list || fn:length(list) == 0}
${fn:length(property)}

1.1.2 <S:IF><C:IF >
C标签没有 <c:else> 只有单<c:if>
而<s:if>有<s:else> 可以配对使用
但是能用<c:if> 尽量不要用<s:if>
原因? 自己想吧!
EX:
<c:if test="${fn:length(content) <= 8}" var="ifresult">
<s:property value="content" />
</c:if>
<c:if test="${!ifresult}">
<s:property value="content.substring(0,8)" />..</a>&nbsp;
</c:if>
EX:<s:if><s:else>


1.1.3 STRUTS2拦截器 用户登录验证(2009-12-25 12:52:56)

Struts2拦截器 用户登录验证(2009-12-25 12:52:56)转载标签:杂谈 

实现方式:
实现Interceptor接口

init() {} 初始化时调用
destroy() {} 销毁时调用
class MyInterceptor implements Interceptor {
private String hello;
//setter...getter...
public void init() {
  System.out.println("init()...");
  System.out.println( hello );
}
public void destroy() {
  System.out.println("destroy()...");
}
public String intercept(ActionInvocation invocation) throws Exception {
  System.out.println("intercept()1...");
  //拦截器有拦截器站
  //invoke()将判断是否还有下一个拦截器,有就执行下一个拦截器,没有则开始执行被拦截的类
  String result = invocation.invoke();
  System.out.println("finish1...");
  return result;
}
继承AbstractInterceptor

该类实现了Interceptor接口,并且空实现了init()和destroy()方法

public class MyInterceptor extends AbstractInterceptor {

public String intercept(ActionInvocation invocation) throws Exception {

  System.out.println("intercept()2...");

  String result = invocation.invoke();

  System.out.println("finish2...");

  return result;

}

}


继承MethodFilterInterceptor

public class MyInterceptor3 extends MethodFilterInterceptor {
@Override
public void init() {
  System.out.println("init3");
}
//intercept()已经实现好了,不用去管他
//需要重写一下doIntercept
@Override
public String doIntercept(ActionInvocation invocation) throws Exception {
  // 取得请求相关的ActionContext实例
  ActionContext ctx = invocation.getInvocationContext();
  Map session = ctx.getSession();
  // 取出名为user的session属性
  User user = (User) session.get("user");
  // 如果没有登陆,返回重新登陆
  if (user != null) {
   return invocation.invoke();
  } else {
   return "login";
  }
}
}

MethodFilterInterceptor中包含两个protected的属性
Set includeMethods : 包含谁
Set excludeMethods : 排除谁

<interceptors>
<interceptor name="myInterceptor3" class="..."></in