- 浏览: 99635 次
文章分类
最新评论
-
jXee:
lgs0626 写道求源码,兄弟给共享下吧 "jee ...
jee6 学习笔记 4 - CRUD 2: View Details, Primefaces row selection -
lgs0626:
求源码,兄弟给共享下吧
jee6 学习笔记 4 - CRUD 2: View Details, Primefaces row selection
这题目可能有点儿吓人,但却是实事求是。
我们看看JSF2有哪些方法来获取HTTP GET parameters:
1. use of new JSF tag "f:viewParam". is it ugly?
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.
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.
the page: "tst/testGetParam.xhtml"
the backing bean: "ParameterTester.java"
test screens:
1. set get request with url: http://localhost:8180/ProJee6/tst/testGetParam.jsf?s1=hi01&s2=hi02&s3=hi03
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"
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.
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
我们看看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") @RequestScoped public 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
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"
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.
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
发表评论
-
ActiveMQ and Spring JMS Framework Message Loss
2019-06-28 07:15 29Java Message Service (JMS) prov ... -
how to proxy to k8s web console
2018-06-28 07:16 564### how to access k8s web conso ... -
Call Stored Procedure with JPA 2.1
2018-06-27 10:57 660JPA 2.1 introduces APIs to call ... -
Send response and then process - async processing
2017-10-12 09:35 555If your request processing take ... -
java 8 time api test
2017-08-29 05:40 477public class ParseUtcDateTime ... -
Setup ApiKey in header with Swagger generated client code
2017-08-23 06:41 472@Value("${api.base.path} ... -
Simple tool to monitor jvm memory usage and garbage collection
2016-10-13 06:06 359JDK has built-in tool to moni ... -
Externalize Application Config properties with JBoss 7.1
2017-06-02 12:09 335If you have configuration pro ... -
JPA native query does not support setting list parameters
2014-03-27 06:45 1011you might want to do the ... -
Owning Side and Inverse Side of JPA entity relationships
2013-09-10 07:08 806Entity relationships may be b ... -
avoid setParameter for "order by" in JPQL
2013-03-07 05:55 777you might want to create a JP ... -
JPA Path Expression, operator IN and Collection properties
2013-01-23 16:25 1394If we want to select the Orde ... -
与JEE6/EJB3.1相比, Spring framework 丧失了几乎所有的优势
2013-01-19 13:13 1035The Spring framework was a ma ... -
Simple EasyMock tutorial
2012-12-20 11:57 672http://veerasundar.com/blog/20 ... -
Servlet 3.0 @WebFilter and @WebServlet
2012-12-04 07:09 2677Servlet 3.0 provides new annota ... -
Why JSF2 @ViewScoped not working?
2012-12-03 06:55 1368javax.faces.bean.ViewScoped sai ... -
When to configure an XA datasource?
2012-11-16 12:58 1264If you ever came across this wa ... -
java ee transaction and datasource concepts
2012-11-10 13:48 10441. What is a transaction? A tra ... -
pass params to primefaces confirmation dialog box
2012-09-28 19:30 1335<p:dataTable id="idStuD ... -
Handle Big Dataset with Real Pagination with Primefaces 3.3 LazyDataModel
2012-09-21 13:41 5619If you have millions of record ...
相关推荐
开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32...
eclipse-jee-mars-1-win32-x86_64.7z eclipse-jee-mars-1-win32-x86_64.zip 我打的 7z 压缩包 关于有 Alt + / 不起作用解决办法: window -> General -> Keys -> Content Assist -> Binding: 改为 Alt + / When:...
eclipse-jee-mars-2-win32 javaee开发工具 eclipse-jee-mars-2-win32 javaee开发工具
eclipse-jee-ganymede-SR2-win32.zip
标题 "eclipse-jee-juno-SR2-linux-gtk-x86_64.tar.gz" 指示的是一个特定版本的Eclipse集成开发环境(IDE)针对Java企业版(Java Enterprise Edition,简称JEE)的发行包,适用于Linux操作系统,并且是64位(x86_64...
eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64
在Java企业版(Java EE)6中,`@Asynchronous`注解是一个非常重要的特性,它使得开发者可以方便地在应用程序中实现异步处理。这个注解是Java EE并发编程的一部分,主要应用于EJB(Enterprise JavaBeans)环境,用于...
"eclipse-jee-2023-09-R-linux-gtk-x86_64.tar.gz" 文件是Eclipse专为Java企业版(Java EE)开发者设计的2023年9月版本,适用于64位的Linux操作系统。这个版本包含了对Java EE开发所需的全部工具和功能,如Web服务器...
eclipse-jee-luna-SR2-win32-x86_64网盘下载地址(官网厂货); 绿色纯净版,解压缩方可运行(亲测可行)
在解压eclipse-jee-2022-06-R-win32-x86_64.zip后,我们会得到一个名为“eclipse”的文件夹,这个文件夹包含了整个IDE的所有组件和配置。启动Eclipse IDE,用户会看到熟悉的界面,包括工作区(Workspace)、透视图...
标题中的“eclipse-jee-indigo-SR2-win32”指的是Eclipse IDE的一个特定版本,它是专为Java EE(企业级Java)开发者设计的。Eclipse是一个开源的集成开发环境(IDE),广泛用于Java编程,它提供了丰富的工具集来支持...
eclipse-jee-neon-1a-win_64
大家下载eclipse-jee-kepler-SR2-win32.zip解压后运行,发现不能直接从SVN中导入项目,下面是我自己想到的方法: 方法:找到自己使用过的带有svn的,低版本的eclipse,找到features和plugins目录,将其中的带有“org...
在Eclipse JEE版本中,这些功能得到了进一步增强,特别是对于Java EE应用程序的开发,如Web服务、Java服务器页面(JSP)、JavaServer Faces(JSF)以及Enterprise JavaBeans(EJB)等,提供了全面的工具集和模板。...
标题 "eclipse-jee-2018-09-win32-x86_64.zip" 提供的信息表明这是一款针对Java企业级开发的Eclipse集成开发环境(IDE)的2018年9月版本,适用于Windows 32位操作系统、x86_64架构的计算机。Eclipse是一个开源的、跨...
eclipse-jee-juno-SR2-win32-x86_64, 百度云盘下载!
2. `eclipse-jee-2020-09-R-win32-x86_64.zip`:这个文件名再次确认了压缩包的内容,即Eclipse IDE的可执行安装程序。通常,用户会解压这个文件,然后运行其中的可执行文件来安装Eclipse。 Eclipse IDE for Java EE...
Eclipse64位3.6.2太阳神版eclipse-jee-helios-SR2-win32-x86_64.zip支持jdk1.5 Eclipse 支持jdk1.5 64位 helios 太阳神版 eclipse-jee-helios-SR2-win32-x86_64.zip 更多eclipse版本可看查看我的系列,欢迎下载~
开始使用Eclipse JEE 2022-09 R,首先需要下载"eclipse-jee-2022-09-R-win32-x86_64.zip"压缩包,解压后运行“eclipse.exe”。初次启动,用户需要配置工作空间,选择Java开发工具,以及根据项目需求添加服务器...