在JSP页面中,使用标记库代替传统的Java代码来实现页面的显示逻辑是大势所趋,然而,由于自定义标记很容易造成重复定义和非标准的实现,于是出现了基于java Web的JSTL和基于struts的taglib等标签库来解决这些问题。
这些标签库在实现原理上相差无几,学会一种,另一种也就很容易上手,那咱们就从Struts的标签库走起。
常用的strust标签库有<bean:write>、<logic:empty>和<logic:notEmpty>、<logic:present>和<logic:notPresent>、<logic:iterator>4种。
使用方法:
1.在strus-config.xml中配置message-resources,注意,这个标签要放在action-mappings标签后面。配置信息如下:
<message-resources parameter="MessageResources" />2.在src目录下引入提供国际化资源文件MessageResources.properties
3.在JSP页面中引入taglib
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>4.下面是分别是<bean:wriete>(获取字段值、JavaBean属性值)、<logic:empty>和<logic:notEmpty>(判断是否为空)、<logic:present>和<logic:notPresent>(判断是否存在)、<logic:iterator>(迭代取出集合中的数据)分别在Action和JSP页面的配置
(1)<bean:wriete>-->action
public class BeanWriteAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 普通字符串
request.setAttribute("hello", "quwenzhe");
request.setAttribute("today", new Date());
Group group = new Group();
group.setName("提高班");
User user = new User();
user.setUsername("quwenzhe");
user.setAge("24");
user.setGroup(group);
request.setAttribute("user", user);
return mapping.findForward("success");
}
} <bean:wriete>-->JSP
姓名:<input type="text" name="username" value="<bean:write name="user" property="username"/>"><br/> 年龄:<input type="text" name="age" value=" <bean:write name="user" property="age"/>"><br> 所属组:<input type="text" name="group" value=" <bean:write name="user" property="group.name"/>"><br/>(2)<logic:empty>和<logic:notEmpty>、<logic:present>和<logic:notPresent>-->action
public class EmptyPresentAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("attr1", null);
request.setAttribute("attr2", "");
request.setAttribute("attr3", new ArrayList());
return mapping.findForward("success");
}
}
<logic:empty>和<logic:notEmpty>、<logic:present>和<logic:notPresent>-->JSP
<p> <logic:empty name="attr1"> attr1为空 </logic:empty> <logic:notEmpty name="attr1"> attr1为空 </logic:notEmpty> <logic:present name="attr1"> attr1存在 </logic:present> <logic:notPresent name="attr1"> attr1不存在 </logic:notPresent> </p> <p> <logic:empty name="attr2"> attr2为空 </logic:empty> <logic:notEmpty name="attr2"> attr2为空 </logic:notEmpty> <logic:present name="attr2"> attr2存在 </logic:present> <logic:notPresent name="attr2"> attr2不存在 </logic:notPresent> </p> <p> <logic:empty name="attr3"> attr3为空 </logic:empty> <logic:notEmpty name="attr3"> attr3为空 </logic:notEmpty> <logic:present name="attr3"> attr3存在 </logic:present> <logic:notPresent name="attr3"> attr3不存在 </logic:notPresent> </p>(3)<logic:iterator>-->action
public class IteratorAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Group group = new Group();
group.setName("提高班");
List userList = new ArrayList();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setUsername("quwenzhe-->" + i);
user.setAge("24-->" + i);
user.setGroup(group);
userList.add(user);
}
request.setAttribute("userList", userList);
return mapping.findForward("success");
}
}
<logic:iterator>-->JSP
<table border="1"> <tr> <td>姓名</td> <td>年龄</td> <td>所属组</td> </tr> <logic:empty name="userList"> <tr> <td colspan="3">没有符合条件的数据</td> </tr> </logic:empty> <logic:notEmpty name="userList"> <logic:iterate id="user" name="userList"> <tr> <td> <bean:write name="user" property="username"/> </td> <td> <bean:write name="user" property="age"/> </td> <td> <bean:write name="user" property="group.name"/> </td> </tr> </logic:iterate> </logic:notEmpty> </table>
这样我们在后台Action中通过request设置的值,在JSP页面中通过标签就能很方便的获取到,大大提高工作效率。
原文:http://blog.csdn.net/quwenzhe/article/details/39483053