如何获取websphere商业bean中的Url参数?
问题描述:
我想获取URL,尝试以下两种方法,但得到'null'commandContext。尝试了其他一些,但每次都得到空。如何获取websphere商业bean中的Url参数?
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();
不太明白我做错了,请任何人帮忙吗?
我想捕获用户在特定场景中击中的url中存在的参数。
例如,
https://webAddress/MyForm?item=someitem&qty=1
我需要 '项目' 和 '数量'。
答
你没有提到你需要参数值的上下文,所以我假设你正在编写Controller Command的业务逻辑。
当控制器命令由运行时框架实例化时,将调用setRequestProperties()
方法,传入一个TypedProperties
实例,其中包含请求参数(无论是作为GET或POST参数传递,从XML消息映射,或者通过REST层)。
这个(在com.ibm.commerce.commands.ControllerCommandImpl
中)的默认实现将简单地将其存储在requestProperties
属性中,然后您可以通过它访问它,例如performExecute()
。
我通常会推荐人们覆盖setRequestProperties()
来挑选他们想要的值并将它们存储在实例变量中。
不要忘记验证validateParameters()中的参数。
请参阅知识中心的Creating business logic教程,以获得更全面的介绍。
答
它已经有点晚了,但我认为这对别人有帮助。
要从url中获取控制器命令中的查询参数,可以按照此tutorial 中提到的任一方法进行操作,也可以尝试如下方式,获取请求属性并使用查询参数名称获取值
TypedProperty requestProp = getRequestProperties();
String item = requestProp.getString("item");
int itemQty = Integer.parseInt(requestProp.getString("qty"));
其次是您的业务逻辑。
希望这会有所帮助。