struts2标签的使用(一)

来源:互联网 发布:沐林视觉美工学费贵吗 编辑:程序博客网 时间:2024/04/29 16:14

网上铺天盖地的有很多的例子,手册之类,这里引用一个我初学时做的一个例子

TestAction中:

public class TestAction extends ActionSupport {

 public String test1() {
  Product product = new Product();
  product.setProdId(new Long(112233));
  ProdSpec prodSpec = new ProdSpec();
  prodSpec.setProdSpecId(new Integer("379"));
  prodSpec.setName("CDMA");
  product.setProdSpec(prodSpec);
  HttpServletRequest request = ServletActionContext.getRequest();
  request.setAttribute("myName", "zhuxia");

  //增加一个地址
  List<Prod2Addr> list = new ArrayList<Prod2Addr>();
  Prod2Addr addr1 = new Prod2Addr();
  addr1.setProdId(new Long(111));
  addr1.setAddressId(new Long(1233));
  addr1.setReasonCd(new Integer(3));
  Prod2LocationReason p1 = new Prod2LocationReason();
  p1.setReasonCd(new Integer(3));
  p1.setName("install address");
  addr1.setProd2LocationReason(p1);
  addr1.setAddressDesc("Nan Jing Road");
  list.add(addr1);
  Prod2Addr addr2 = new Prod2Addr();
  addr2.setProdId(new Long(111));
  addr2.setAddressId(new Long(1233));
  addr2.setReasonCd(new Integer(3));
  addr2.setAddressDesc("Shang Hai Road");
  addr2.setProd2LocationReason(p1);
  list.add(addr2);
  product.setProd2Addrs(list);
  request.setAttribute("product", product);
  return "test1";
 }
}

 

test1 对应test1.jsp页面,内容如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
 <s:set name="name" value="#request.myName"/>
 1: request.myName value1112233444<br/>
 <s:property value="#request.myName"/><br/>
 the name is : <s:property value="#name"/> <br/>
 <br/>
   
    2: test if<br/>
    <s:set name="prodSpecName" value="#request.product.prodSpec.name"/>
    the prodSPecName is: <s:property value="#prodSpecName"/>
    <br>
    test another example
    <s:set name="prodSpec" value="#request.product.prodSpec"/>
    <s:if test="#prodSpec != null">
     prodSpecId is:<s:property value="#prodSpec.prodSpecId"/><br/>
     prodSpecName is: <s:property value="#prodSpec.name"/>
    </s:if>
   
    test another example: the context can not be seen <br/>
    <s:set name="area" value="#request.product.area"/>
    <s:if test="#area != null">
     areaId: <s:property value="#area.areaId"/><br/>
     areaName:<s:property value="#area.name"/> <br/>
    </s:if>
    <s:else>
      the product.area is null
    </s:else>
    <br/>
    <br/>
    3:iterator <br/>
    <s:set name="prod2Addrs" value="#request.product.prod2Addrs"/>
    <s:iterator value="#prod2Addrs" status="prod2Addr">
     <br/>
     <br/>
     addressDesc : <s:property value="addressDesc"/><br/>
     prod2Addr.reasonName11 : <s:property value="prod2LocationReason.name"/><br/> 
     location : <s:set name="prod2LocationReason" value="prod2LocationReason"/><br/>
     reasonName : <s:property value="#prod2LocationReason.name"/>    <br/>
     prod2Addr.reasonName22 : <s:property value="prod2LocationReason.name"/><br/>
    </s:iterator>
</body>
</html

 

运行结果:

 

1: request.myName value1112233444
zhuxia
the name is : zhuxia

2: test if
the prodSPecName is: CDMA
test another example prodSpecId is:379
prodSpecName is: CDMA test another example: the context can not be seen
the product.area is null

3:iterator
addressDesc : Nan Jing Road
prod2Addr.reasonName11 : install address
location :
reasonName : install address
prod2Addr.reasonName22 : install address
addressDesc : Shang Hai Road
prod2Addr.reasonName11 : install address
location :
reasonName : install address
prod2Addr.reasonName22 : install address

原创粉丝点击