Thursday, 17 May 2012

Forward View and Redirect View in Websphere Commerce


Redirect View 

In the case of redirect view when redirects from a command the View or JSP will run in a new transaction Hence if an  exception occurs in the JSP what is saved in the command won't roll back.Redirect is done via URL  and hence it can't pass objects  back to View.It can only pass name value pairs.Redirect has a browser URL length limitation with IE browser. 

There are two approaches for handling a limited length redirect URL:
1.Override the getViewInputProperties method in the controller command to return only the sets of parameters that are required to be passed to the redirect response.
2.Use a specified special character in the URL parameters to indicate which parameters can be removed from the input parameter string.
For E.g.: While redirecting to TestViewName we need to remove certain parameters due to URL length limitation. In this case property2, property3 has to be removed.

URL="TestViewName";
Property1=" PropertyValue1";
Property2=" PropertyValue2";
Property3=" PropertyValue3";

Using Approach 1 (Override the getViewInputProperties method)

If you are overriding the getViewInputProperties method, the new method can be written so that only the following parameters are passed to the view command:

Property1="PropertyValue1";

public TypedProperty getViewInputProperties() {
TypedProperty tp = viewReqProperties;
tp.remove(Property2);

tp.remove(Property3);
return tp;
}


Using the second approach(Use special character to indicate which parameters can be removed), consider this URL=”TestViewName?Property2*=&Property3*="

This URL parameter instructs the Web Sphere Commerce runtime framework of the following:
The Property2*= specification means that all parameters whose names start with Property2 should be removed.
The Property3*= specification means that all parameters whose names start with Property3 should be removed.
TypedProperty rspProp = new TypedProperty();
rspProp.put(ECConstants.EC_URL, "TestViewName");
rspProp.put(ECConstants.EC_VIEWTASKNAME, ECConstants.EC_GENERIC_REDIRECTVIEW);

ForwardView

In the case of forward view when we are forwarding from a command to a View or a JSP the command and the JSP will run in the same transaction.Hence if an exception in JSP will cause what is saved in command to roll back.Forward View method can pass objects back to the view.

TypedProperty rspProp = new TypedProperty();
rspProp.put(ECConstants.EC_VIEWTASKNAME, "TestViewName");        

Note: If you use forward view while passing control from a command to a view then if user presses refresh button or F5 the command will be executed again as the URL will remain the same command URL for e.g. While passing control from OrderItemAddCmd to a view. Then when user presses F5 button one more item might be added to cart. But in the case of redirect since the URL is changed to the view URL even if user presses F5 button only the view will be loaded again.




2 comments:

  1. Can you explain why are the attributes set in a command handled as normal Objects when using forward and arrays of Objects when redirecting?

    I mean if I set an Attribute in command and forward it, I can use it using normal casting to certain type, while during the redirection I need to use casting to array of the very same type and then use zeroth element of array.

    e.g.
    retur.put("requestSuccessStatus", "1");

    with forwarding I can retrieve it as (String)request.getAttribute("requestSuccessStatus");

    but with redirect I need this:
    ((String[]) request.getAttribute("requestSuccessStatus"))[0];

    Thanks!

    ReplyDelete
  2. Redirect is done via URL and hence can only support name/value pair. Forward works with Objects but redirects can not pass objects back to the view. When trying to access the object retrieved from WCParam, it throws exception.

    ReplyDelete