您的位置:首页 > Web前端 > JavaScript

JSF参数传递方式之f:param标签和f:attribute区别

2015-10-13 09:46 363 查看

页面到Bean的参数传递页面中设置参数:Java代码<h:form><h:commandLinkvalue="Test2"action="#{paramBean.test}"><f:paramname="name"value="zhang"></f:param><f:paramname="id"value="123456"></f:param></h:commandLink></h:form>
注意:这里只能使用h:commandLink标签,而不能使用h:commandButton标签!后台取参数:(1)通过Request对象取值Java代码HttpServletRequestrequest=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();request.getParameter("name");
(2)通过RequestParameterMap取值Java代码MapvarMap=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();varMap.get("id");
(3)通过配置文件进行Bean的属性值注入,在Bean的方法中直接使用属性Java代码<managed-bean><managed-bean-name>paramBean</managed-bean-name><managed-bean-class>com.spg.bean.ParamBean</managed-bean-class><managed-bean-scope>request</managed-bean-scope><managed-property><property-name>id</property-name><property-class>java.lang.String</property-class><value>#{param.id}</value></managed-property></managed-bean>
页面到页面的参数传递页面中设置参数:(1)Java代码<h:outputLinkvalue="param2.jsf"><h:outputTextvalue="Test4"></h:outputText><f:paramname="name"value="chen"></f:param><f:paramname="id"value="123456"></f:param></h:outputLink>
(2)Java代码<h:outputLinkvalue="param2.jsf?name=chen&id=123456"><h:outputTextvalue="Test4"></h:outputText></h:outputLink>注意:以上两种方法,不能同时使用!页面中取参数:(1)使用JSF的值表达式Java代码<h:outputTextvalue="#{param.name}"></h:outputText><h:outputTextvalue="#{param.id}"></h:outputText>
(2)使用JSP的表达式Java代码<%=request.getParameter("name")%><%=request.getParameter("id")%>另外还见到一种方式:如下但没有实验过从JSF页面传递参数给托管Bean虽然利用h:commandLink和h:commandButton组件,可以通过action和actionListener来触发托管Bean中的方法,但是不能向这些方法中传递参数。对于动态传递参数,不是不可以实现,这点可以通过使用f:attribute来实现。而且f:attribute也可以很好的和actionListener联合使用。例子:<h:commandLinkactionListener="#{myBean.action}"><f:attributename="attrname1"value="attrvalue1"/><f:attributename="attrname2"value="attrvalue2"/>...<h:outputTextvalue="Clickhere"/></h:commandLink><h:commandButtonvalue="Presshere"actionListener="#{myBean.action}"><f:attributename="attrname1"value="attrvalue1"/><f:attributename="attrname2"value="attrvalue2"/>...</h:commandButton>这些属性可以通过父组件的getAttributes()方法取到,父组件可以通过传递给actionListener的ActionEvent实例取到publicvoidaction(ActionEventevent)...{Stringattrvalue1=(String)event.getComponent().getAttributes().get("attrname1");Stringattrvalue2=(String)event.getComponent().getAttributes().get("attrname2");...}变量attrvalue1和attrvalue2包含从f:attributeset进来的值。另一个欠文雅的方式就是通过f:param组件来传值,这个只是对h:commandLink起效。<h:commandLinkaction="#{myBean.action
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: