在JSP页面中,使用标记库代替传统的Java代码来实现页面的显示逻辑是大势所趋,然而,由于自定义标记很容易造成重复定义和非标准的实现,于是出现了基于Java Web的JSTL和基于struts的taglib等标签库来解决这些问题。
这些标签库在实现原理上相差无几,学会一种,另一种也就很容易上手,那咱们就从Struts的标签库走起。
常用的strust标签库有、和、和、4种。
使用方法:
1.在strus-config.xml中配置message-resources,注意,这个标签要放在action-mappings标签后面。配置信息如下:
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.下面是分别是(获取字段值、JavaBean属性值)、和(判断是否为空)、和(判断是否存在)、(迭代取出集合中的数据)分别在Action和JSP页面的配置
(1)-->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");
?}
}
-->JSP
姓名:">
年龄:">
所属组:">
(2)和、和-->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");
?}
}
?和、和-->JSP
?
?
? ?attr1为空
?
?
? ?attr1为空
?
?
? ?attr1存在
?
?
? ?attr1不存在
?
?
?
?
?
? ?attr2为空
?
?
? ?attr2为空
?
?
? ?attr2存在
?
?
? ?attr2不存在
?
?
?
?
?
? ?attr3为空
?
?
? ?attr3为空
?
?
? ?attr3存在
?
?
? ?attr3不存在
?
?
? (3)-->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");
?}
}
?-->JSP
?
? ?
? ? 姓名 |
? ? 年龄 |
? ? 所属组 |
? ?
? ?
? ?
? ? ?没有符合条件的数据 |
? ?
? ?
? ?
? ?
? ? ?
? ? ? ? ? ? ? ? ? ? |
? ? ? ? ? ? ? ? ? ? |
? ? ? ? ? ? ? ? ? ? |
? ? ?
? ?
? ?
?
这样我们在后台Action中通过request设置的值,在JSP页面中通过标签就能很方便的获取到,大大提高工作效率。