J3 Limited
 
Google
WWW J3Ltd
 
List System Properties Page

Introduction

In the previous section a basic web application was created and tested to encure that all the necessary components are present and configured correctly.

In this section a Struts page that lists system properties is developed and run.

Edit struts-config.xml

The struts-config.xml is edited as follows:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
<form-beans />
<global-exceptions />
<global-forwards >
<forward name="listProperties" path="/listProperties.jsp"/>
</global-forwards>
<action-mappings>
<action
path="/listProperties"
type="com.j3ltd.controller.ListPropertiesAction"
scope="request"
input="/listProperties.jsp">
</action>
</action-mappings>
<message-resources parameter="com.j3ltd.messages.StrutsProject"/>

</struts-config>

Edit index.jsp

The index.jsp file is updated to hold a hyperlink to the Struts action that is about to be added:

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:html>
<title><bean:message key="indexTitle"/></title>
<body>
<h1><bean:message key="indexHeading"/></h1><br>
<html:link page="/listProperties.do"><bean:message key="indexListProperties"/></html:link>
</body>
</html:html>

the uri for the tag libraries is found by opening up the struts jar file struts-taglib-1.3.8.jar. It contains the tld files. For example, the file struts-html.tld, when opened in a text editor, contains the line "<uri>http://struts.apache.org/tags-html</uri>"

Create listProperties.jsp

A new jsp called listProperties.jsp is created, with the following content:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><bean:message key="listPropertiesTitle"/></title>
</head>
<body>
<h1><bean:message key="listPropertiesHeading"/></h1><br>
<table border="1">
   <thead>
      <tr>
         <th align="left">
            <bean:message key="listPropertiesNameHeader"/>
         </th>
         <th align="left">
            <bean:message key="listPropertiesValueHeader"/>
         </th>
      </tr>
   </thead>
   <tbody>
      <c:choose>
         <c:when test="${empty systemProperties}">
            <tr>
               <td align="left" colspan="2">
                <bean:message key="listPropertiesNoProperties"/>
               </td>
            </tr>
         </c:when>
         <c:otherwise>
            <c:forEach items="${systemProperties}" var="aProperty">
               <tr>
                  <td align="left" valign="top">${aProperty.name}</td>
                  <td align="left" valign="top">${aProperty.value}  </td>
               </tr>
            </c:forEach>
         </c:otherwise>
      </c:choose>
   </tbody>
</table>

</body>
</html:html>
                        
  • The JSTL core tag lib is used. The uri specified is "http://java.sun.com/jstl/core_rt". This was found by opening up the struts jar file standard-1.0.2.jar, and looking at the tld files' content.
  • For internationalized messages, the bean:message tag is used. JSTL has the fmt:message tag. In order to avoid repeating the message file name in web.xml, struts-config.xml and possibly the jsp's, the Struts bean tag has been used: the message file name is only present in struts-config.xml

Create The Messages File

The message file, which contains all the text that is displayed by the application, is created in the src folder (src\com\j3ltd\messages\StrutsProject.properties):

#
# index.jsp resources
#
indexTitle=Struts Project
indexHeading=This is the struts index page
indexListProperties=Display a list of the system properties 

#
# listProperties.jsp resources
#
listPropertiesTitle=List System Properties
listPropertiesHeading=System Property List
listPropertiesNameHeader=Property Name
listPropertiesValueHeader=Property Value
listPropertiesNoProperties=No system properties to list                          
                          

Create Supporting Files (POJO, DAO)

A Java bean is created which holds a system property's name and value:

package com.j3ltd.bean;

public class PropertyBean {
	private String name;
	private String value;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
}                          
                          

A class to get the system properties into a list is created:

package com.j3ltd.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.j3ltd.bean.PropertyBean;


public class StrutsProjectDao {
	public static List<PropertyBean> getPropertyList() {
		Properties systemProperties = System.getProperties();
		ArrayList propertyBeans = new ArrayList(systemProperties.size());
		for (Object aKey : systemProperties.keySet()) {
			Object aValue = systemProperties.get(aKey);
			PropertyBean aProperty = new PropertyBean();
			aProperty.setName(aKey.toString());
			aProperty.setValue(systemProperties.getProperty(aKey.toString()));
			propertyBeans.add(aProperty);
		}
		return propertyBeans;
	}
}                          
                          

Create An Action Controller

Finally, the Struts Action controller class is written:

package com.j3ltd.controller;


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 com.j3ltd.factory.StrutsProjectDao;


public class ListPropertiesAction extends Action {
	@Override
	public ActionForward execute(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response)
			throws Exception {
			request.setAttribute("systemProperties", 
                              StrutsProjectDao.getPropertyList());
			return mapping.findForward("listProperties");
	}
}                          
                          

Run The Project

The project is launched in debug mode, and the index page is displayed, in Eclipse:

index.jsp

The hyperlink is clicked to display the listProperties.jsp:

property list

A Jar file of the project files can be downloaded here. The jar file's contents can be extracted using various opensource archive utilities, or using the JDK's jar command line tool.

In the next section facilities to edit system properties are added.

 

  Copyright © 2006 J3 Ltd Permission is granted to reproduce material on this page, on the condition that a reference to "WWW.J3Ltd.com" is given as the source of the material.