Iterate List / ArrayList in Struts 2

Tagged:  

File Heirarchy

struts 2 file heirarchy

//Display.java
package phoneBook;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;

public class Display extends ActionSupport{

	private static final long serialVersionUID = 1L;	
	List<PhoneBean> list = null;
	
	public List<PhoneBean> getList() {
		return list;
	}
	public void setList(List<PhoneBean> list) {
		this.list = list;
	}

	public String execute() throws Exception{
		list = new ArrayList<PhoneBean>();
		
		PhoneBean bean = new PhoneBean();
		bean.setName("juan dela cruz");
		bean.setAge(17);
		bean.setBirthDate(Date.valueOf("1987-1-1"));
		bean.setContactNumber("12345");
		list.add(bean);
		
		bean = new PhoneBean();
		bean.setName("john cruise");
		bean.setAge(14);
		bean.setBirthDate(Date.valueOf("1988-2-2"));
		bean.setContactNumber("67890");
		list.add(bean);
		
		return SUCCESS;
	}

}
//PhoneBean.java
package phoneBook;

import java.sql.Date;
public class PhoneBean {
	private String name = null;
	private int age = 0;
	private Date birthDate = null;
	private String contactNumber = null;

	public String getContactNumber() {
		return contactNumber;
	}
	public void setContactNumber(String contactNumber) {
		this.contactNumber = contactNumber;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthDate() {
		return birthDate;
	}
	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}	
}

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="default" extends="struts-default">
        <action name="Display" class="phoneBook.Display">
             <result>/display.jsp</result>
        </action>
    </package>
    
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
</web-app>

A very good Example.........Thank You So Much

Thank you for a very nice article

Thank you!!

This is example is very good

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.