Thứ Tư, 5 tháng 6, 2013

trong struts

In danh sách nhân viên sử dụng tag <logic:iterate> trong struts, như bên dưới


  1.  Users class
    package packClass;
    public class Products {
    private String name;
    private String age;

    public Products(String age,String name){
        this.age=age;
        this.name=name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    }

  2.  ListAction

    import packClass.Products;

    public class ListProductsAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // TODO Auto-generated method stub
        List<Users> listusers=new ArrayList<Users>();
       
        listusers.add(new Users("20","Nguyen ngoc bich"));
        listusers.add(new Users("21","Tran thi thuy"));
        listusers.add(new Users("22","Le van troi"));
        listusers.add(new Users("23","Nguyen cong"));
        listusers.add(new Users("24","Luu chi binh"));
        request.setAttribute("listusers", listusers);
        return mapping.findForward("listusers");
       
    }
    }
  3. struts-config.xml

    <action path="/list" type="pack.ListAction" scope="request">
                <forward name="listusers" path="/list.jsp"></forward>
            </action>
  4. list.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
         <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
        <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <body>

    <table border="1" style="text-align: center; border: 1px solid ;">
    <tr>
    <TH>Name</TH>
    <TH>Age</TH>
    </tr>
    <logic:iterate id="listid" name="listusers">
        <tr>
            <td> <bean:write name="listid" property="name"/></td>
            <td> <bean:write name="listid" property="age"/>   </td>
        </tr>
        </logic:iterate>
    </table>
    </body>
    </html>

Không có nhận xét nào:

Đăng nhận xét