Intent

Web programming in Language:Java with Technology:Struts configuring with XML

Motivation

This is a simple Struts-based web implementation of the basic 101companies-features. Java annotations are used for redirection rules. Server-side storage or persistence relies on XML an object streams. See Contribution:strutsAnnotation for a similar implementation that uses annotations instead for the redirection rules.

Illustration

The company data is stored in a serialization file. To obtain the stored data, there is an auxiliarly method in the Company class.

public class Company { 
 ...
public static Company readObject(String filename) {
  Object o = null;
  try {
	FileInputStream fis = new FileInputStream(filename);
	ObjectInputStream in = new ObjectInputStream(fis);
	o = in.readObject();
	in.close();
  } catch (IOException e) {
     e.printStackTrace();
  } catch (ClassNotFoundException e) {
	 e.printStackTrace();
  }
  return (Company) o;
}
...

Based on the struts2 architecture, the controller layer is implemented by Java classes (action classes) that extend the class ActionSupport and implement the Preparable interface. This approach uses the xworks framework and a simple CRUD design, where only one action is necessary to all operations (edit and cut salaries of a department, edit and cut salaries of an employee, and so on). CompanyAction is the single Action class in this project, with the definition:

public class CompanyAction extends ActionSupport implements Preparable {
... 
}


The view layer might be implemented using Java Server Pages (JSPs) or other template languages such as Velocity. Here, we use JSPs to implement the view layer. The listing bellow shows a code fragment of the department.jsp view component. You should notice some special tags such as s:form and s:iterator, which are declared in the Struts tag library. Using theses tags we are able to refer to properties of the action that redirected to the specific view.

...
<h2>Department details</h2>

<s:form action="DEP!save.action" method="post">
	<s:textfield name="department.name" value="%{department.name}" label="Name" size="30" />
	<s:textfield name="department.manager.person.name" value="%{department.manager.person.name}" label="Manager" size="30" />
	<s:textfield name="department.manager.salary" value="%{department.manager.salary}" label="Manager salary" size="15" />
	<s:textfield name="department.manager.person.address" value="%{department.manager.person.address}" label="Manager address" size="30" />
	<s:textfield value="%{department.total()}" label="Total salaries" readonly="true" size="30" />
	<s:hidden name="empName" value="%{department.manager.person.name}" />
	<s:hidden name="deptName" value="%{department.name}" />
	<s:submit value="Save" />
	<s:submit value="Cut" action="cutSalaries"/>
	<s:submit value="Company details" action="index" />
</s:form>
<br>
<h2><s:text name="Subdepartments" /></h2>
<table class="outline" border="1" width="50%">
	<tr>
		<th width="70%"><s:text name="Name" /></th>
		<th width="30%">Edit department</th>
	</tr>
	<s:iterator value="department.subunits" status="status">
	<tr>
		<s:if test="department">
			<td class="nowrap"><s:property value="name" /></td>
			<td class="nowrap">
				<s:url action="DEP!input" id="link">
					<s:param name="department.name" value="name" />
				</s:url> 
				<a href="<s:property value="#link"/>">Detail</a>
			</td>
		</s:if>
	</tr>
	</s:iterator>
</table>
...

As explained, an XML file declares which view should be displayed as an action response. For instance, if the cutSalaries action was performed, the system should redirect to one of two components: department.jsp or employee.jsp. This decision depends on the returned value of the corresponding method ( implemented by the CompanyAction class).

public String cutSalaries() {
  String redirect = SUCCESS;
  if(department != null && deptName != null) {
    service.cutDepartmentSalaries(deptName);
	redirect = DEPARTMENT SAVE;
  }
  else if(employee != null && empName != null) {
	service.cutEmployeeSalary(empName);
	redirect = EMPLOYEE SAVE;
  }
  return redirect;
}

The struts.xml file informs the next view that should be displayed, depending on the returned value of a call to the cutSalaries method.


<action name="cutSalaries" class="org.softlang.action.CompanyAction" method="cutSalaries">
  <result name="department.save">/WEB-INF/jsp/department.jsp</result>
  <result name="employee.save">/WEB-INF/jsp/employee.jsp</result>
</action>

Usage

Requirements:

Import (Eclipse only):" Import the strutsAnnotation implementation into eclipse as Technology:Maven project:
  • Click the "File"-button in the menu bar and "Import...".
  • Select the "Maven" folder and "Existing Maven Projects".
  • Browse to your local "strutsAnnotation" folder and "Finish".
Build:
  • Run "mvn clean" and "mvn install" in the root directory of the struts 2 implementation or (Eclipse only) right click on your imported project and first click "Run As" -> "Maven clean" and second "Run As" -> "Maven install".
  • Copy the target/struts2app.war file to the web-application folder of your Web server (JBoss application server: /server/default/deploy).
Run

Issues

  • Potentially simplify Controller/Service; no service object needed
  • Add substantial README/code documentation

Metadata


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.