˘ˇˆ ˘ˆ - baychi · 1-http event (post) 8-http response 7-forward 5-invoke controller business...

40

Upload: others

Post on 19-Apr-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

�������������������

� �����������

� �������������������������

Page 2: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

��������

�������������������������������� ������������� ���������� � ����� ��

Page 3: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

!�����"��#�����

$����������"�����������

Page 4: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

%"����

&����������'

(����������������� ��� ������'

)���������*+����

��������

���

Page 5: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

&����������'

,���������

%�����-�.�������#���

����/00#�.����1������1��"0������0

)������������/212

3�������. ����������"#���������������������

4���������-�5+����6������������+�)�

Page 6: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

7��-�5+����6

Browser

Controller

Model

View

1-Http event(get, post)

5-Http response

4-Forward

2-Invoke

3-Set

Page 7: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

%"����

&����������'

(����������������� ��� ������'

)���������*+����

��������

���

Page 8: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����������������

21�����������8� �� ������%�����-�.����������

61)�� ���#�� ��� ��������������� � ���8� ���&94:�;30��

�1)�� ���1��� ��� ��������������� � ���8� ������&94:�;3

<19���"���1���� ���������������� "��������������"

�1)�����"������ ������:��� "1����1����� ���������=17���

Page 9: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

9���"���1���>�������?

>�������:����?�����>0�������:����?

>�������:�����?��"1������1������1�����1%�����������>0�������:�����?

>��:�����?

>�����:����?����">0�����:����?>�����:�����?@>0�����:�����?

>0��:�����?

>��:�����?

>�����:����?�����>0�����:����?

>�����:�����?@>0�����:�����?

>0��:�����?

>����:��:�������?2>0����:��:�������?

>0�������?

>�������:�����"?

>�������:����?�����>0�������:����?

>���:�������?0������������>0���:�������?>0�������:�����"?

Page 10: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

)�����"������ ������:��� "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������:��� "?

Page 11: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����� ����������������

Message from the struts controller meaning that it does not know anything

about that http event

Page 12: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

%"����

&����������'

(����������������� ��� ������'

)���������*+����

��������

���

Page 13: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

(����&����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

Page 14: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����

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

Page 15: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

(���������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

Page 16: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

��������������������������������

)����/��"1������1������1�����1%�����������

�������� ����� ���������������/������������������������ ���������������"��0�������"����"������������ "������

)�� "����� /������:��� "1���

Page 17: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

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

Page 18: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

���������������"�����������

���������#����������������������/

� ��"1������1������1�����1%����

+������������� �

,�����������������K�������������������������"�

7���������������"�������� ����

Page 19: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����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");

}

}

Page 20: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

�����/����������������21����

)����������������L:�/

<html>

<head>

<title>helloworld1 page</title>

</head>

<body>

This is the last page of the helloworld1 example.

</body>

</html>

Page 21: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

%"����

&����������'

(����������������� ���������'

)���������*+����

��������

���

Page 22: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

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

Page 23: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����1

2

3

Page 24: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

(��������� 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

Page 25: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����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>

(��������������������������������������������������������������

Page 26: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

������������������������������

���#�������*�����H������/� M�����"����������������

� 5����"��������������

+�����������"1������1������1�����1%����3���

5��������������������"

����������� 9-4*����7���� ��,�#���*�������,�#���

Page 27: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����6/������"�����������#�������

public class HelloWorld2Form extendsActionForm {

private String message;

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

}

Page 28: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

�����/����������� �����������������"�

<?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>

Page 29: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����</��� �"������������"���"����������"� ��������������

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");

}

Page 30: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

%"����

&����������'

(����������������� ��� ������'

)���������*+����

��������

���

Page 31: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

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

Page 32: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����1 2

3 4

Page 33: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

(��������� ������������"���

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

Page 34: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

�������������������������

-�5��"��

$�����������-�7!��"�������������� ���17�� "�������������� ��������������#���������������������������H����� �������������

Page 35: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����2/������"������

>NO ��"����AP0&94:�;30������:����1���P��� �AP����PN?

>����?

>����?

>����?����������� ���>0����?

>0����?

>��� ?

>����/ ��������AB0��������������B?

+����"�/>����/����������� AB�����"�B�8�AB�@B0?>4Q?>4Q?

>����/�����0?

>0����/ ���?

>0��� ?

>0����?

Page 36: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

����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");

}

Page 37: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

�����/)������������ "������

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):

Page 38: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

������

7��������������� "������#����"�

� �������/���#�������0&94:�;30� ������������������� ���� ���������

7��������������� "�������������������������

� �������/����������7�.��0�7�.������ ������

Page 39: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

)��������

��� ��� �������./� )����������� ����

� 4�������������� ����� �������� �������� ��������

��� ������*������������������

��������������������./������:��� "1���� �������/�������������:��� "1������ "������

Page 40: ˘ˇˆ ˘ˆ - BayCHI · 1-Http event (post) 8-Http response 7-Forward 5-Invoke Controller Business Logic 6-Return url to go (stateless java class) 2-Populate from request 3-Dispatch

4��.�