Wednesday 22 December 2010

STRUTS 2 Simple Master-Detail JAVA program

This is a simple STRUTS 2 example done with NetBeans 6.9.1. It is a simple Master-Detail example. The master list data is kept in a list and displayed in a table. When a row is chosen with the view link a detail JSP screen is opened and the selected item is displayed. The list that is used by the master table is loaded with the setList action at the very beginning of the application.

This is a migration of the same program from Struts 1 to Struts 2. It looks simple but this technique may be used to populate DDLBs in real applications. A reservation about this is: if the amount of the material is too much it causes substantial delays in the startup of the program.

Some credit goes to Sedat Ergin and RoseIndia whose works I changed and took out bits and pieces.

Kind regards.
Ali Riza SARAL

Note: I left intentionally some debugging lines to give some hints.






web.xml
-------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Struts Blank</display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>


index.html
-----------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=ars/setList.action">
</head>

<body>
<p>Loading ...</p>
</body>
</html>







struts.xml
----------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<include file="ars.xml"/>

<!-- Add packages here -->

</struts>



ars.xml
-------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="ars" namespace="/ars" extends="struts-default">

<action name="setList" class="ars.SetListAction">
<result name="success">/listProcess.jsp</result>
</action>
<action name="viewDetail" class="ars.ViewDetailAction">
<result name="success">/viewDetailProcess.jsp</result>
</action>
</package>
</struts>



SetListAction.java
------------------
package ars;
import com.opensymphony.xwork2.ActionContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

public final class SetListAction extends ActionSupport implements
ServletRequestAware,ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
private List list;

public SetListAction() {
request = getServletRequest();
}

public String execute() throws Exception {
System.out.println("ARSSSss SetListAction.execute()");
System.out.println("ARSSSss ContextPath="+request.getContextPath());

HttpSession session = request.getSession();
System.out.println("ARSSSss ServletContext="+session.getServletContext());
list = new ArrayList();
list.add("Fruits");
list.add("Apple");
list.add("Mango");
list.add("Orange");
list.add("Pine Apple");

session.setAttribute("baseList",list);
System.out.println("ARSSSsss baseList="+session.getAttribute("baseList").toString());
// ServletActionContext.getRequest().getSession().setAttribute("baseList", list);
//Map session1 = (Map) ActionContext.getContext().get("session");
//session1.put("baseList", list);


return SUCCESS ;
}
public List getList(){
return list;
}
public void setServletRequest(HttpServletRequest request){
this.request = request;
}

public HttpServletRequest getServletRequest(){
return request;
}

public void setServletResponse(HttpServletResponse response){
this.response = response;
}

public HttpServletResponse getServletResponse(){
return response;
}
}



listProcess.jsp
---------------
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Iterator</title>
</head>
<body>
<table border="2px" bgcolor="#CCCC99" cellspacing="1">
<s:iterator value="#session.baseList" id="iteratorItem">
<tr>
<td>Item Value:</td>
<td>
<s:property value="iteratorItem"/>
</td>
<td>
<s:url var="url" action="/viewDetail">
<s:param name="iteratorItem" value="iteratorItem"/>
</s:url>
<s:a href="%{url}">View</s:a>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>









ViewDetailAction.java
---------------------
package ars;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

public final class ViewDetailAction extends ActionSupport implements
ServletRequestAware, ServletResponseAware {

private HttpServletRequest request;
private HttpServletResponse response;

public ViewDetailAction() {
request = getServletRequest();
}

public String execute() throws Exception {

System.out.println("ARSSSsss ViewDetailAction java");

HttpSession session = request.getSession();
String detailInfo = request.getParameter("iteratorItem");
System.out.println("ARSSSsss detailInfo=" + detailInfo);
System.out.println("ARSSSsss request " + request.getParameterNames());
session.setAttribute("baseDetail", detailInfo);
return SUCCESS;
}

public void setServletRequest(HttpServletRequest request) {
this.request = request;
}

public HttpServletRequest getServletRequest() {
return request;
}

public void setServletResponse(HttpServletResponse response) {
this.response = response;
}

public HttpServletResponse getServletResponse() {
return response;
}
}



viewDetailProcess.jsp
---------------------
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Detail</title>
</head>
<body>
<s:text name="#session.baseDetail" />
</body>
</html>