Struts - WordPress.com

Download Report

Transcript Struts - WordPress.com

Introduction to Struts Framework
Introduction - What is Apache Struts?
•The Struts framework was created by Craig R.
McClanahan and was donated to the Apache software
foundation in 2000. Since then it is a open source
software.
•Strut is an open-source framework for building more
flexible, maintainable and structured front-ends in Java
web applications.
•There are two key components in a web application:
–The data and business logic performed on this data
–The presentation of data
• It helps structuring these components in a Java web
app.
•It controls the flow of the web application, strictly
separating these components.
•This separation between presentation, business logic
and control is achieved by implementing the ModelView-Controller (MVC) design pattern.
The Model-View-Controller (MVC) Overview
•Splits up responsibilities for handling user interactions in an
application into three layers:
The Model-View-Controller
• Model
– holds application data and business logic
– is absolutely independent unit.
–Builds Java Bean/EJB.
• View
– Presentation parts of the Model to the user
– independent from the internal implementation of the
Model
– there can be different Views presenting the same
Model data
Controller
•
•
•
•
“bridge” between Model and View.
controls the flow of the application.
receives/interprets user input.
performs operations and executes the
appropriate business logic from the Model.
BENEFITS
• Structural separation of data presentation and
business logic.
• Easy separation of development tasks (web
design, database, …).
• Increases maintainability and extendibility (new
views!).
•Increases reusability of code.
BENEFITS
• Based on standard Java technologies (JSP,
Servlets, JavaBeans).
•Thus running on all kinds of JSP/Servlet
containers.
•Open-source, affordable, no dependence on
external companies.
Struts Architecture
Controller
Client
3
1
Action
Servlet
4
Client
2
Action Form
Model
Action class
Business logic
5
7
View Component
HTML
JSP
8 output
Struts-config.xml
Fig. Struts Application Flow
Server
6
DB
View
Steps:1. The Client sends an HTTP request to the server, as usual. In
Struts applications, an ActionServlet receives HTTP
requests.
2. The ActionServlet consults a configuration file named strutsconfig.xml to figure out what to do next. It realizes that it
has to forward this request to a view component (usually a
JSP).
3. The ActionServlet forwards the request to the form (a Java
representation of every field in the form data). The Action
Form class is also called as an Form Bean. This is a
JavaBean, which has getter & setter methods for the fields
on the form.
4. It then consult the action class (for validating the user input
provided in the HTML form). The action class decides how
to invoke the business logic now.
Steps:5. The business logic processing happens at this stage. The
business logic can also be a part of the Action class or it can
be a separate code.
6. Optionally, the database is also accessed.
7. The result of above steps causes some output to be produced
(which is not in the displayable HTML format yet). This is
now passed to the View component (usually a JSP).
8. The JSP transforms this output into the final output format
(usually HTML).
The struts architecture
http://my-company.com/login.action
class LoginForm extends ActionForm
{
String username,password;
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username=username;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
}
<action-mappings>
<action
path="/login"
type=“com.myproject.action.LoginAction" >
<forward name=“success" path="/jsp/MainMenu.jsp" />
<forward name=“fail"
path="/jsp/LoginView.jsp" />
</action>
</action-mappings>
Working with Struts
•
From strut’s official download page http://struts.apache.org/download.cgi:
– From the recent release, download Struts2-blank.war
– Rename Struts2-blank.war to YourProjectName.war (say MyStruts.war)
– Make sure that your tomcat is up and running.
– Copy MyStruts.war under ~/../webapps/ the war file will be automatically extract
itself under the same directory (~/../webapps/MyStruts).
– Restart your tomcat.
– Test your Struts application through:
http://localhost:8080/ YourProjectName
(http://localhost:8080/MyStruts)
– You should see the following screen:
•Download it from: http://struts.apache.org/download.cgi
•Environment set up: set Java path as usual C:\Program Files
(x86)\Java\jdk1.7.0_17\bin,
•install Tomcat &
•set CLASSPATH for struts as D:\struts-2.3.14\lib.
Struts application structure
MyStruts
|-- META-INF
|-- WEB-INF
|-- example
`-- index.html
(jars, classes, and configurations)
(jsp files) (optional)
(front page) (optional)
MyStruts/WEB-INF/classes
|-- example
(package example, you can find the source under ‘MyStruts/WEB-INF/src/java/example/’)
| |-- ExampleSupport.class
| |-- HelloWorld.class
| |-- Login-validation.xml
| |-- Login.class
| |-- package.properties
| `-- package_es.properties
|-- example.xml
`-- struts.xml
Thanks & Best of luck for
Pre-sem & External Exams