Welcome
to the world of struts 2 framework. In this article you'll get how to
develop basic Hello World! Application using struts 2 in Netbeans 7.
As operating system I am using Linux Ubuntu but, it does not matter
which OS you use, the only thing matter is Struts 2.
Softwares
you'll need.
These
are the sites from where you can download all needed softwares and
file.
- JDK 1.5 or above. (Download)
- Netbeans 7 (Download)There is option for the particular platform supported version in both above software.
- Apache struts 2 jar file. (Download)
You
do not need to install server separately, with Netbeans set-up you
can install two servers, Glassfish and Apache tomcat. You can use
either at once for deploy and running the application.
Lets
do it!
Create
web application from the File -> New Project, from Netbeans.
Give
your project name and set physical path of the project.
Select
the server from the list and the Java EE version. You can set your
context path.
[Note
: It is preferable to select Java EE 5.]
Directory Structure
After
completion of start up process you'll get the directory structure of
the project like this.
Now
first thing you have to do is add some struts 2 JAR file. Just add
below mentioned JAR files from the Struts 2 you have downloaded.
- commons-logging-1.0.4.jar
- freemarker-2.3.8.jar
- ognl-2.6.11.jar
- struts2-core-2.0.12.jar
- xwork-2.0.6.jar
Struts
2 is combine of many jar files. This jar file version may differ with
the version of the Struts 2.
Note
: We'll study each JAR files in future articles.
The
above directory structure represent the basic structure. You have to
take care about the files you are going to create.
- All the JSP files should kept in Web Pages folder.
- Keep you STRUTS.XML and Property file in WEB-INF -> classes(You can choose your name here)
- Keep your action class files in Source Packages folder.
Take
a look at below directory structure of this program for reference.
- HelloStruts
- Web Pages
- META-INF
- context.xml
- WEB-INF
- classes
- ApplicationResources.Properties
- struts.xml
- WEB.XML
- index.jsp
- welcome.jsp
- Source Packages
- javabysm.struts2
- HelloStruts.java
- Libraries
- common-logging-1.0.4.jar
- freemarker-2.3.8.jar
- ognl-2.6.11.jar
- struts2-core-2.0.14.jar
- xwork-2.0.7.jar
- Apache Tomcat 7.0.11 (Note: Apache Tomcat version is what you have installed)
- Configuration Files
Make
sure you follow the structure coz it may create problem if file are
not at there places.
WEB.XML
WEB.XML
is the deployment descriptor which leads the execution of the
project. You can say this is the entrance of the project execution.
In this application we are using FilterDispatcher. This filter
executes actions by consulting the
ActionMapper
and determining if the requested URL should invoke an action. If the
mapper indicates it should, the
rest of the filter chain is stopped
and the action is invoked. This is important, as it means that
filters like the SiteMesh filter must be placed before
this filter or they will not be able to decorate the output of
actions.
Index.jsp
As
you can see in the above image of INDEX.JSP there are few tags belong
to struts 2 framework.
First
of all the tag <%@taglib ... %>
need to include and use struts 2 tags.
Next
one is <s:actionerror/>
The actionerror tag is a UI tag that renders action errors (in the
jsp pages.) if they exists while the actionmessage tag renders action
messages if they exists. We have created an action class that uses
methods
addActionMessage(
String
)
and
addActionError(
String
)
within
the execute() method. The addActionMessage(
String
)
will
print the passed string on the success jsp page while the
addActionError(
String
)
will
print the passed string on the error jsp page.
<s:form>
is same as we have in JSP, the difference is JSP form tag directly
calls the servlet where as in struts 2 action refers to the action
mentioned in the struts.xml
file.
On
the click of submit button the respective action in the struts.xml
file will call.
Welcome.jsp
Above
image represent the welcome.jsp file. Same as index.jsp we have
included the struts tag library.
We
have used <s:property > tag to display the value of the class
attribute. The property tag is a generic tag that is used to
get the property of a value, which will default to the top of
the stack if none is specified.
HelloStruts.java
Above
is the action or we can say class which have the business logic.
Above action class has one String attribute named USERNAME. As
execute() method which a default method for any struts 2 action
class.
Method
execute() return success
if user have inserted something in the textbox in the index.jsp, else
it return error. In
addition it sets the ActionError with the value of property name
error.login. The valu
of error.login will
reflect in the tag <s:actionerror> in the index.jsp page.
At
the end of the execute() method control will move to struts.xml
file and respective result tag
will execute. If it returns success then Welcome.jsp will
call, otherwise Index.jsp.
STRUTS.XML
The
Struts 2 Framework uses a configuration file (struts.xml) to
initialize its own resources. These resources include:
- Interceptors that can pre-process and post-process a request
- Action classes that can call business logic and data access code
- Results
that can prepare views using JavaServer Pages, Velocity and
FreeMarker templates
execute()
method. If execute() method returns success, user will be redirected
to Welcome.jsp else to Index.jsp.Also note that a constant is specified with name struts.custom.i18n.resources. This constant specify the resource bundle file that we created in struts.xml. We just have to specify name of resource bundle file without extension (ApplicationResources without .properties).
Our HelloStruts(POJO) contains the method execute() which is the default method getting called by Sturts2. If the name of method is different, like call(); then we should specify the method name in
<action
name="hellostruts" method=”call”
class="javabysm.struts2.HelloStruts">
ApplicationResources.Properties
Resource
Bundle:
Resource Bundle is very useful Java entity that helps in putting the static content
away from the source file. Most of the application define a resource
bundle file such as ApplicationResources.properties file which
contains static messages such as Username or Password or Invalid
Username/Password.
We have defined an ApplicationResources.properties file for our application. This property file should be present in WEB-INF/classes folders when the source is compiled.
Struts.xml
map this value for us in the JSP file as we use <constant> tag
in it.
e.g.
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />
The
value attribute of above tag represent name of the Properities file.
With use of this functionality we can use constants of the properties
file in out JSP file.
e.g.
<s:textfield name="username" key="label.username"
size="20" />
The attribute
KEY
assigned
a value
label.username
which
is nothing but a constant in properties file. The value of the particular constant will assign here.
That's
it!
All
you need to do now is run the application. You'll get something like
below.
You'll
get error if you keep text box empty and press the submit button.
No comments:
Post a Comment