struts1.x标签

一、beanwrite标签

action中在request范围中设置了以下属性


public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  // TODO Auto-generated method stub
  request.setAttribute("name", "bird");
  request.setAttribute("name1", "<font color='red'>bird</font>");
 
  request.setAttribute("date", new Date());
  request.setAttribute("decimal", 1234567.123);
  //设置关联对象,Student类中有Clazz类属性
  Clazz c = new Clazz();
  c.setCname("class");
  Student s = new Student();
  s.setAge(20);
  s.setName("xxx");
  s.setClasses(c);
  request.setAttribute("student", s);
  return mapping.findForward("success");}



返回到jsp页面用beanwrite标签进行显示


<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<h1>BeanWrite标签测试</h1>
   //name属性表示范围中的属性名
   <bean:write name="name"/><br/>
   //当属性值中含有html标签是,可以配置filter="false"让其对标签进行解释,默认配置为true
   <bean:write name="name1" filter="false"/><br/>
   //未经格式化的日期显示
   <bean:write name="date" /><br/>
   //格式化日期
   <bean:write name="date" format="yyyy-MM-dd"/>
   <bean:write name="decimal" /><br/>
   //格式化小数,显示3位一个逗号
   <bean:write name="decimal" format="###,###.####"/><br>
   //小数位用0格式化,如果原来的小数位不够则会在后面补0
   <bean:write name="decimal" format="###,###.0000"/><br>
   //如果属性是一个对象要输出对象的某些属性,可以配置property,指定要输出的属性(前提是该属性要在类中有对应的get方法)
   <bean:write name="student" property="name"/><br>
   <bean:write name="student" property="age"/><br>
   //一个类中若含有另一个类属性,想要输出另一个类中的属性可以直接用.导航
   <bean:write name="student" property="classes.cname"/>






二、logic标签(逻辑判断)


action中在request范围设置的属性

public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

  request.setAttribute("a", null);
  request.setAttribute("b", "");
  request.setAttribute("c", new ArrayList());
  return mapping.findForward("success");
}



返回jsp页面用logic标签进行逻辑判断


<%@taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>

<h1>测试logic标签</h1><br><hr>
    <!-- 当一个属性设置为null时,判断结果为empty, not present -->
    <logic:empty name="a">   a is empty <br>    </logic:empty>
    <logic:notEmpty name="a">  a is not empty<br> </logic:notEmpty>
    <logic:present name="a">    a is present <br> </logic:present>
    <logic:notPresent name="a"> a is not present <br></logic:notPresent>
    <!-- 当一个属性设置为""(空字符串)时,判断结果为empty ,present -->
    <logic:empty name="b">   b is empty <br>    </logic:empty>
    <logic:notEmpty name="b">  b is not empty<br> </logic:notEmpty>
    <logic:present name="b">    b is present <br> </logic:present>
    <logic:notPresent name="b"> b is not present <br></logic:notPresent>
    <!-- 当一个属性设置为一个空集合是,判断结果为empty,prensent -->
    <logic:empty name="c">   c is empty <br>    </logic:empty>
    <logic:notEmpty name="c">  c is not empty<br> </logic:notEmpty>
    <logic:present name="c">    c is present <br> </logic:present>
    <logic:notPresent name="c"> c is not present <br></logic:notPresent>
    <!-- 当在任何范围都未设置该属性时,判断结果为empty,not present -->
    <logic:empty name="d">   d is empty <br>    </logic:empty>
    <logic:notEmpty name="d">  d is not empty<br> </logic:notEmpty>
    <logic:present name="d">    d is present <br> </logic:present>
    <logic:notPresent name="d"> d is not present <br></logic:notPresent>




三、iterator标签(迭代输出)


action中在request范围设置一个list属性


public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  // TODO Auto-generated method stub
  List<Student> list = new ArrayList<Student>();
  Clazz c = new Clazz();
  c.setCname("class");
  for(int i=0;i<5;i++){
   Student s = new Student();
   s.setAge(20+i);
   s.setName("student"+i);
   //设置一个学生所属的班级
   s.setClasses(c);
   list.add(s);
  }
  request.setAttribute("list", list);
  return mapping.findForward("success");
}



jsp页面逻辑判断输出


<%@taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
<%@taglib prefix="bean"  uri="http://struts.apache.org/tags-bean" %>

   <logic:empty name="list">
    没有可以输出的信息<br>
   </logic:empty>
   <logic:notEmpty name="list">
    //id属性为list集合中的当前元素起一个别名
    <logic:iterate id="li" name="list">
     <bean:write name="li" property="name"/>&nbsp;&nbsp;
     <bean:write name="li" property="age"/>&nbsp;&nbsp;
     <bean:write name="li" property="classes.cname"/><br/>
    </logic:iterate>
   </logic:notEmpty>

你可能感兴趣的:(struts1)