在JSF中获取请求参数值 [英] Get request parameter values in JSF

查看:93
本文介绍了在JSF中获取请求参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个<odc:tabbedPanel/>组件.在其中,我在<odc:bfPanel/>组件中有一个页面.我想从我的<odc:tabbedPanel/>托管Bean类的<odc:bfPanel/>中的页面访问(输入文本或单选按钮的)值.请指导我如何解决这个问题.请在这里注意,我不想在这里使用会话.我只需要请求.我尝试了以下选项,但它们对我不起作用.

I have a <odc:tabbedPanel/> component. Inside this I have a page in the <odc:bfPanel/> component. I want to access a value (of a inputtext or radiobutton) from the page in the <odc:bfPanel/> in my <odc:tabbedPanel/> managed bean class. Please guide me as to how do I go about this. Please note here that I do not want to use the session here. I want it in request only. I have tried the below options but they didn't work for me.

选项一

String value = (String) ctx.getExternalContext()
                        .getRequestParameterValuesMap()
                        .get("managedbean.property");

选项二

String value = (String) ctx.getExternalContext()
                         .getRequestParameterValuesMap()
                         .get("property");

选项三

HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance()
                         .getExternalContext().getRequest();
System.out.println(req.getParameter("property"));

选项四

Map requestMap = FacesContext.getCurrentInstance() 
                         .getExternalContext().getRequestHeaderValuesMap(); 
String msgId = (String) requestMap.get("property"); 
System.out.println(msgId);

选项五

HttpServletRequestWrapper r = new HttpServletRequestWrapper(req);
String value1 = r.getParameter("managedbean.property");


我希望该值不在jsp的托管bean中……但我希望在另一个托管bean中实现.在这里,我在页面内有页面(正如我之前提到的选项卡式面板).现在我想要外部标签的托管bean中的值–


i want the value not in the jsp's managed bean ... but i want it in some another managed bean. here i have pages inside a page(as i had mentioned its a tabbed panel). now i want the value in the managed bean of the outer tab –

推荐答案

首先让我们解释一下为什么您尝试的选项不起作用:

Let first explain why the options you tried did not work:

选项1和2无效,因为它以String[]的形式返回值(!!),而不是以String的形式返回单个值.

Option 1 and 2 are invalid because it returns values(!!) as a String[], not a single value as String.

如果参数在那里,选项3应该可以工作.但这不是JSF式的好方法.

Option 3 should work if the parameter was there. But this is not a nice JSF-ish way.

选项4无效,因为未在请求标头中设置参数.

Option 4 is invalid because the parameters are not set in the request header.

选项5无效,因为它根本没有意义.您只是在其中添加了一个额外的抽象层,而实际上在此之间没有任何改变.

Option 5 is invalid because it simply makes no sense. You're only adding an extra abstract layer in between which in fact doesn't change anything here.

使用JSF的方式将是使用 ExternalContext#getRequestParameterMap() :

The JSF-ish way would be the using ExternalContext#getRequestParameterMap():

Map<String, String> parameterMap = (Map<String, String>) externalContext.getRequestParameterMap();
String param = parameterMap.get("paramName");

关于选项3为什么不起作用的原因很可能是因为参数名称不是您认为/期望的那样. JSF即根据视图树中的父UINamingContainer组件添加(木材)客户ID.右键单击Web浏览器中JSF页面的生成的HTML输出,然后选择查看源代码.检查生成的感兴趣的<input>元素的名称.使用那个名称代替参数名称.

As to why option 3 does not work is likely because the parameter name is not what you think/expect it is. JSF namely prepends (woodstocks) client ID's based on parent UINamingContainer components in the view tree. Rightclick the generated HTML output of the JSF page in your webbrowser and choose View Source. Examine the name of the generated <input> element of interest. Use that name instead as parameter name.

也就是说,这毕竟是解决方法,而不是解决方案.但是我们不能合理地建议 real 解决方案,因为功能要求和所用代码尚不十分清楚.您通常将输入元素绑定到支持bean.如果您实际上在另一个支持bean中,那么您也可以从支持bean内部访问该 支持bean,然后依次访问与其绑定的输入值.有关如何操作的信息,请参见互相插入托管Bean .

That said, this is after all a workaround not a solution. But we can't reasonably suggest the real solution since the functional requirement and the code you have are not fully clear. You normally bind the input elements to a backing bean. If you're actually inside a different backing bean, then you can also just access that backing bean from inside the backing bean and then in turn access the input value bound with it. See Injecting Managed Beans in each other for an how to.

这篇关于在JSF中获取请求参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆