Apache Struts 2, Java SE Development Kit, Apache Tomcat

mobdescriptiveSoftware and s/w Development

Oct 28, 2013 (3 years and 8 months ago)

64 views

Apache Struts 2, Java SE Development Kit, Apache Tomcat



We will start with a simple Struts2 application. All what you need for now:


Download

Java SE De
velopment Kit



1. Run the self
-
installing executable to unpack and install the JDK software bundle



2. Add JAVA_HOME to the location of your JDK, C:
\
Program Files
\
Java
\
jdk1.6.0_21

(see
what verision did you installed)


3. Update Path environment variabl
e, add the value %JAVA_HOME%
\
bin



4. Open a new command prompt and run javac
-
version to verify that it is correctly installed.



Download and install

Apache Tomcat

(
Download version: Bin
ary Distributions
-
> Core
-
>
.zip version
)


Check Tomcat installation & configuration: http://127.0.0.1:8080/



Download

lesson1.war

file and save to your Tomcat


C:
\
Program Files
\
Apache Software Foundation
\
Tomcat 6.0
\
webapps


directory. Tomcat will create lesson1 directory.



Test Struts 2 Lesson 1 example:
http://127.0.0.1:80
80/lesson1/test.action



Examine C:
\
Program Files
\
Apache Software Foundation
\
Tomcat 6.0
\
webapps
\
lesson1
directory. In WEB
-
INF
\
web.xml all requests are redirected to FilterDispatcher. WEB
-
INF
\
classes
\
struts.xml configures User class. When you request test.
action, execute() method
from User class sets Congratulation message, displayed by test.jsp.


Actions do the actual work for a given request, it serves as a data carrier in the framework’s
automatic transfer of data from the request to the view, assists th
e framework in determining
which result should render the view that’ll be returned in the request response. In the Lesson 1
the action’s work is to build a customized greeting for the User class.


















Eclipse IDE for Java EE Developers, Struts
2 tags



Starting Lesson 2 we will use Eclipse IDE to enhance our application.


Stop Tomcat server:
C:
\
apache
-
tomcat
-
6.0.29
\
bin
\
shutdown.bat to stop

the tomcat

Download

lesson2.war
.



Download

Eclipse IDE for Java EE Developers

and unpack zip file to


C:
\
Program Files
\
eclipse.


Run C:
\
Program Files
\
eclipse
\
eclipse.exe, close

welcome window. In Project Explorer, right
click mouse and select Import
-
>War file. In War Import click Browse... and select
lesson2.war file. Click Finish. Right click mouse on lesson2 project. Select Run As
-
> Run on
Server. Open Apache, select Tomcat v6
.0 server. Click Next, Browse... Select C:
\
Program
Files
\
Apache Software Foundation
\
Tomcat 6.0, click Finish.


Change url to http://127.0.0.1:8080/lesson2/forum.action You will get input text element.
Open web browser and run http://127.0.0.1:8080/lesson2/
forum.action Play with the
application, add new forum thread.


Explorer lesson2 web project. Open Java Resources: src, WebContent, WebContent WEB
-
INF, com.onepd.struts2.lesson tabs.


Forum.java implements Action interface
-

Struts 2 entry point execute() m
ethod and set/get
methods for name variable.



forum.jsp uses

Struts 2 tags

to pull data into rendering view page, to build the user interface of
our web application. Some of these tag
s mimic standard HTML tag while providing added
value, other tags create non
-
standard, but useful controls. s:form tag renders HTML an input
form, s:textfield renders an HTML input field of type text, s:submit tag is used together with
the form tag to prov
ide form submissions, s:property is used to get the property of a value.





















Model
-
View
-
Controller (MVC), Object
-
Graph Navigation Language (OGNL)


In this lesson we will introduce ActionContext, interceptors, the ValueStack, and OGNL.


The
ActionContext contains all of the data that makes up the context in which an action
occurs. This includes the ValueStack and stuff the framework itself will use internally, such
as the request, session, and application maps from the Servlet API.


Intercept
ors are invoked both before and after the action, they allow common, cross
-
cutting
tasks to be defined in clean, reusable components that you can keep separate from your action
code.


Struts 2 uses the ValueStack as a storage area for all application domai
n data that will be
needed during the processing of a request, it is manipulated there during action execution.


OGNL is used in many places in the framework to reference and manipulate data in the
ValueStack. It binds HTML form fields to data objects on t
he ValueStack for data transfer,
pulls data into the rendering of JSPs and other result types.

Download

lesson3.war
.


Make sure tha
t Tomcat service is stopped. Start Eclipse and import lesson3.war file. Open
Servers tab, right click mouse on Tomcat V6.0 Server as localhost, open Add and Remove
Projects..., add lesson3 project, click Start the server.


Open web browser and enter http:/
/127.0.0.1:8080/lesson3/forum.action


Open lesson3 project. In our Forum application classes from com.onepd.struts2.lesson
represent the Model and test.jsp represents the View. Variables name and threads from
Forum.java stored in ValueStack and transferred

by OGNL to test.jsp. We use s:iterator tag to
navigate through threads list created in ThreadSession class and saved in the session by
Forum class that implements SessionAware.