在 JSF 的托管 bean 中获取 GET 参数

发布于 2024-08-27 13:20:44 字数 1748 浏览 9 评论 0原文

有人可以告诉我如何捕获从 JSF 托管 bean 中的 URI 传递的参数吗? 我有一个导航菜单,其中的所有节点都链接到某些导航案例。我那里有两个类似的项目:获取产品和发行产品。它们具有相同的页面,但有一个不同的参数:productType。我尝试通过将其添加到“to-view-id”元素中的 URL 来设置它,如下所示:

<navigation-case>
    <from-outcome>acquiring|products</from-outcome>
    <to-view-id>/pages/products/list_products.jspx?productType=acquiring</to-view-id>
</navigation-case>

<navigation-case>
    <from-outcome>issuing|products</from-outcome>
    <to-view-id>/pages/products/list_products.jspx?productType=issuing</to-view-id>
</navigation-case>

但我无法从我的托管 bean 中获取此“productType”。我尝试像这样通过 FacesContext 获取它:

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("productType")

像这样:

HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    request.getParameter("productType");

我尝试将它作为 faces-config.xml 中托管 bean 的参数包含在内,然后通过普通设置器获取它:

 <managed-bean>
        <managed-bean-name>MbProducts</managed-bean-name>
        <managed-bean-class>my.package.product.MbProducts</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
     <managed-property>
         <property-name>productType</property-name>
         <value>#{param.productType}</value>
     </managed-property>
    </managed-bean>
...
public class MbProducts {
...
 public void setProductType(String productType) {
  this.productType = productType;
 }
...
}

但是这些方法都没有帮助我。他们全部返回 null。我怎样才能得到这个产品类型?或者我怎样才能通过其他方式传递它?

Can someone tell me how to catch parameters passed from URI in JSF's managed bean?
I have a navigation menu all nodes of which link to some navigation case. And i have two similar items there: Acquiring products and Issuing products. They have the same page but one different parameter: productType. I try to set it just by adding it to URL in "to-view-id" element like this:

<navigation-case>
    <from-outcome>acquiring|products</from-outcome>
    <to-view-id>/pages/products/list_products.jspx?productType=acquiring</to-view-id>
</navigation-case>

<navigation-case>
    <from-outcome>issuing|products</from-outcome>
    <to-view-id>/pages/products/list_products.jspx?productType=issuing</to-view-id>
</navigation-case>

But i can't get this "productType" from my managed bean. I tried to get it through FacesContext like this:

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("productType")

And like this:

HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    request.getParameter("productType");

And i tried to include it as a parameter of managed bean in faces-config.xml and then getting it through ordinary setter:

 <managed-bean>
        <managed-bean-name>MbProducts</managed-bean-name>
        <managed-bean-class>my.package.product.MbProducts</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
     <managed-property>
         <property-name>productType</property-name>
         <value>#{param.productType}</value>
     </managed-property>
    </managed-bean>
...
public class MbProducts {
...
 public void setProductType(String productType) {
  this.productType = productType;
 }
...
}

But neither of these ways have helped me. All of them returned null. How can i get this productType? Or how can i pass it some other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卸妝后依然美 2024-09-03 13:20:45

导航规则默认执行前进。即它重用初始请求。无论您尝试以何种方式访问​​转发资源中的请求参数,它总是会尝试从初始和已处理的请求中获取它们。

要解决此问题,您需要触发重定向而不是转发。它创建一个全新的请求(您也可以在浏览器地址栏中看到这反映)。

在 JSF 中,添加

<redirect/>

到导航案例就可以了。

The navigation rule by default does a forward. I.e. it reuses the initial request. Whatever way you try to access the request parameters in the forwarded resource, it will always try to grab them from the initial and already-processed request.

To fix this, you need to fire a redirect instead of forward. It creates a brand new request (you also see this reflecting back in the browser address bar).

In JSF, adding

<redirect/>

to the navigation case should do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文