Title: Introduction to Struts 2.0
1Introduction to Struts 2.0
- Jenny Ni
- Joey Feng
- Winddays Wang
- Hewmmi Zhu
- Heather Lv
2Contents
- What is Struts?
- Why to use framework?
- Struts 2.0 Overview
- Struts 2.0 MVC components
- Request Lifecycle in Struts 2
- Struts 2.0 Architecture
- Sample Application
- Why we should use Struts 2.0?
- Struts 1.x vs Struts 2.0
- What you need to start using Struts2.0
3What is Struts?
- Open Source java framework for creating web
applications. - Action Based Framework
- Create web application using MVC 2 architecture
- Apache Struts offer two major version
- Struts 1.x
- Struts 2.0
- Struts 2 WebWork Struts
4Why use framework?
- Do we need framework?
- No and Yes.
- No.
- In small applications where you dont want the
overhead of learning new things. - But
- Yes
- We have to use framework in real world
application because- - Automation of common tasks
- Concentrate on higher level concerns.
5Struts 2.0
- Complete new framework based on webwork
framework. - Struts 2.0 implements MVC 2 design pattern.
6Struts 2.0 MVC Components
- Controller-
- Filter Dispatcher-
- First component that start processing that is why
this type of MVC is called front controller MVC - Looks at the request and apply the appropriate
action. - Struts framework handles all of the controller
work. - Its configured in web.xml
- Interceptors-
- Can execute code before and after an Action is
executed. - They can be configured per action basis.
- Can be used for data validation, file upload,
double submit guards.
7Struts 2.0 MVC Components contd.
- Model-
- Implemented by action class
- For model you can use any data access
technologies like JDBC,EJB,Hibernate - View
- Its your result part. It can be JSP,JSTL,JSF etc.
- Presentation part of the MVC
8Request Lifecycle in Struts 2.0
- User Sends Request
- Filter Dispatcher determines the appropriate
action - Interceptors are applied
- Execution of action
- Output Rendering
- Return of Request
- Display of result to user.
9Struts 2.0 Architecture
10Struts 2.0 Architecture
11Why we should use Struts 2.0?
- Simplified Design
- Simplified Action
- Simplified Testability
- Better tag features
- Annotation introduced
- Easy plug-in
- AJAX Support
12Struts 1.x vs Struts 2.0
- How Struts 1.x and Struts 2.0 differ from each
other? - Configuration
- Action Class
- Dependency injection.
- Servlet Dependency
- Validation
- Threading model
- Testability
- Expression Language
13- Struts 1
- Action ?
- ActionForm ?
- ActionForward ?
- struts-config.xml ?
- ActionServlet ?
- RequestProcessor ?
- Struts 2
- Action
- Action or POJOs
- Result
- struts.xml
- FilterDispatcher
- Interceptors
14What you need to start using Struts 2.0?
- Java 5.0
- Tomcat 5.x(Servlet api 2.4 and jsp api 2.0)
15Lets see something real
16Web, Mail 2000
17Web, Mail 2009
18(No Transcript)
19(No Transcript)
20(No Transcript)
21(No Transcript)
22(No Transcript)
23(No Transcript)
24(No Transcript)
25(No Transcript)
26(No Transcript)
27(No Transcript)
28(No Transcript)
29(No Transcript)
30(No Transcript)
31Struts Plugins
32Simple Example
33Struts 1
lthtmlerrors/gt lthtmlform action"/SaveMeeting"gt lt
table border"0" width"100"gt lttrgt ltth
align"right"gt Name lt/thgt lttd
align"left"gt lthtmltext property"name"
size50 /gt lt/tdgt lt/trgt lttrgt ltth
align"right"gt Date
34 lt/thgt lttd align"left"gt lthtmltext
property"date" size"50"/gt lt/tdgt lt/trgt lttrgt
ltth align"right"gt Invitees lt/thgt lttd
align"left"gt lthtmlselect property"invitees"
multiple"true"gt
lthtmloptions collection"employees"
property"value"
labelProperty"label"/gt lt/htmlselectgt
35lt/trgt lttrgt ltth align"right"gt Description
lt/thgt lttd align"left"gt lthtmltextarea
property"description"
rows"4" cols"50" /gt lt/tdgt lt/trgt lttrgt lttd
align"right"gt nbsp lt/tdgt
36 lttd align"left"gt lthtmlsubmit
property"DO_SUBMIT"gt Save
lt/htmlsubmitgt lt/tdgt lt/trgt lt/tablegt lt/htmlformgt
37Struts 2
ltsform action"Meeting" validate"true"gt
ltstoken /gt ltstextfield labelName
namename /gt ltstextfield labelDate"
name"date"/gt ltsselect labelInvitees
name"invitees" list"employees"/gt ltstextarea
labelDescription name"description"
rows"4" cols"50" /gt ltssubmit
valueSave" method"save"/gt lt/sformgt
38Example Revisited
39ltstextfield label"Name" name"name"
tooltip"Meeting name" /gt
40ltsdatepicker label"Date" name"date"/gt
41ltsoptiontransferselect ... /gt
42ltjspinclude page"/ajax/commonInclude.jsp"/
gt ... ltstextarea theme"ajax" label"Description"
name"description" rows"4" cols"50" /gt
43But there's more . . .
44Brian Kernighan Law of Debugging Difficulty
- Debugging is twice as hard as writing the code in
the first place. Therefore, if you write the code
as cleverly as possible, you are, by definition,
not smart enough to debug it.
45Prevention and Cure
46struts.devMode true
47(No Transcript)
48(No Transcript)
49Built-in Testing Support
public class MyActionTest extends
StrutsTestCase public void testExecute()
throws Exception assertTrue(true)
50any.action?debugconsole
51any.action?profilingyes
52Time to Upgrade?
53Tutorials, Guides, and FAQs
54Struts 2 Training Course
55Run Struts 1 Actions as Is
ltaction name"editGangster"
class"org.apache.struts2.s1.Struts1Action"gt
ltparam name"className"gt com.mycompany.gangst
as.EditGangsterAction lt/paramgt ltresultgt
gangsterForm.jsp lt/resultgt lt/actiongt
56How do I get started?
57Where We are Going