˘ˇˆ ˘ˆ - baychi · message from the struts controller meaning that it does not know anything...
TRANSCRIPT
�������������������
� �����������
� �������������������������
��������
�������������������������������� ������������� ���������� � ����� ��
!�����"��#�����
$����������"�����������
%"����
&����������'
(����������������� ��� ������'
)���������*+����
��������
���
&����������'
,���������
%�����-�.�������#���
����/00#�.����1������1��"0������0
)������������/212
3�������. ����������"#���������������������
4���������-�5+����6������������+�)�
7��-�5+����6
Browser
Controller
Model
View
1-Http event(get, post)
5-Http response
4-Forward
2-Invoke
3-Set
%"����
&����������'
(����������������� ��� ������'
)���������*+����
��������
���
����������������
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���>'���������AB21@B������"AB��,:CC�D:2B'?
>E�,)7F59������:��� "5$4!�)B:00%������� �����3��������00�7�������)�� "������212009;BB����/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
%"����
&����������'
(����������������� ��� ������'
)���������*+����
��������
���
(����&����2
%����������H��������/
� ����.�������������"������"��"������I(����&����EJ��������������� ������������� ����������1
� ��������������"����I(����&����EJ����������1
Send Http eventto server by
entering URLin Browser
Server prints“Hello World”
In shell Windows
Server sends user to the
helloworld1 page
Browserdisplays
helloworld1page
http event
http response
����
Congratulations!Our “business logic” has been successfully
executed!
1
2
This creates the http event the controller
is expecting
The struts controller has forwarded the user to helloworld1.html after
executing the business logic
(���������2�����������"���
Browser
Controller(Servlet)
ModelView(html page)
1-Http event(get, post)
6-Http response
5-Forward3-Invoke
ControllerBusiness Logic
(stateless java class)
2-Dispatch
4-Return url to go
Tasks:1 - configure the controller2 - write the business logic3 - create the html page
��������������������������������
)����/��"1������1������1�����1%�����������
�������� ����� ���������������/������������������������ ���������������"��0�������"����"������������ "������
)�� "����� /������:��� "1���
3�����I(����&����EJ���������/>'���������AB21@B������"AB��,:CC�D:2B'?
>E�,)7F59������:��� "5$4!�)B:00%������� �����3��������00�7�������)�� "������212009;BB����/00#�.����1������1��"0������0����0������:��� "G2G21���B?
>������:��� "?
> ���:�����0?
>�����:�����"�?���������� ������������������ �������������������������������
>0�����:�����"�?
>0������:��� "?
����2/)�� "������� �������������������
Http event
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>
%"����
&����������'
(����������������� ���������'
)���������*+����
��������
���
I(����&����EJ6
%����������������H��������/
� �������� ����� �"I(����&����EJ���������������* ����������������"��"������������������"����������������������� ���1
Submit form to server by clickingon submit button
Server printsMessage
in shell Windows
Server sendsuser to the
helloworld2 page
Browserdisplays
helloworld2page
Enter messagein the html
form
http event
http response
����1
2
3
(��������� 6�����������"���
Browser
Controller(Servlet)
ModelView(html page)
View State(JavaBean)
1-Http event(post)
8-Http response
7-Forward 5-Invoke
ControllerBusiness Logic
(stateless java class)6-Return url to go
2-Populate from request
3-Dispatch
4-Get properties
Tasks:1 - create the html form2 - create the view state3 - configure the controller toknow which view state to populate4 - 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 extendsActionForm {
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");
}
%"����
&����������'
(����������������� ��� ������'
)���������*+����
��������
���
I(����&����EJ�
%�������������������H�������/
� 7�������"� ���������������������������������������������������
Send http event by entering URL for
html form inbrowser
Server generates prepopulated form
Browser displaysprepopulated form
User modifies message andsubmit form
Server printsmessage in
shell windows
Server sendsuser to the
helloworld3 page
Browser displayshelloworld3 page
http event
http response
http event
http response
Tasks:1 – Modify the html form to be able to be prepopulated2 – Write the business logic to prepopulate the form3 – Modify controller configuration to handle formprepopulation
����1 2
3 4
(��������� ������������"���
Browser
Controller(Servlet)
ModelView(jsp page)
View State(JavaBean)
1-Http event(get, post)
9-Http response
7-Forward 5-Invoke
ControllerBusiness Logic
(stateless java class)
3-Dispatch
6-Return url to go
2-Populate from request4-Get/Set properties
8-Populate form from properties
�������������������������
-�5��"��
$�����������-�7!��"�������������� ���17�� "�������������� ��������������#���������������������������H����� �������������
����2/������"������
>NO ��"����AP0&94:�;30������:����1���P��� �AP����PN?
>����?
>����?
>����?����������� ���>0����?
>0����?
>��� ?
>����/ ��������AB0��������������B?
+����"�/>����/����������� AB�����"�B�8�AB�@B0?>4Q?>4Q?
>����/�����0?
>0����/ ���?
>0��� ?
>0����?
����6/4��������"����������������� ���
public ActionForward execute(ActionMappingactionMapping,ActionFormactionForm,HttpServletRequestarg2,HttpServletResponse arg3)
throws Exception {
HelloWorld3Form helloWorld3Form = (HelloWorld3Form)actionForm;
helloWorld3Form.setMessage(String.valueOf( System.currentTimeMillis()));
return actionMapping.findForward("continue");
}
�����/)������������ "������
public ActionForward execute(ActionMappingactionMapping,ActionForm actionForm,HttpServletRequestarg2,HttpServletResponse arg3)
throws Exception {
[…]
return actionMapping.findForward("continue");
}
<action-mappings>
[…]
<action path="/starthelloworld3" scope="request" name="helloWorld3Form" type="presentation.StartHelloWorld3Action">
<forward name="continue" path="/helloworldForm3.jsp"/>
</action>
</action-mappings>
Extract from struts-config.xml:
Extract from the HelloWorld3Action.java (business logic):
������
7��������������� "������#����"�
� �������/���#�������0&94:�;30� ������������������� ���� ���������
7��������������� "�������������������������
� �������/����������7�.��0�7�.������ ������
)��������
��� ��� �������./� )����������� ����
� 4�������������� ����� �������� �������� ��������
��� ������*������������������
��������������������./������:��� "1���� �������/�������������:��� "1������ "������
4��.�