Archive for Struts

Struts iterator database fetching example

Sunday, March 14th, 2010

Struts Iterator database fetching Example

In this struts example, we have to fetch and retrieve records from database and show on JSP page and submit to action class.

Struts tutorials will explain how to use struts in logic Iterator tag in JSP to get data from database.

1. index.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ 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://struts.apache.org/tags-tiles" prefix="tiles" %>

<html:html>
  <head>
    <title>Struts.jsp</title>
  </head>

  <body>
  <html:form action="/searchUser">

   <logic:iterate id="loop"  name="UserForm" property="ListUser" >
       <bean:write name="loop" property="userName"/><br>
       <bean:write name="loop" property="deptName"/><br><br>
   </logic:iterate>

   </html:form>
  </body>
</html:html>

2. struts-config.xml

<form-beans>
     <form-bean name="UserForm" type="org.apache.struts.validator.DynaValidatorForm">
         <form-property name="userId" type="java.lang.Integer" />
         <form-property name="userName" type="java.lang.String" />
         <form-property name="deptName" type="java.lang.String" />
         <form-property name="ListUser" type="java.util.ArrayList" />
    </form-bean>
  </form-beans>

  <action-mappings>
        <action
            path="/searchUser"
            type="com.UserSearchAction"
            name="UserForm"
            scope="request"
            validate="false"
            input="/index.jsp">
            <forward name="success" path="/index.jsp"/>
           </action>
  </action-mappings>

3. UserSearchAction.java

package com;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

public class UserSearchAction extends Action{

     public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{

         DynaActionForm userForm = (DynaActionForm)form;

         UserList ul=new UserList();
         ArrayList ar=new ArrayList();
         ar=ul.getUser();

         userForm.set("ListUser", ar);

         return mapping.findForward("success");
     }
}

4. UserList.java

package com;

import java.util.ArrayList;

public class UserList extends User{
     public ArrayList getUser()
     {
        ArrayList<UserList> ar=new ArrayList<UserList>(); 

        UserList ul;

        for(int i=0;i<=20;i++)
        {
            ul=new UserList();
            ul.setUserId(i);
            ul.setUserName("userName"+i);
            ul.setDeptName("Dept"+i);
            ar.add(ul);
        }
        return ar;
     }
}

5. User.java

package com;

public class User {
  private int userId;
  private String userName;
  private String deptName;

    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

 

Run this on web browser http://localhost:8080/struts/searchUser.do

download source code of struts Iterator