这题目可能有点儿吓人,但却是实事求是。

我们看看JSF2有哪些方法来获取HTTP GET parameters:

1. use of new JSF tag "f:viewParam". is it ugly?



    //in the page:    <f:metadata>          <f:viewParam name="s1" value="#{pt.s1}" />      </f:metadata>  

    //in the bean:    private String s1;    // s1 getter/setter

2. use @ManagedProperty(value="#{param.s2}"). an EL in backing bean? what's the point? The backing bean must be @RequestScoped, which is the default scope. For a post-redirect, test showed that you must append the "faces-redirect=true" to the GET url, as part of the query string. otherwise it's just not working.



@ManagedProperty(value="#{param.s2}")private String s2;// s2 getter/setter

public String urActionMethod() {  return "/tst/testGetParam.xhtml?faces-redirect=true&s2=hello&s1=hi"}

3. use of tag <f:setPropertyActionListener value="val" target="#{bean.propertySetter}">.

4. get the "native" HttpServletRequest" object and get parameters by the servlet API: your final resort if all other means does not work.

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

the page: "tst/testGetParam.xhtml"

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"    xmlns:f="http://java.sun.com/jsf/core"    xmlns:h="http://java.sun.com/jsf/html"    xmlns:p="http://primefaces.org/ui">

<h:head>  <title>Test JSF Parameter Binding</title></h:head>

<h:body>

    <!-- GET: by a new JSF tag "f:viewParam" -->  <f:metadata>        <f:viewParam name="s1" value="#{pt.s1}" />    </f:metadata>

 <p:panel header="Test1: by new tag 'f:viewParam'" style="width:50%">        s1=<h:outputText value="#{pt.s1}"/> </p:panel>

    <p:spacer height="7"/>

 <!-- POST followed by a redirect with GET. Not working without redirect --> <h:form id="frm2">       <p:panel header="Test2: POST followed by a redirect GET" style="width:50%">           <p:commandButton update="out2" value="Go GET 2" action="#{pt.print2}"/>            s2=<h:outputText id="out2" value="#{pt.s2}"/>        </p:panel>    </h:form>

    <p:spacer height="7"/>

    <!-- POST with tag 'f:setPropertyActionListener' -->  <h:form id="frm3">       <p:panel header="Test3: with tag 'f:setPropertyActionListener'" style="width:50%">          <p:commandButton update="out3" value="Go POST" actionListener="#{pt.print3}">              <f:setPropertyActionListener value="hello world III!" target="#{pt.s3}"/>             </p:commandButton>          s3=<h:outputText id="out3" value="#{pt.s3}"/>        </p:panel>    </h:form></h:body></html>

the backing bean: "ParameterTester.java"

package com.jxee.action.getparam;

import java.io.Serializable;

import javax.annotation.PostConstruct;import javax.faces.bean.ManagedBean;import javax.faces.bean.ManagedProperty;import javax.faces.bean.RequestScoped;import javax.faces.context.FacesContext;import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

/** * backing bean to test JSF2 parameter binding */@ManagedBean(name="pt")@RequestScopedpublic class ParamTester implements Serializable {

  private static final Logger log = Logger.getLogger(ParamTester.class);

  private String s1;

  @ManagedProperty(value="#{param.s2}")  private String s2;

  private String s3;

  private HttpServletRequest request;

  @PostConstruct  public void init() {    log.debug(String.format(">>> PostConstruct: s1=%s, s2=%s, s3=%s", s1, s2, s3));    request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();    this.printRequestParams();  }

  private void printRequestParams() {    log.debug("--- printing 3 params in http request:");    String ps1 = request.getParameter("s1");    String ps2 = request.getParameter("s2");    String ps3 = request.getParameter("s3");    log.debug(String.format("--- Http Request params: ps1=%s,ps2=%s,ps3=%s ---", ps1,ps2,ps3));  }

  public String print2() {    log.debug(">>> print2 called, do redirect with url query string");    this.printRequestParams();    return "/tst/testGetParam.xhtml?faces-redirect=true&s2=hello john!&s1=hi s1 again!";  }

  public String print3() {    log.debug(">>> print3 called, setPropertyActionListener");    this.printRequestParams();    return null;  }

  public String getS1() {    return s1;  }

  public void setS1(String s1) {    this.s1 = s1;  }

  public String getS2() {    return s2;  }

  public void setS2(String s2) {    this.s2 = s2;  }

  public String getS3() {    return s3;  }

  public void setS3(String s3) {    this.s3 = s3;  }}

test screens:

1. set get request with url: http://localhost:8180/ProJee6/tst/testGetParam.jsf?s1=hi01&s2=hi02&s3=hi03

[img]http://dl.iteye.com/upload/attachment/0070/9236/49831985-c9eb-3fa8-9164-85c905173d7f.png[/img]

server log:
2012-07-18 12:13:52,003 DEBUG [ParamTester] >>> PostConstruct: s1=null, s2=hi02, s3=null
2012-07-18 12:13:52,018 DEBUG [ParamTester] --- printing 3 params in http request:
2012-07-18 12:13:52,018 DEBUG [ParamTester] --- Http Request params: ps1=hi01,ps2=hi02,ps3=hi03 ---

2. clicked button "GET 2"

[img]http://dl.iteye.com/upload/attachment/0070/9238/a1891233-6d79-33b5-9be0-0d8c1e170b78.png[/img]

server log:
2012-07-18 12:16:59,141 DEBUG [ParamTester] >>> PostConstruct: s1=null, s2=null, s3=null
2012-07-18 12:16:59,141 DEBUG [ParamTester] --- printing 3 params in http request:
2012-07-18 12:16:59,141 DEBUG [ParamTester] --- Http Request params: ps1=null,ps2=null,ps3=null ---
2012-07-18 12:16:59,141 DEBUG [ParamTester] >>> print2 called, do redirect with url query string
2012-07-18 12:16:59,141 DEBUG [ParamTester] --- printing 3 params in http request:
2012-07-18 12:16:59,141 DEBUG [ParamTester] --- Http Request params: ps1=null,ps2=null,ps3=null ---
2012-07-18 12:16:59,141 DEBUG [ParamTester] >>> PostConstruct: s1=null, s2=hello john!, s3=null
2012-07-18 12:16:59,141 DEBUG [ParamTester] --- printing 3 params in http request:
2012-07-18 12:16:59,157 DEBUG [ParamTester] --- Http Request params: ps1=hi s1 again!,ps2=hello john!,ps3=null ---

3. clicked button "GO POST"
one thing to mention: after the form submitted, "s2" still has the same value. this is because that it used Primefaces <p:commandButton> instead of JSF <h:commandButton/>. Primefaces <p:commandButton/> uses ajax as default method to submit the form. This can be disabled by adding attribute ajax="false": <p:commandButton value="Go POST" action="#{pt.print3}" ajax="false">. Now after "Go POST" clicked, the page re-rendered and "s2" should be empty.

[img]http://dl.iteye.com/upload/attachment/0070/9240/a021cb2d-dd63-3957-b500-e6b2f367a413.png[/img]

server log:
2012-07-18 12:20:15,217 DEBUG [ParamTester] >>> PostConstruct: s1=null, s2=null, s3=null
2012-07-18 12:20:15,217 DEBUG [ParamTester] --- printing 3 params in http request:
2012-07-18 12:20:15,217 DEBUG [ParamTester] --- Http Request params: ps1=null,ps2=null,ps3=null ---
2012-07-18 12:20:15,217 DEBUG [ParamTester] >>> print3 called, setPropertyActionListener
2012-07-18 12:20:15,217 DEBUG [ParamTester] --- printing 3 params in http request:
2012-07-18 12:20:15,217 DEBUG [ParamTester] --- Http Request params: ps1=null,ps2=null,ps3=null ---

结论:

也许是本人,maybe it's JSF2.0, that is struggling with GET parameters. it seems to me that @ManagedPropery requires @RequesetScoped might be reasonable but this limitation makes the @ManagedProperty virtually useless in this case.

the only use full stuff JSF provides in this use case is the tag <f:setPropertyActionListener value="val" target="#{bean.property}">.

JSF should provide something like SEAM2 tag @RequestParameter("pname"), so that the GET parameter can be injected to the backing bean directly. forget the @ManagedProperty here: the value is in the HttpServletRequest and the framework should be able to bind the request parameter by a simple annotation, without anything else required. no more drama!

It's such a common simple task to bind GET parameters to backing beans. but JSF2 tried lots of stuff and just failed the test.

Next i'd explore some features of EJB3.1 and then come back to Primefaces, such as adding menu, using template and etc.

The zipped project so far: ProJee6-phase1.zip

jee6 学习笔记 5 - Struggling with JSF2 binding GET params相关推荐

  1. A (Zero-Knowledge) Vector Commitment with Sum Binding and its Applications学习笔记

    1. 引言 Qiang Wang等人2019年发表于Oxford University Press on behalf of the Institute of Mathematics and its ...

  2. SilverLight学习笔记--Silverlight之数据绑定初探

    数据绑定(Data Binding)是用户界面UI和业务对象或其它数据提供者(data provider)的连接.用户界面对象称为目标,数据提供者成为数据源.   数据绑定帮助隔离应用程序的用户界面层 ...

  3. go 变量在其中一个函数中赋值 另一个函数_go 学习笔记之仅仅需要一个示例就能讲清楚什么闭包...

    本篇文章是 Go 语言学习笔记之函数式编程系列文章的第二篇,上一篇介绍了函数基础,这一篇文章重点介绍函数的重要应用之一: 闭包 空谈误国,实干兴邦,以具体代码示例为基础讲解什么是闭包以及为什么需要闭包 ...

  4. Windows phone 8 学习笔记(8) 定位地图导航

    Windows phone 8 学习笔记(8) 定位地图导航 原文:Windows phone 8 学习笔记(8) 定位地图导航 Windows phone 8 已经不使用自家的bing地图,新地图控 ...

  5. Web Service学习笔记

    Web Service概述 Web Service的定义 W3C组织对其的定义例如以下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的A ...

  6. 1.5 Hello, world! 解剖 -JSF实战 -hxzon -jsf学习笔记

    为什么80%的码农都做不了架构师?>>>    1.5 Hello, world! 解剖 -JSF实战 -hxzon -jsf学习笔记 既然已经对JSF能够解决什么问题有了初步理解, ...

  7. Windows phone 8 学习笔记(5) 图块与通知

    基于metro风格的Windows phone 8 应用提到了图块的概念,它就是指启动菜单中的快速启动图标.一般一个应用必须有一个默认图块,还可以有若干个次要图块.另外,通知与图块的关系比较密切,我们 ...

  8. 数据库LINQ TO SQL在Silverlight中的应用(WCF)------学习笔记(一)

    数据库LINQ TO SQL在Silverlight中的应用(WCF)------学习笔记(一) 步骤: 1. 创建SILVERLIGHT应用程序 2. 创建LINQ TO SQL [注意序列化的问题 ...

  9. SilverLight学习笔记--如何在xaml文件中操作用户在后台代码定义的类(2)--示例篇:创建一个登录控件(原创)(转载本文请注明出处)...

    本文将示例如何运用前篇所写知识来建立一个用户自定义的登录控件.此控件界面非常简单,主要涉及的知识点是:   如何创建用户控件(包括对此控件的自定义事件和属性的编写,此处我们将创建一个名为LoginBo ...

最新文章

  1. pigcms 标签读不出
  2. kuka机器人if逻辑编程_【视频】说说工业机器人控制与PLC通讯
  3. matlab灰色关联代码,灰色关联分析matlab代码
  4. 让JavaScript回归函数式编程的本质
  5. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权
  6. HDOJ/HDU 2555 人人都能参加第30届校田径运动会了(判断加排序~)
  7. OC和Swift混合编程引用Pods管理的模块
  8. java线程中yield(),sleep(),wait()区别详解
  9. html做一个年份月份天数选择器,jquery编写日期选择器
  10. 管理信息系统开发项目管理
  11. 说下我自己对空号检测的理解跟心得
  12. Python cx_Oracle执行的sql字符串拼接含分号导致报“ORA-01756“引号内的字符串没有正确结束
  13. 高一计算机课程教案,高一信息技术《信息及其特征》教案
  14. GRE填空词汇专项训练
  15. ov5640帧率配置_OV5640(2):配置寄存器
  16. 8个接私活的网站,只要你有码,那“我”就有钱
  17. 东方财富 自动止损程序
  18. Excel实现一个基础的蒙特卡洛模拟
  19. https证书服务器怎么完成部署?
  20. 职业年金是发放到养老退休金里面吗?

热门文章

  1. 第一次软工作业(构建之法)
  2. Proxy Server源码及分析(TCP Proxy源码 Socket实现端口映射)
  3. Android 系统应用开发实战
  4. 关于SpringMVC中使用LocalDateTime类型接收参数提示类型不匹配的问题
  5. 计划任务linux每天执行一次,linux 每天执行任务计划
  6. android系统profile文件路径,Android Profile Tools 入门
  7. 单链表指定结点的前插与后插(C/C++)
  8. 第一章,实现数据完整性
  9. Web(牛腩)概念知识总结
  10. java zone_offset_java 的 ZoneOffset 与 ZoneId