Contribution:
strutsAnnotation
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 ... />
"allCompanies"
List<Company> allCompanies;
Controller
The
action="company.cutSalaries"
cutSalaries()
@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", ...)
CompanyService.instance()
results
@Action(value = "company.cutSalaries", ...)
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
- The Technology:JSP files for the view are located in this!!src/main/webapp/WEB-INF/content.
- The index.jsp in the this!!src/main/webapp folder represents the initial page, which redirects to the first list-all-companies.jsp. The this!!src/main/webapp folder also contains the Language:CSS files for this application.
- The this!!src/main/resources folder contains two necessary files. The this!!src/main/resources/sampleCompany.ser provides the serialized company data. The this!!src/main/resources/struts.xml Language:XML file defines, that the initial page of this application is the index.jsp.
- The main Language:Java code of the application is located in this!!src/main/java/org/softlang. We have four folders for the different concerns. this!!src/main/java/org/softlang/actions contains the actions described in the illustration section for the controller. The this!!src/main/java/org/softlang/basics folder contains all necessary classes for the deserialized company. The CompanyService.java is located in the this!!src/main/java/org/softlang/services folder. The last folder this!!src/main/java/org/softlang/util contains some helpful Language:Java files for the deserialization.
Usage
Requirements:
- Technology:Maven (Version 2.x) as Technology:Eclipse plugin (http://eclipse.org/m2e/download/) or standalone (http://maven.apache.org/download.html)
- Web server or application server based on the servlet technology (We recommend JBoss application server, but Technology:Apache Tomcat will also be sufficient).
- 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".
- 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).
- Start your Web server (JBoss application server:
/bin/run.bat (Windows) or /bin/run.sh (Unix)). - Start your Web browser and go to http://localhost:8080/struts2app.
Contributors
Backlinks
User contributions
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.