Java 在 JSF 中获取请求参数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3351348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:31:42  来源:igfitidea点击:

Get request parameter values in JSF

javajsf

提问by user265950

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.

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

option one

选项一

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

option two

选项二

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

option three

选项三

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

option four

选项四

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

option five

选项五

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


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 –

我想要这个值不在 jsp 的托管 bean 中......但我想要它在另一个托管 bean 中。在这里,我在页面中有页面(正如我所提到的,它是一个选项卡式面板)。现在我想要外部选项卡的托管 bean 中的值 –

采纳答案by BalusC

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

首先解释为什么您尝试的选项不起作用:

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

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

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

如果参数存在,则选项 3 应该有效。但这不是一个很好的 JSF-ish 方式。

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

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

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.

选项 5 是无效的,因为它根本没有意义。您只是在中间添加了一个额外的抽象层,实际上这里并没有改变任何东西。



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

JSF 风格的方法是使用ExternalContext#getRequestParameterMap()

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

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 UINamingContainercomponents 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 thatname instead as parameter name.

至于为什么选项 3 不起作用可能是因为参数名称不是您认为/期望的那样。JSF 即根据UINamingContainer视图树中的父组件预先添加(woodstocks)客户端 ID 。在 Web 浏览器中右键单击 JSF 页面生成的 HTML 输出并选择View Source。检查生成<input>的感兴趣元素的名称。使用名称作为参数名称。



That said, this is after all a workaround not a solution. But we can't reasonably suggest the realsolution 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 thatbacking bean from inside the backing bean and then in turn access the input value bound with it. See Injecting Managed Beans in each otherfor an how to.

也就是说,这毕竟是一种解决方法而不是解决方案。但是我们不能合理地提出真正的解决方案,因为功能要求和您拥有的代码并不完全清楚。您通常将输入元素绑定到支持 bean。如果您在不同的后台bean里面实际上是,那么你也可以只访问从后台bean内支持bean,然后依次访问的输入值与它的约束。有关如何操作,请参阅在彼此中注入托管 Bean

回答by ewernli

I don't understand exactly your question. If the inputtextor radiobuttonis correctly bound to a value in a backing bean, then you can access the value directly in Java. If what you are interested in is the programmatic lookup of a managed bean, then that's how I do it:

我不完全明白你的问题。如果inputtextorradiobutton正确绑定到支持 bean 中的值,那么您可以直接在 Java 中访问该值。如果您感兴趣的是托管 bean 的编程查找,那么我就是这样做的:

FacesContext facesContext = FacesContext.getCurrentInstance();
MyBean currentProperty = (MyBean) facesContext.getELContext().getELResolver().getValue(facesContext.getELContext(), null, "nameOfTheBackingBean");

回答by McDowell

The fact that your controls are inside IBM ODC panels is not relevant.

您的控件位于 IBM ODC 面板内这一事实与此无关。

Usually, you would bind the input control to a managed bean value.

通常,您会将输入控件绑定到托管 bean 值。

Bean definition:

豆定义:

<managed-bean>
  <managed-bean-name>demo</managed-bean-name>
  <managed-bean-class>foo.MyManagedBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

View tags:

查看标签:

<!-- needs to be inside a form control -->
<h:inputText value="#{demo.foo}" />
<h:commandButton value="Click Me" action="#{demo.invokeMe}" />

Bean:

豆角,扁豆:

package foo;
public class MyManagedBean {
  private String foo;
  public String getFoo() { return foo; }
  public void setFoo(String foo) { this.foo = foo; }

  public String invokeMe() {
    System.out.println(foo);
    return null; //no navigation
  }
}

If you wanted to bind your input control to a different bean to the one with the application logic, you can use property injection to refer to the other bean.

如果您想将输入控件绑定到具有应用程序逻辑的不同 bean,您可以使用属性注入来引用另一个 bean。

<managed-bean>
  <managed-bean-name>demo</managed-bean-name>
  <managed-bean-class>foo.MyManagedBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
    <property-name>propName</property-name>
    <value>#{someExpression}</value>
  </managed-property>
</managed-bean>

You can look stuff up directly via the context, or use the expression classes to resolve stuff via code, but this is a cleaner approach.

您可以直接通过上下文查找内容,或使用表达式类通过代码解析内容,但这是一种更简洁的方法。