struts1主要标签使用方法

struts1主要标签使用方法

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>struts1标签</title>
</head>
<body>
<html:form action="output">
<font color="blue">html:text标签:</font><br/>
name属性为默认form:<html:text property="username"/>name属性非当前form:<html:text property="teacherName" name="grade" /><hr/>
<font color="blue">html:select标签:</font><hr/>
optionsCollection:
<html:select property="username">
   <html:optionsCollection name="userList" label="username" value="userID"/>
</html:select>
options:
<html:select property="username">
   <html:options collection="userList" property="userID" labelProperty="username"/>
</html:select>
</html:form><hr/>
<font color="blue">logic:empty标签:</font><hr/>
logic:empty:<logic:empty name="grade" scope="request">
   <span οnclick="javascript:alert('request中不存在班级!')"></span>
</logic:empty>
logic:notEmpty:<logic:notEmpty name="grade" scope="session">
   <span οnclick="alert('session中存在班级')"></span>
</logic:notEmpty><hr/>
<font color="blue">logic:equals标签:</font><br/>
<c:forEach items="${requestScope.userList}" var="usr">
   ${usr.username }
   <logic:equal name="usr" property="userID" value="${sessionScope.currUser.userID}">
    <a href="#">编辑个人信息</a>
   </logic:equal> <br/>
</c:forEach><br/>
<font color="blue">logic:iterate标签:</font><br/>
<logic:iterate id="usr" name="userList">
   ${usr.username } <c:out value="${usr.birth}"></c:out><br/>
</logic:iterate><hr/>
<font color="blue">bean:wirte标签:</font><br/>
<logic:iterate id="usr" name="userList">
   <bean:write name="usr" property="username"/>
   <bean:write name="usr" property="score" format="#,###"/>
   <bean:write name="usr" property="birth" format="yyyy年MM月dd日"/><br/>
</logic:iterate><hr/>
<font color="blue">bean:define标签:</font><br/>
传统获取记录条数:
<%=((java.util.List)request.getAttribute("userList")).size() %><br/>
bean标签获取记录条数:
<bean:define scope="request" id="dataList" name="userList" type="java.util.List" />
<%=dataList.size() %><hr/>
<font color="blue">bean:size标签:</font><hr/>
bean标签获取记录条数:
<bean:size id="count" name="userList"/>
${count }
<pre>
User对象的字段有:
   private int userID;
   private String username;
   private Date birth;
   private Double score;
Teacher对象的字段有:
   private int graID;
   private String gradeName;
   private String teacherName;

//以下是在跳转到当前页面之前的action处理方法:
public ActionForward doShow(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
   System.out.println("到达action");
   SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");

   UserForm userForm = (UserForm) form;
   userForm.setUserID(22);
   userForm.setUsername("TZ");
   try {
    userForm.setBirth(fmt.parse("2004-5-5"));
   } catch (ParseException ex1) {
    ex1.printStackTrace();
   }

   List<User> list = new ArrayList<User>();
   try {
    list.add(new User(1, "Urey", fmt.parse("2009-10-1"),330.0));
    list.add(new User(2, "Protoss", fmt.parse("2010-1-1"),11110.0));
    list.add(new User(3, "BlackWinter", fmt.parse("2008-5-5"),3444.0));
    request.setAttribute("userList", list);
    request.getSession().setAttribute("currUser",
      new User(2, "Protoss", fmt.parse("2010-1-1"),4050.0));

    Grade grade = new Grade(1, "T64", "Miss Gao");
    request.getSession().setAttribute("grade", grade);

   } catch (ParseException ex) {
    ex.printStackTrace();
   }
   return mapping.findForward("ok");//在配置文件中设置的"ok"转向为当前页面
}
</pre>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值