Wednesday, January 4, 2012

Difference between ViewScope and RequestScope

Here i explained difference between ViewScope and request Scope by simple example.

step1:

create new X-pages and click source. Erase all code and put following code

<?xml version=”1.0″ encoding=”UTF-8″?>

<xp:view xmlns:xp=”http://www.ibm.com/xsp/core”>

<xp:this.afterPageLoad>

<xp:setValue binding=”#{viewScope.x}” value=”0″/>

</xp:this.afterPageLoad>

<xp:link escape=”true”

text=”#{javascript:viewScope.x=@Integer(viewScope.x)+1}”

id=”link1″>

<xp:eventHandler event=”onclick” submit=”true”

refreshMode=”complete” immediate=”false” save=”false”>

</xp:eventHandler>

</xp:link>

</xp:view>

step2:

Save that x-pages and click preview it shows 1. click 1 it shows count of how many time button click

From which example, the viewScope variable hold value till current page

step 3:

create newanother X-pages and click source. Erase all code and put following code

<?xml version=”1.0″ encoding=”UTF-8″?>

<xp:view xmlns:xp=”http://www.ibm.com/xsp/core”>

<xp:this.afterPageLoad>

<xp:setValue binding=”#{requestScope.x}” value=”0″/>

</xp:this.afterPageLoad>

<xp:link escape=”true”

text=”#{javascript:requestScope.x=@Integer(requestScope.x)+1}”

id=”link1″>

<xp:eventHandler event=”onclick” submit=”true”

refreshMode=”complete” immediate=”false” save=”false”>

</xp:eventHandler>

</xp:link>

</xp:view>
<?xml version=”1.0″ encoding=”UTF-8″?>
<xp:view xmlns:xp=”http://www.ibm.com/xsp/core”>
<xp:this.afterPageLoad>
<xp:setValue binding=”#{viewScope.x}” value=”0″/>
</xp:this.afterPageLoad>
<xp:link escape=”true”
text=”#{javascript:viewScope.x=@Integer(viewScope.x)+1}”
id=”link1″>
<xp:eventHandler event=”onclick” submit=”true”
refreshMode=”complete” immediate=”false” save=”false”>
</xp:eventHandler>
</xp:link>
</xp:view>

step4:

Save that x-pages and click preview it shows 1. click 1 it dont change. it show only 1.

Two example code are same . first example i used viewScope and second example i used requestScope .

requestscope Variable hold value untill request hit the server. so each time click link it hit server so each click the RequestScope variable cleared and set as a 0.

No comments:

Post a Comment