Monday, August 29, 2011

Post/Redirect/Get Pattern with SEAM === Pass parameters to next page with h:commandButton

Post/Redirect/Get (PRG) is a common design pattern for web developers to help avoid certain duplicate form submissions and allow user agents to behave more intuitively with bookmarks and the refresh button.

Problems


Solutions


With seam
h:commandButton also follows this pattern. When user clicks on h:commandButton, it will automatically send http post request to server. At the phase render response in JSF life cycle, it will send http get to browser to render page. Hence, to pass a parameter to next page after user clicks on h:commandButton, seam can help us to do this task very easily

Page1: which has the command button

<h:commandButton action="execute">
</h:commandButton>


Page1.page.xml
cThe navigation rule will tell seam that whenever render response (page 2), it will pass param1 to page2. When render response, seam follow Post/Redirect/Get, it means that seam send get request to server to render page 2

For page 2, to receive the param which passed from page 1, we just need to declare all params needed in page2.page.xml

page2.page.xml

<param name="param1" value=#{bean2.param1} />
<navigation>
</navigation>