Struts2 tag for checking empty bean
<s:if test="%{!nmap.isEmpty()}">
<thead>
<tr>
<td>Name</td>
<td>Age</td>
</tr>
</thead>
</s:if>
<tbody>
<s:iterator var="Person" value="value">
.
.
.
</s:iterator>
</tbody>
Struts2 tag for checking empty bean
<s:if test="%{!nmap.isEmpty()}">
<thead>
<tr>
<td>Name</td>
<td>Age</td>
</tr>
</thead>
</s:if>
<tbody>
<s:iterator var="Person" value="value">
.
.
.
</s:iterator>
</tbody>
Running Iterator Through Bean Stored in Map List
public class Iterator extends ActionSupport
{
Map nmap = new HashMap<String, List<Person>>();
public String execute()
{
Person objPerson = new Person();
objPerson.userName = "Name1";
objPerson.age = 25;
List<Person> myList = new ArrayList<Person>();
myList.add(objPerson);
nmap.put("Mugil", myList);
objPerson.userName = "Name2";
objPerson.age = 26;
List<Person> myList2 = new ArrayList<Person>();
myList2.add(objPerson);
nmap.put("Mani", myList2);
objPerson.userName = "Name3";
objPerson.age = 27;
List<Person> myList3 = new ArrayList<Person>();
myList3.add(objPerson);
nmap.put("Vinu", myList3);
setNmap(nmap);
return SUCCESS;
}
public Map getNmap()
{
return nmap;
}
public void setNmap(Map nmap)
{
this.nmap = nmap;
}
}
Person Bean Code
class Person
{
String userName;
int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
JSP Page Using Struts2 Tag
<table>
<s:iterator value="nmap">
<s:iterator var="Person" value="value">
<tr>
<td>
<s:property value="key" />
</td>
<td>
<s:property value="age" />
</td>
</tr>
</s:iterator>
</s:iterator>
</table>
Once a Time I had a Scenario where I should display a Page with List of employees movement from Last Friday to this week Saturday if Saturday is over(you are in Sunday).If you are in any other day other than Saturday(Mon..Fri) then you should choose date of Last before Saturday to Friday.
The above would be made with the following


As you can see in the above diagram I have added two calendar.
In First I should Get Date between 15 to 21 since today date is this 28 and Saturday is not over
In Second I should Get Date between 22 to 28 since today date is this 30 nd Saturday is over
java.util.Date dt = new java.util.Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String currentTime = sdf.format(dt);
Calendar cal = Calendar.getInstance();
int daysBackToSat = cal.get(Calendar.DAY_OF_WEEK);
//Checks if this weeks friday is Over.in My Case 28 falls on Friday.
//Returns 6 the below case is True so adds 13 to get before Saturday
if(daysBackToSat < 7)
daysBackToSat += 7; //6+7
//Subtracting 13 Days for Last Saturday
//Since this Saturday is not Over
cal.add(Calendar.DATE, daysBackToSat * -1);
System.out.println("Start Date" + sdf.format(cal.getTime()));
cal.add(Calendar.DATE, 6);
System.out.println("End Date" + sdf.format(cal.getTime()));
Output Code
2013-06-15
2013-06-21
Function that Returns No of Rows in ResultSet
public int getRows(ResultSet res)
{
int totalRows = 0;
try
{
res.last();
totalRows = res.getRow();
res.beforeFirst();
}
catch(Exception ex)
{
return 0;
}
return totalRows ;
}
Action Class Code
public class DropDown extends ActionSupport{
private List arrUserDetails = new ArrayList<Users>();
public DropDown()
{
Users objUsers = new Users();
objUsers.setUserId("101");
objUsers.setUserName("User1");
arrUserDetails.add(objUsers);
objUsers.setUserId("102");
objUsers.setUserName("User2");
arrUserDetails.add(objUsers);
objUsers.setUserId("103");
objUsers.setUserName("User3");
arrUserDetails.add(objUsers);
}
public String execute(){
return SUCCESS;
}
public List getArrUserDetails() {
return arrUserDetails;
}
public void setArrUserDetails(List arrUserDetails) {
this.arrUserDetails = arrUserDetails;
}
}
Users Bean Class
public class Users
{
private String userId;
private String userName;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
JSP Page Struts2 Tag
<s:select label="Departments" list="departmentList" listKey="deptId" listValue="deptName" name="department"/>
This warning is generated since you have specified formName.action in action attribute of form.You get get rid of those by changing the action attribute to formName instead of formName.action
Reason for Error
<s:form action="Reports.action" method="post">
Change it To
<s:form action="Reports" method="post" namespace="/">
Below I have a List of Time to be added together
List<String> arrTime = new ArrayList<String>();
int hour = 0;
int minutes = 0;
int seconds = 0;
String Nmin = null;
String Nsec = null;
String result = null;
arrTime.add("09:05:25");
arrTime.add("09:30:15");
arrTime.add("10:15:01");
arrTime.add("08:19:49");
arrTime.add("09:17:40");
for (Iterator itr = arrTime.iterator(); itr.hasNext();)
{
String Time = (String) itr.next();
if (Time != null)
{
String[] rowtime = Time.split(":");
hour += Integer.parseInt(rowtime[0]);
minutes += Integer.parseInt(rowtime[1]);
seconds += Integer.parseInt(rowtime[2]);
}
}
hour += minutes/60;
minutes += seconds/60;
minutes %= 60;
seconds %= 60;
if (minutes < 10)
Nmin = "0" + String.valueOf(minutes);
else
Nmin = String.valueOf(minutes);
if (seconds < 10)
Nsec = "0" + String.valueOf(seconds);
else
Nsec = String.valueOf(seconds);
result = hour + ":" + Nmin + ":" + Nsec;
System.out.println(result);
Output
46:28:10
public class Database
{
private static DataSource dataSource;
static
{
try
{
dataSource = new InitialContext().lookup("jndifordbconc");
}catch (NamingException e)
{
throw new ExceptionInInitializerError("'jndifordbconc' not found in JNDI", e);
}
}
public static Connection getConnection()
{
return dataSource.getConnection();
}
}
public List<Entity> list() throws SQLException
{
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<Entity> entities = new ArrayList<Entity>();
try
{
connection = Database.getConnection();
statement = connection.prepareStatement("SELECT id, foo, bar FROM entity");
resultSet = statement.executeQuery();
while (resultSet.next())
{
Entity entity = new Entity();
entity.setId(resultSet.getLong("id"));
entity.setFoo(resultSet.getString("foo"));
entity.setBar(resultSet.getString("bar"));
entities.add(entity);
}
}
finally
{
if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}
return entities;
}
Time to be Added together
TimeWorked 09:05:25 09:30:15 10:15:01 08:19:49 09:17:40
Code
int hour = 0;
int minute = 0;
for() {
String[] rowtime= row[i].split(":");
hour += Integer.parseInt(rowtime[0]);
minute += Integer.parseInt(rowtime[1]);
}
hour += minute / 60;
minute %= 60;
String result = hour + ":" + minute
if(minute<10)
Nmin = "0"+ String.valueOf(minute);
else
Nmin = String.valueOf(minute);
String result = hour + ":" + Nmin;
Calendar cal = Calendar.getInstance();
int daysBackToSat = cal.get(Calendar.DAY_OF_WEEK );
if(daysBackToSat<7)
{
daysBackToSat += 7;
}
cal.add(Calendar.DATE, daysBackToSat*-1);
System.out.println(sdf.format(cal.getTime()));