Headline

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

Motivation

This Language:Java web application illustrates the use of the popular Technology:Struts technology in combination with Technology:JSP and a servlet based web server. It introduces the use of Technology:Maven as a good advantage for Language:Java based applications, as well.

Illustration

The Technology:Struts architecture is based on MVC. The view is implemented with Technology:JSP, the controller is based on Language:Java action classes in combination with servlets and the model is provided by a service class and a class for each company, department and employee. The initial Feature:Hierarchical company data is stored in a serialization file (compare Contribution:javaInheritance). We will illustrate the Technology:Struts implementation with an example of showing and cutting a company.

View

The Technology:JSP file for the view of the company offers two textfields and two buttons. One of the textfields shows the name, the other one shows the Feature:Total value. The buttons allow the user to request for further detailed information like a department list. It also allows her to cut all salaries of the company.

...

<s:form action="company">

		...

		<s:iterator value="allCompanies">
			<tr>
				
				<td><s:property value="name"/></td>
				<td><s:property value="total"/></td>
				<td>
					
					<s:url id="cutURL" action="company.cutSalaries">
						<s:param name="id" value="%{id}"/>
					</s:url>
					<s:a href="%{cutURL}">Cut</s:a>
				</td>
				<td>
					
					<s:url id="detailURL" action="company.details">
						<s:param name="id" value="%{id}"/>
					</s:url>
					<s:a href="%{detailURL}">Detail</s:a>
				</td>
			</tr>
		</s:iterator>

		...

The iterator

<s:iterator ... />
creates a table row for each company listed in the ListAllCompaniesAction.java instance. The value
"allCompanies"
refers to the member
List<Company> allCompanies;
of the class. Each company within this list has a getter for its name and another for the Feature:Total value.

Controller

The

action="company.cutSalaries"
of the Feature:Cut link invokes the
cutSalaries()
method of CompanyAction.java, which is, in combination with a servlet, a controller of the application:

@Action(value = "company.cutSalaries",
			results = { @Result(name = "listAllCompanies", type="redirectAction", location="list-all-companies")})
public String cutSalaries() {
	company =  CompanyService.instance().findCompany(Long.parseLong(RequestUtil.getRequestParameter("id")));
	company.cut();
	return "listAllCompanies"; 
}

The

@Action(value = "company.cutSalaries", ...)
maps this method to the action name company.cutSalaries. Whenever this name is called within an action of the Technology:JSP files, this method is invoked.
CompanyService.instance()
returns the instance of the model, which returns the necessary entity for the company. This object is used to perform the companies Feature:Cut method. The
results
parameter redirects the application to the list-all-companies.jsp, which simply means, that the page is reloaded. Every result entry refers to a returned string value of the
@Action(value = "company.cutSalaries", ...)
. In this case, there is only one possible return value.

Model

All data are instantiated within the Singleton CompanyService.java, which is considered as a major part of the model. The instance contains lists of the company, its departments and its employees. As we have seen in the controller description, the Feature:Cut method of all of these entities is invokeable. In our case, the cut method for the company simply invokes the cut method of the departments:

	public void cut() {
		for (Department d : getDepts())
			d.cut();
	}

The lists within this CompanyService.java are initialized by loading a previously serialized company. We strongly recommend to have a look on Contribution:javaInheritance, to get an overview over the serialization and deserialization process in Language:Java.

Architecture

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:

Contributors


Kevin edited this article at Wed, 18 Oct 2017 17:53:10 +0200
Compare revisions Compare revisions

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.