shivu
shivu

Reputation: 3

How to display the arraylist in jsp struts 2

 public class BrandDetails extends SqlContainer{

        public static ArrayList<UserUtilityModel> getDetailsofBrands()
        {
    ArrayList<UserUtilityModel> alist=new ArrayList<UserUtilityModel>() ;

            System.out.print("Get the Brand values");
            PreparedStatement stmt=null;
            Connection conn=null;
            ResultSet rs=null;

            try
            {
                String sql=SqlContainer.getBrandDetailsSQL;
                conn=ULDBConnectionUtility.getDBConnection();
                 stmt=conn.prepareStatement(sql);

            //   stmt.setString(1,branchName);
                    rs=stmt.executeQuery();


                while(rs.next())
                {

                    UserUtilityModel brand=new UserUtilityModel();


                    brand.setBrandId(rs.getInt("BRAND_ID"));
                    brand.setManufacureId(rs.getInt("MANUFACTURER_ID"));
                    brand.setBrandName(rs.getString("BRAND_NAME"));
                    brand.setBrandDesc(rs.getString("BRAND_DESC"));
                    brand.setStatus(rs.getString("STATUS"));

                    alist.add(brand);
                }
                System.out.print(alist);
        }

            return alist;   
        }

Am displaying the database records using getter and setter method in struts2 and i dont know how do get the array list values in jsp page. Can any help me out to solve this problem.

Upvotes: 0

Views: 5784

Answers (2)

Pritesh Shah
Pritesh Shah

Reputation: 887

Say in your action class there is property called orderList with its getter and setter. In prepare method or your action method, set this property either by getting values from database.

OrderList is list of Order objects, where Order has some properties like price, status and so on Then in jsp try below code,

<s:iterator value="orderList">
  <s:property value="price"/>
  <s:property value="status" />
</s:iterator>

In your case, say you have userUtilityModelList property in your action class and you have created getter and setter for it. now you set its value from database and in jsp do something like below,

<s:iterator value="userUtilityModelList">
      <s:property value="brandId"/>
      <s:property value="manufacureId" />
      <s:property value="brandName" />
      <s:property value="brandDesc" />
      <s:property value="status" />
 </s:iterator>

Upvotes: 1

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

You can use forEach tag in JSTL if you want to iterate over a collection

 <c:forEach var="toBeUsedInTheLoop" items="${aList}" >
     // operations what you want to perform on `toBeUsedInTheLoop`      
 </c:forEach>

This is similar to advanced for loop in java

for (Object o : objectList){
    // do something with o
}

Upvotes: 0

Related Questions