Struts 2 Overview2
Struts 2 Overview2
0 an Overview
(http://www.skill-guru.com)
What is Struts?
• Struts is a framework to develop web application easily.
Using Struts, makes easier to develop web application
and maintain them.
• Struts is an open source framework which makes
building web applications easier, based on Java Servlets
and JSP technologies.
• 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.
Why struts? What’s wrong with jsp/servlet coding?
• Using only Servlets – difficult to output a html and needs lot of out.printlns – hard to
read and clumsy
• Using only JSP – added scriptlets and implicit objects into jsp - awkward to see
java inside html– hard to read and maintain – useful if very small application
• Using JSP+ Java beans – Code inside bean and jsp to display . Good choice for
small applications. But what if there is need of multiple type of views? Eg: if there is
need of different language display depending on client location? - making request to
a general servlet, which outputs data according to the client locale, for same url
request, will be good choice – Model 2 architecture evolved.
• Using JSP+Servlets+JavaBeans Model 2 architecture
Request made to servlet, servlet does business calculation using simple java POJO
gets the result. Also decides the view and give back the response using the view to
the client.
Here servlet called – Controller, Business calculation POJO called – Model and JSP
called - View
Uses : the business logic is separated from JSPs and JSP gets displayed depending
upon the result of model (the business function). similar behavior like all
applications above, but the code is more structured now. Changing business logic will
not affect view and vice versa.
Servlet
JSP File
Java function
Why do we need Web Application
Frameworks like Struts when we have good
solution like Model 2 pattern?
bean.setAmout(amount);
boolean flgResult = ejbObject.processAmount(amount);;
if(flgResult){
// success
// dispatch request to same or different page - hard coded
}else{
// dispatch request to same or different page - hard coded
}
Using web framework like struts 2.0 it will look
simpler
Jsp file:
<s:textfield label=“Amount" name=“amount" value="%{amount}" />
In action file you must have simple getter and setter:
double amount;
public double getAmount(){ return amount;}
public void setAmount(double amount){this.amount = amount;}
That’s it. You can directly use the amount in action method
without get extra code:
public String execute() throws Exception{
// use amount directly
return “success”;
}