J3 Limited
 
Google
WWW J3Ltd
 
Edit System Properties

Introduction

In the previous section, a page to list system properties was completed.

In this section, facilities to edit system properties are added. In order to achieve this the following steps need to be completed:

  • The Struts configuration file is updated with the new infomration
  • The listProperties.jsp, developed in the previous section, is updated with hyperlinks to the new jsp created in this section
  • A new JSP to edit a system property is created
  • A Struts form bean for the new JSP is created
  • A Struts action class is created to control the flow of the edti process
  • Some supporting classes are updated (DAO class) or created.
  • The messages properties file is updated

Edit struts-config.xml

The struts-config.xml is edited. The new action mapping for editProperty has a parameter called method. Struts DispatchAction class uses this to determine which method to call within the controller that is coded (EditPropertyAction)

<?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>
      <form-bean
         name="editPropertyForm"
         type="com.j3ltd.forms.EditPropertyForm">
      </form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards >
   <forward name="listProperties" path="/listProperties.jsp"/>
   <forward name="editProperty" path="/editProperty.jsp"/>
  </global-forwards>
  <action-mappings>
        <action
            path="/listProperties"
            type="com.j3ltd.controller.ListPropertiesAction"
            scope="request"
            input="/listProperties.jsp">
        </action>
        <action
            path="/editProperty"
            type="com.j3ltd.controller.EditPropertyAction"
            parameter="method"
            name="editPropertyForm"
            scope="request"
            validate="true"
            input="/editProperty.jsp">
          <forward name="view" path="/editProperty.jsp"/>
          <forward name="deletedProperty" path="/listProperties.do" redirect="true" />
        </action>
  </action-mappings>
   <message-resources parameter="com.j3ltd.messages.StrutsProject"/>
</struts-config> 

Edit listProperties.jsp

The listProperties.jsp is edited to have hyperlinks to a page that allows properties to be editted (create, update and delete):

<%@ 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>

<html:link page="/editProperty.do">
   <html:param name="method">view</html:param>
   <bean:message key="listPropertiesNewProperty"/>
</html:link>
<br><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">
                     <html:link page="/editProperty.do">
                        <html:param name="method">view</html:param>
                        <html:param name="name">${aProperty.name}</html:param>
                        ${aProperty.name}
                     </html:link>
                  </td>
                  <td align="left" valign="top">${aProperty.value}  </td>
               </tr>
            </c:forEach>
         </c:otherwise>
      </c:choose>
   </tbody>
</table>

</body>
</html:html>

Create editProperty.jsp

A new JSP called editProperty.jsp is created. The contents are as follows:

<%@ 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" %>
<html:html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>
      <bean:message key="${viewItems.titleKey}" arg0="${editPropertyForm.name}"/>
</title>
</head>

<body>
<h1><bean:message key="${viewItems.headingKey}"  arg0="${editPropertyForm.name}"/></h1><br>
<html:errors/>
<html:messages id="msg" message="true">
   <bean:write name="msg"/><br>
</html:messages>

<html:link page="/listProperties.do"><bean:message key="indexListProperties"/></html:link>

<html:form action="/editProperty">
   
<table border="1" width="80%">
   <tr>
      <th><bean:message key="editPropertyLabelName"/></th>
      <td>
         <c:choose>
            <c:when test="${viewItems.newProperty}">
               <html:text property="name" size="40" maxlength="80"/>
               <html:hidden property="method" value="create"/>
            </c:when>
            <c:otherwise>
               ${editPropertyForm.name}
               <html:hidden property="name" />
               <html:hidden property="method" value="update"/>
            </c:otherwise>
         </c:choose>
      </td>
   </tr>
   <tr>
      <th><bean:message key="editPropertyLabelValue"/></th>
      <td><html:textarea property="value" cols="40" rows="10"/></td>
   </tr>
   <tr>
      <td colspan="2"> </td>
   </tr>
   <tr>
      <td colspan="2">
         <html:submit><bean:message key="editPropertyLabelSubmit"/></html:submit>
          
         <html:reset><bean:message key="editPropertyLabelReset"/></html:reset>
          
         <html:submit 
               disabled="${viewItems.newProperty}"
               onclick="document.forms[0].elements['method'].value='delete'">
            <bean:message key="editPropertyLabelDelete"/>
         </html:submit>
      </td>
   </tr>
</table>
</html:form>
</body>

</html:html>

Create A Form Bean

A Struts form bean class is created. This class basically holds the JSP's input field values. The contents are shown below:

package com.j3ltd.forms;

import org.apache.struts.action.ActionForm;

import com.j3ltd.bean.PropertyBean;

public class EditPropertyForm extends ActionForm {
   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;
   }

   
   public void setFormPropertyBean(PropertyBean bean) {
      this.name = bean.getName();
      this.value = bean.getValue();       
   }
   
   public PropertyBean getFormPropertyBean() {
      PropertyBean bean = new PropertyBean();
      bean.setName(this.name);
      bean.setValue(this.value);
      return bean;
   }
}

Create A DispatchAction Controller

The Struts action class is created, to control the flow within the JSP:

package com.j3ltd.controller;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

import com.j3ltd.bean.EditPropertyViewItems;
import com.j3ltd.dao.StrutsProjectDao;
import com.j3ltd.forms.EditPropertyForm;


public class EditPropertyAction extends DispatchAction {
   private static final String FORWARD_VIEW = "view";
   private static final String FORWARD_DELETED = "deletedProperty";
   
   public ActionForward view(
         ActionMapping mapping,
         ActionForm actionForm,
         HttpServletRequest request,
         HttpServletResponse response) {
      String propertyName = request.getParameter("name");
      EditPropertyForm form = (EditPropertyForm) actionForm;
      if (propertyName != null) {
         setupViewItems(request, false);
         form.setFormPropertyBean(StrutsProjectDao.getPropertyByName(propertyName));
      }
      else {
         setupViewItems(request, true);
      }
      return mapping.findForward(FORWARD_VIEW);
   }
   
   public ActionForward update(
         ActionMapping mapping,
         ActionForm actionForm,
         HttpServletRequest request,
         HttpServletResponse response) {
      String propertyName = request.getParameter("name");
      EditPropertyForm form = (EditPropertyForm) actionForm;
      
      StrutsProjectDao.updateProperty(form.getFormPropertyBean());
      
      setupViewItems(request, false);
      ActionMessages messages = new ActionMessages();
      messages.add(ActionMessages.GLOBAL_MESSAGE,
            new ActionMessage("editPropertyStatusUpdateSaved"));
      super.saveMessages(request, messages);
      form.setFormPropertyBean(StrutsProjectDao.getPropertyByName(propertyName));
      return mapping.findForward(FORWARD_VIEW);
   }   
   
   public ActionForward create(
         ActionMapping mapping,
         ActionForm actionForm,
         HttpServletRequest request,
         HttpServletResponse response) {
      String propertyName = request.getParameter("name");
      EditPropertyForm form = (EditPropertyForm) actionForm;

      StrutsProjectDao.createProperty(form.getFormPropertyBean());
      
      setupViewItems(request, false);
      ActionMessages messages = new ActionMessages();
      messages.add(ActionMessages.GLOBAL_MESSAGE,
            new ActionMessage("editPropertyStatusCreateSaved"));
      super.saveMessages(request, messages);
      form.setFormPropertyBean(StrutsProjectDao.getPropertyByName(propertyName));
      return mapping.findForward(FORWARD_VIEW);
   }   
   
   public ActionForward delete(
         ActionMapping mapping,
         ActionForm actionForm,
         HttpServletRequest request,
         HttpServletResponse response) {
      String propertyName = request.getParameter("name");
      StrutsProjectDao.deletePropertyByName(propertyName);

      return mapping.findForward(FORWARD_DELETED);
   }   
   
   private void setupViewItems(HttpServletRequest request, boolean isNewProperty) {
      EditPropertyViewItems viewItems = new EditPropertyViewItems();
      viewItems.setNewProperty(isNewProperty);
      if (isNewProperty) {
         viewItems.setTitleKey("editPropertyTitleNew");
         viewItems.setHeadingKey("editPropertyTitleNew");
      }
      else {
         viewItems.setTitleKey("editPropertyTitleEdit");
         viewItems.setHeadingKey("editPropertyTitleEdit");
      }
      request.setAttribute("viewItems", viewItems);
   }
}

Create A View Bean

A new class called EditPropertyViewItems is created. This class is used to pass all the information that the JSP needs for display purposes:

package com.j3ltd.bean;

public class EditPropertyViewItems {
   String titleKey;
   String headingKey;
   
   boolean newProperty;
   
   public boolean isNewProperty() {
      return newProperty;
   }
   public void setNewProperty(boolean newProperty) {
      this.newProperty = newProperty;
   }
   public String getTitleKey() {
      return titleKey;
   }
   public void setTitleKey(String titleKey) {
      this.titleKey = titleKey;
   }
   public String getHeadingKey() {
      return headingKey;
   }
   public void setHeadingKey(String headingKey) {
      this.headingKey = headingKey;
   }
}             

Update DAO

The dao class is updated to include methods to create, update and delete system properties:

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 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;
   }
   
   public static PropertyBean getPropertyByName(String name) {
      Properties systemProperties = System.getProperties();
      PropertyBean aProperty = new PropertyBean();
      aProperty.setName(name);
      aProperty.setValue(systemProperties.getProperty(name));
      return aProperty;
   }
   
   public static void updateProperty(PropertyBean property) {
      System.setProperty(property.getName(), property.getValue());
   }
   
   public static void createProperty(PropertyBean property) {
      System.setProperty(property.getName(), property.getValue());
   }
   
   public static void deletePropertyByName(String propertyName) {
      System.clearProperty(propertyName);
   }
}

Update Message Resources

The strutsProject.properties file is updated to hold the messages that have been added to the code and jsp's:

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

#
# listProperties resources
#
listPropertiesTitle=List System Properties
listPropertiesHeading=System Property List
listPropertiesNameHeader=Property Name
listPropertiesValueHeader=Property Value
listPropertiesNoProperties=No system properties to list
listPropertiesNewProperty=Create a new property

#
# editProperty resources
#
editPropertyTitleEdit=Edit {0}
editPropertyTitleNew=New Property
editPropertyHeadingEdit=Edit {0}
editPropertyHeadingNew=New Property
editPropertyLabelName=Property name
editPropertyLabelValue=Property value

editPropertyLabelSubmit=Save Property
editPropertyLabelReset=Restore Defaults
editPropertyLabelDelete=Delete Property
editPropertyStatusUpdateSaved=Changes saved OK
editPropertyStatusCreateSaved=Property created OK

Publish To Tomcat

The servers view in Eclipse is opened up:

republish

The "Tomcat v6.0" is right clicked, and "publish" is selected from the popup menu, the status is updated to "synchronized":

synchronized

Run

The project is launched in debug mode on the server:

run

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.

This completes the quick introdution to Struts programming.

 

  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.