Sunteți pe pagina 1din 40

!

" #

$ "
%"
& '
( '
) *+
& '
,
% -. #
/
00#. 1 1 "0 0
) /21
2
3 . "#

4 -5+ 6
+)
7 -5+ 6
1-Http event
(get, post) Controller
2-Invoke

Browser
4-Forward Model

5-Http response
3-Set

View
%"
& '
( '
) *+
21 8 %
-.
61) #
8 &94: ;30
1) 1
8 &94: ;3
<19 " 1
" "
1) " : "1
1
=17
9 " 1
> ?
> : ? >0 : ?
> : ? "1 1 1 1
% >0 : ?
> : ?
> : ? ">0 : ?
> : ?@>0 : ?
>0 : ?
> : ?
> : ? >0 : ?
> : ?@>0 : ?
>0 : ?
> : : ?2>0 : : ?
>0 ?

> : "?
> : ? >0 : ?

> : ?0 >0 : ?
>0 : "?
) " :
"1
>' AB
21@B "AB ,:CC D:2B'?

>E,)7F59 : "5$4!)B
:00%
3 00 7 ) " 21
2009;B
B / 00#. 1 1 "0 0 0 :
"G2G21 B ?

> : "?
> : ?
>0 : ?

> : "?
>0 : "?
>0 : "?
Message from the struts
controller meaning that it
does not know anything
about that http event
%"
& '
( '
) *+
Send Http event
( & 2 to server by
entering URL
in Browser

http event
%
Server prints
H / “Hello World”
In shell Windows
. "
" "
I
( & E
J Server sends
user to the
helloworld1 page

1 http response
"
I
( & E
J Browser
displays
1 helloworld1
page
This creates the http
event the controller
1
is expecting

The struts controller has


forwarded the user to
helloworld1.html after
executing the business logic

Congratulations!
Our “business logic”
has been successfully
executed!
( 2 "
2-Dispatch Controller
1-Http event Controller Business Logic
(get, post) (stateless java class)
(Servlet) 4-Return url to go

Browser
5-Forward
3-Invoke
Tasks:
1 - configure the controller
6-Http response
2 - write the business logic
3 - create the html page
Model
View
(html page)
) / "1 1 1 1
%

/
" 0
" " "
) " / : "1
2/) "

3 I
( & E
J /
>' AB21
@B "AB ,:CC D:2B'?
>E,)7F59 : "5$4!)B :00%
3 00 7 ) " 21
2009;B
B /
00#. 1 1 "0 0 0 : "G2G21 B
?

> : "?
> : 0? Http event

> : "?

>0 : "?
>0 : "? Business logic to call when receiving
the “helloworld” http event
"

# /
"1 1 1 1
%
+
, K
"
7 "
6/ "
" .
public class HelloWorld1Action extends Action {

public ActionForward execute(ActionMapping arg0,


ActionForm arg1, HttpServletRequest arg2,
HttpServletResponse arg3) throws Exception {

// start business logic


System.out.println("Hello World!");
// end business logic

// tell which page to go next


return new ActionForward("/helloworld1.html");
}

}
/ 21
) L
: /
<html>
<head>
<title>helloworld1 page</title>
</head>
<body>
This is the last page of the
helloworld1 example.
</body>
</html>
%"
& '
( '
) *+
Enter message
in the html
I
( & E
J6 form

Submit form to
% server by clicking
on submit button
H /
" http event

I
( & E
J Server prints
Message
* in shell Windows

" "
Server sends
" user to the
1 helloworld2 page

http response

Browser
displays
helloworld2
page
1

3
( 6 "
3-Dispatch Controller
1-Http event Controller Business Logic
(post) (stateless java class)
(Servlet) 6-Return url to go

2-Populate from request


Browser
4-Get properties
7-Forward 5-Invoke

View State
8-Http response
(JavaBean)

Tasks:
View
1 - create the html form Model
(html page) 2 - create the view state
3 - configure the controller to
know which view state to populate
4 - modify the business logic to access the view state
2/ "
+ "1 / (
<html>
<head>
<title>sendMessage page</title>
</head>
<body>
<form method="post" action="/controller/helloworld2">
Message:<input type="text" name="message"/><BR><BR>
<input type="submit"/>
</form>
</body>
</html>
# * H /
M "
5 "
+
"1 1 1 1
% 3
5 "
9-
4* 7
,# * ,#
6/ "
#
public class HelloWorld2Form extends
ActionForm {
private String message;

public String getMessage() {


return message;
}

public void setMessage(String message) {


this.message = message;
}
}
/
"
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD


Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<form-beans>
<form-bean name="helloWorld2Form"
type=“presentation.HelloWorld2Form"/>
</form-beans>

<action-mappings>
<action path="/helloworld2" scope=“request”
name="helloWorld2Form" type=“presentation.HelloWorld2Action"/>
</action-mappings>

</struts-config>
</ " "
" "
public ActionForward execute(ActionMapping arg0,
ActionForm actionForm, HttpServletRequest arg2,
HttpServletResponse arg3)
throws Exception {
// get the property out of the view state
HelloWorld2Form helloWorld2Form =
(HelloWorld2Form) actionForm;
String message = helloWorld2Form.getMessage();

System.out.println("Message: " + message);

return new ActionForward("/helloworld2.html");


}
%"
& '
( '
) *+
Send http event by
entering URL for
html form in

I
( & E
J browser
http event
Server generates
prepopulated form
http response
%
Browser displays
H / prepopulated form

7 "
User modifies
message and
submit form
http event
Server prints
message in
Tasks: shell windows
1 – Modify the html form to be able to be prepopulated
2 – Write the business logic to prepopulate the form Server sends
user to the
3 – Modify controller configuration to handle form helloworld3 page
prepopulation http response

Browser displays
helloworld3 page
1 2

3 4
( "
3-Dispatch Controller
1-Http event Controller Business Logic
(get, post) (Servlet) 6-Return url to go (stateless java class)

2-Populate from request


Browser
4-Get/Set properties
7-Forward 5-Invoke

View State
9-Http response
(JavaBean)

8-Populate form
View from properties Model
(jsp page)
-5 "

$ -7! "
17 "
#
H
2/ "
>NO " AP0&94: ;30 : 1 P AP PN?
> ?
> ?
> ? >0 ?
>0 ?
> ?
> / AB0 B
?
+ " /> / AB " B 8AB@B
0?
>4Q?>4Q?
> / 0?
>0 / ?
>0 ?
>0 ?
6/4 "

public ActionForward execute(ActionMapping


actionMapping,ActionForm
actionForm,HttpServletRequest
arg2,HttpServletResponse arg3)
throws Exception {
HelloWorld3Form helloWorld3Form = (HelloWorld3Form)
actionForm;
helloWorld3Form.setMessage(String.valueOf(
System.currentTimeMillis()));
return actionMapping.findForward("continue");
}
/) "
Extract from the HelloWorld3Action.java (business logic):
public ActionForward execute(ActionMapping
actionMapping,ActionForm actionForm,HttpServletRequest
arg2,HttpServletResponse arg3)
throws Exception {
[…]
return actionMapping.findForward("continue");
}

Extract from struts-config.xml:


<action-mappings>
[…]
<action path="/starthelloworld3" scope="request" name="helloWorld3Form"
type="presentation.StartHelloWorld3Action">
<forward name="continue" path="/helloworldForm3.jsp"/>
</action>
</action-mappings>
7 " # "
/ # 0&94: ;30

7 "

/ 7. 0 7.
)
.
/
)
4

.
/ : "1
/ : "1 "
4 .

S-ar putea să vă placă și