Just_another_developer
Just_another_developer

Reputation: 5957

How to get Url Parameter in websphere commerce bean?

I am trying to get URL, tried following two methods but got 'null' commandContext. Tried some other also but got null every time.

  Object req = commandContext.getRequest();
 com.ibm.commerce.webcontroller.HttpControllerRequestObject req1 = (com.ibm.commerce.webcontroller.HttpControllerRequestObject) req;
javax.servlet.http.HttpServletRequest httpReq = req1.getHttpRequest();
ses = httpReq.getSession();


 HttpServletRequest request =
 ((com.ibm.commerce.webcontroller.HttpControllerRequestObject) this 
 .getCommandContext()
 .getRequest())
 .getHttpRequest();

Don't quite get what I am doing wrong, would please anyone help?

I want to catch parameters present in url that user hit in specific scenario.

For instance,

https://webAddress/MyForm?item=someitem&qty=1

I need to get 'item' and 'qty'.

Upvotes: 2

Views: 1990

Answers (2)

Narendra Jaggi
Narendra Jaggi

Reputation: 1338

It been a little late, but I think it will be helpful to others.
To get the query parameters in the controller command from the url, You can follow the either of the approach mentioned in this tutorial or you can try like following , Get the request properties and fetched the value by using query parameter name

TypedProperty requestProp = getRequestProperties();
String item = requestProp.getString("item");
int itemQty = Integer.parseInt(requestProp.getString("qty"));

followed by your businees logic.
Hope this helps.

Upvotes: 3

dovetalk
dovetalk

Reputation: 1991

You don't mention the context in which you need the parameter values, so I'll assume you're writing the business logic for a Controller Command.

When a controller command is instantiated by the runtime framework, it will invoke the setRequestProperties() method, passing in a TypedProperties instance, which contains the request parameters (whether passed as GET or POST parameters, mapped from an XML message, or through the REST layer).

The default implementation of this (in com.ibm.commerce.commands.ControllerCommandImpl) will simply store this in the requestProperties attribute from which you can then access it in, say, performExecute().

I usually recommend people to override setRequestProperties() to pick out the values they want there and store them in instance variables, though.

Don't forget to validate the parameters in validateParameters().

Refer to the Creating business logic tutorial in the Knowledge Center for a more thorough walk-through.

Upvotes: 0

Related Questions