1 struts FORM 标签
与jstl标准标签库类似的,struts有专属标签库
form标签用于提交数据
修改addProduct.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>submit</title>
</head>
<body>
<s:form action="addProduct">
<s:textfield name="product.name" label="product name" />
<s:submit value="Submit" />
</s:form>
</body>
</html>
2 struts使用S:ITERATOR 标签遍历集合
- 为ProductAction增加一个products属性,类型是List,并提供getter setter
为ProductAction增加一个list()方法,为products添加3个product对象,并返回“list"
private List
products; public List
getProducts() {
return products;
}public void setProducts(List
products) {
this.products = products;
}
public String list() {products=new ArrayList(); Product p1 = new Product(); p1.setId(1); p1.setName("product1"); Product p2 = new Product(); p2.setId(2); p2.setName("product2"); Product p3 = new Product(); p3.setId(3); p3.setName("product3"); products.add(p1); products.add(p2); products.add(p3); return "list";
}
struts.xml中添加
<action name="listProduct" class="com.tzy.struts.action.ProductAction" method="list">
<result name="list">list.jsp</result>
</action>
新建list.jsp
- 使用s:iterator标签进行遍历
- value 表示集合
- var 表示遍历出来的元素
- st 表示遍历出来的元素状态
- st.index 当前行号 基0
- st.count 当前行号 基1
- st.first 是否是第一个元素
- st.last 是否是最后一个元素
- st.odd 是否是奇数
- st.even 是否是偶数
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%><%@ taglib prefix="s" uri="/struts-tags"%>
</tr> <s:iterator value="products" var="p" status="st"> <tr> <td>${p.id}</td> <td>${p.name}</td> <td>${st.index}</td> <td>${st.count}</td> <td>${st.first}</td> <td>${st.last}</td> <td>${st.odd}</td> <td>${st.even}</td> </tr> </s:iterator>
id name st.index st.count st.first st.last st.odd st.even