jsf获取表单的两种方法

xiaoxiao2026-04-11  12

第一种: 1.如在你的页面中有 <input type= "hidden " name= "classid " value= "1 "> 注意要写在form中,并且要用name而不是id. FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get( "classid ")可以得到。 2. <h:form id= "f "> <input type= "hidden " id= "classid " value= "1 "> </h:form> FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get( "f:classid ")可以得到。 3.使用JSF自带的控件,则和2是一样的访问方法 以上方法就可以了,写id的时候,jsf会把form的id加进去 变成 formId:componentId 的格式 第二种: 就是通过findComponent方法获得控件实例,然后通过getValue方法获得值 HtmlInputText text = (HtmlInputText) this.findComponent(this.FORM + ":"+ ENERGY_COMSUPTION_PREFIX + id); String value = text.getValue().toString(); public UIComponent findComponent(String name) { // 不能使用静态变量view保持当前的viewroot,因为每次操作之后的viewroot都是不一样的 ViewRoot view = FacesContext.getCurrentInstance().getViewRoot(); return view.findComponent(name); } 需要注意的是 不能使用静态变量view保持当前的viewroot,因为每次操作之后的viewroot都是不一样的。 对比以上两种方法,第一种简单方便,但只适合于取值操作。第二种可操控的范围和能力更大,不仅可以取值还可以设置和获得所有属性。 相关资源:JAVA上百实例源码以及开源项目源代码
转载请注明原文地址: https://www.6miu.com/read-5047169.html

最新回复(0)