jsf传递参数
来源:互联网 发布:淘宝淘你喜欢 编辑:程序博客网 时间:2024/06/09 17:05
JSF参数传递方式之一:f:param标签
页面到Bean的参数传递
页面中设置参数:
Java代码
- <h:form>
-
<h:commandLink value="Test2" action="#{paramBean.test}"> -
<f:param name="name" value="zhang"></f:param> -
<f:param name="id" value="123456"></f:param> -
</h:commandLink> - </h:form>
<h:form>
</h:form>
注意:这里只能使用h:commandLink标签,而不能使用h:commandButton标签!
后台取参数:
(1) 通过Request对象取值
Java代码
- HttpServletRequest
request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest(); -
request.getParameter("name");
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
Java代码
- Map
varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap(); -
varMap.get("id");
Map varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
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>
<managed-bean>
</managed-bean>
页面到页面的参数传递
页面中设置参数:
(1)
Java代码
- <h:outputLink
value="param2.jsf"> -
<h:outputText value="Test4"></h:outputText> -
<f:param name="name" value="chen"></f:param> -
<f:param name="id" value="123456"></f:param> -
</h:outputLink>
<h:outputLink value="param2.jsf">
(2)
Java代码
- <h:outputLink
value="param2.jsf?name=chen&id=123456"> -
<h:outputText value="Test4"></h:outputText> -
</h:outputLink>
<h:outputLink value="param2.jsf?name=chen&id=123456">
注意:以上两种方法,不能同时使用!
页面中取参数:
(1) 使用JSF的值表达式
Java代码
- <h:outputText
value="#{param.name}"></h:outputText> -
<h:outputText value="#{param.id}"></h:outputText>
<h:outputText value="#{param.name}"></h:outputText>
(2) 使用JSP的表达式
Java代码
- <%=request.getParameter("name")%>
-
<%=request.getParameter("id")%>
JSF参数传递方式之二:Backing Bean 与 h:inputHidden标签
Backing Bean
Java代码
- import
javax.faces.component.UIInput; - import
javax.faces.component.UIOutput; -
- public
class BackingBean - {
-
-
private UIOutput idComponent; -
-
public UIOutput getIdComponent() -
{ -
return idComponent; -
} -
-
public void setIdComponent(UIOutput idComponent) -
{ -
this.idComponent = idComponent; -
} -
-
- }
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
public class BackingBean
{
}
页面到Bean的参数传递
页面中设置参数:
Java代码
- <h:form>
-
<h:inputHidden value="123456" binding="#{backingBean.idComponent}"></h:inputHidden> -
<h:commandButton value="登录" action="#{paramBean.login}"></h:commandButton> -
</h:form>
<h:form>
后台取参数:
Java代码
- FacesContext
context = FacesContext.getCurrentInstance(); -
BackingBean backBean =(BackingBean)context.getApplication().getVariableResolver().resolveVariable(context,"backingBean");//该方法已经过时 -
BackingBean bean =(BackingBean)context.getApplication().getELResolver().getValue(context.getELContext(), null, "backingBean"); -
backBean.getIdComponent().getValue(); -
bean.getIdComponent().getValue();
FacesContext context = FacesContext.getCurrentInstance();
2.2页面到页面的参数传递
页面中设置参数:
Java代码
- <h:form>
-
<h:inputHidden value="123456" binding="#{backingBean.idComponent}"></h:inputHidden> -
<h:commandButton value="Test5" action="param"></h:commandButton> -
<h:commandLink value="Test6" action="param"></h:commandLink> -
</h:form>
<h:form>
注意:h:outputLink 标签不能使用该方式传递参数!
页面中取参数:
Java代码
- <h:outputText
value="#{backingBean.idComponent.value}"></h:outputText>
JSF参数传递方式之三:通过session(application)对象传递
页面到Bean的参数传递
页面中设置参数:
Java代码
- <h:form>
-
<%session.setAttribute("name","hujilie"); %> -
<%application.setAttribute("id","123456"); %> -
<h:commandButton value="Test8" action="#{paramBean.test2}"></h:commandButton> -
<h:commandLink value="Test8" action="#{paramBean.test2}"></h:commandLink> -
</h:form>
<h:form>
后台取参数:
Java代码
- FacesContext
context = FacesContext.getCurrentInstance(); -
Map sessionMap =context.getExternalContext().getSessionMap(); -
Map applicationMap = context.getExternalContext().getApplicationMap(); - HttpSession
session =(HttpSession) context.getExternalContext().getSession(true); - ServletContext
application = (ServletContext)context.getExternalContext().getContext(); -
sessionMap.get("name"); -
applicationMap.get("id"); -
session.getAttribute("name"); -
application.getAttribute("id");
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session =(HttpSession) context.getExternalContext().getSession(true);
ServletContext application = (ServletContext)context.getExternalContext().getContext();
页面到页面的参数传递
页面中设置参数:
Java代码
- <h:form>
-
<%session.setAttribute("name","hujilie"); %> -
<%application.setAttribute("id","123456"); %> -
<h:outputLink value="param2.jsf">Test10</h:outputLink> -
</h:form>
<h:form>
页面中取参数:
Java代码
- <h:outputText
value="#{sessionScope.name}"></h:outputText><br> -
<h:outputText value="#{applicationScope.id}"></h:outputText>
JSF参数传递方式之四:f:attribute标签传递
页面到Bean的参数传递
页面中设置参数:
Java代码
- <h:form>
-
<h:commandButton action="#{paramBean.test3}" value="Test11" actionListener="#{paramBean.changeName}"> -
<f:attribute name="name" value="hujilie"/> -
</h:commandButton> -
<h:commandLink action="#{paramBean.test3}" value="Test12" actionListener="#{paramBean.changeName}"> -
<f:attribute name="name" value="hujilie"/> -
</h:commandLink> -
</h:form>
<h:form>
后台取参数:
Java代码
- public
void changeName(ActionEvent e) -
{ -
UIComponent component = e.getComponent(); -
Map<String, Object> map = component.getAttributes(); -
setName((String)map.get("name")); -
}
JSF参数传递方式之五:f:setPropertyActionListene
页面到Bean的参数传递
页面中设置参数:
Java代码
- <h:form>
-
<h:commandButton action="#{paramBean.test3}" value="Test14"> -
<f:setPropertyActionListener value="hujilie" target="#{paramBean.name}"/> -
</h:commandButton> -
<h:commandLink action="#{paramBean.test3}" value="Test15"> -
<f:setPropertyActionListener value="hujilie" target="#{paramBean.name}"/> -
</h:commandLink> -
</h:form>
<h:form>
后台取参数:直接使用属性的值。
文章出处:http://pity1115.javaeye.com/category/56321
- JSF request参数传递
- jsf传递参数
- JSF request参数传递
- JSF参数传递方式
- JSF的参数传递
- JSF参数传递方式说明
- JSF中传递参数的问题
- jsf页面之间中文参数传递设置
- 通过URL传递参数到JSF
- jsf页面参数的传递及加载数据
- JSF点滴积累--页面间参数的传递
- jsf页面参数的传递及加载数据
- JSF---通过URL往backing bean传递参数
- jsf页面参数的传递及加载数据
- JSF中的h:commandLink如何传递参数 三种取值比较
- JSF页面间参数的传递及加载初始化数据
- JSF中的h:commandLink如何传递参数 三种取值比较
- JSF(PrimeFaces)由后台向前台传递参数
- 读写注册表
- 浅论中国当下情景喜剧发展
- c/c++ scanf printf 用法与优化
- Spring MVC学习之五:注解用法总结
- Automake 使用
- jsf传递参数
- Postgresql默认用户名与密码
- 读取文件大小 获取文件长度
- ibatis 取值以及sql注入
- 使用junit测试ssh中的dao
- Android开发中string.xml文件的使用
- oracle 触发器中修改基表解决办法
- Delphi中的回调函数
- 读取别的apk内部全局文件的问题