---
title: "JSF参数传递方式"
author: "fojian"
date: "2009-07-04T19:25:15Z"
tags: ["Bean", "F#", "JSF", "JSP"]
slug: "jsf"
draft: false
lastmod: "2009-07-04T19:25:15Z"
id: 420210
url: "https://fojian.iteye.com/blog/420210"
---

JSF参数传递方式：

1. &lt;f:param name=" " value=""&gt;&lt;/f:param&gt;

注意：该标签只能传递String类型的参数，如果value的值设为一个对象，则会调用该类的toString()方法获得一个String类型的值作为参数。

1.1. 页面到Bean的参数传递

页面中设置参数：

&lt;h:form&gt;

&lt;h:commandLink value="Test2" action="#{paramBean.test}"&gt;

&lt;f:param name="name" value="zhang"&gt;&lt;/f:param&gt;

&lt;f:param name="id" value="123456"&gt;&lt;/f:param&gt;

&lt;/h:commandLink&gt;

&lt;/h:form&gt;

注意:这里只能使用h:commandLink标签，而不能使用h:commandButton标签!

后台取参数：

(1) 通过Request对象取值

        HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

        request.getParameter("name");

(2)通过RequestParameterMap取值

        Map varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

        varMap.get("id");

(3)通过配置文件进行Bean的属性值注入，在Bean的方法中直接使用属性

&lt;managed-bean&gt;

  &lt;managed-bean-name&gt;paramBean&lt;/managed-bean-name&gt;

  &lt;managed-bean-class&gt;com.spg.bean.ParamBean&lt;/managed-bean-class&gt;

  &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;

  &lt;managed-property&gt;

   &lt;property-name&gt;id&lt;/property-name&gt;

   &lt;property-class&gt;java.lang.String&lt;/property-class&gt;

   &lt;value&gt;#{param.id} &lt;/value&gt;

  &lt;/managed-property&gt;

&lt;/managed-bean&gt;

 页面到页面的参数传递

页面中设置参数：

(1)

&lt;h:outputLink value="param2.jsf"&gt;

&lt;h:outputText value="Test4"&gt;&lt;/h:outputText&gt;

&lt;f:param name="name" value="chen"&gt;&lt;/f:param&gt;

&lt;f:param name="id" value="123456"&gt;&lt;/f:param&gt;

&lt;/h:outputLink&gt;

(2)

&lt;h:outputLink value="param2.jsf?name=chen&id=123456"&gt;

&lt;h:outputText value="Test4"&gt;&lt;/h:outputText&gt;

&lt;/h:outputLink&gt;

注意：以上两种方法，不能同时使用!

页面中取参数：

(1) 使用JSF的值表达式

&lt;h:outputText value="#{param.name}"&gt;&lt;/h:outputText&gt;

&lt;h:outputText value="#{param.id}"&gt;&lt;/h:outputText&gt;

(2) 使用JSP的表达式

&lt;%=request.getParameter("name")%&gt;

&lt;%=request.getParameter("id")%&gt;

2. Backing Bean 与 h:inputHidden

2.1页面到Bean的参数传递

页面中设置参数：

&lt;h:form&gt;

&lt;h:inputHidden value="123456" binding="#{backingBean.idComponent}"&gt;&lt;/h:inputHidden&gt;

&lt;h:commandButton value="登录" action="#{paramBean.login}"&gt;&lt;/h:commandButton&gt;

&lt;/h:form&gt;

后台取参数：

        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();

2.2页面到页面的参数传递

页面中设置参数：

&lt;h:form&gt;

&lt;h:inputHidden value="123456"  binding="#{backingBean.idComponent}"&gt;&lt;/h:inputHidden&gt;

&lt;h:commandButton value="Test5"  action="param"&gt;&lt;/h:commandButton&gt;

&lt;h:commandLink value="Test6" action="param"&gt;&lt;/h:commandLink&gt;

&lt;/h:form&gt;

注意：h:outputLink 标签不能使用该方式传递参数！

页面中取参数：

&lt;h:outputText value="#{backingBean.idComponent.value}"&gt;&lt;/h:outputText&gt;

3. 通过session(application)对象传递

3.1页面到Bean的参数传递

页面中设置参数：

&lt;h:form&gt;

&lt;%session.setAttribute("name","hujilie"); %&gt;

&lt;%application.setAttribute("id","123456"); %&gt;

&lt;h:commandButton value="Test8" action="#{paramBean.test2}"&gt;&lt;/h:commandButton&gt;

&lt;h:commandLink  value="Test8" action="#{paramBean.test2}"&gt;&lt;/h:commandLink&gt;

&lt;/h:form&gt;

后台取参数：

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");

3.2页面到页面的参数传递

页面中设置参数：

&lt;h:form&gt;

&lt;%session.setAttribute("name","hujilie"); %&gt;

&lt;%application.setAttribute("id","123456"); %&gt;

&lt;h:outputLink value="param2.jsf"&gt;Test10&lt;/h:outputLink&gt;

&lt;/h:form&gt;

页面中取参数：

&lt;h:outputText value="#{sessionScope.name}"&gt;&lt;/h:outputText&gt;&lt;br&gt;

&lt;h:outputText value="#{applicationScope.id}"&gt;&lt;/h:outputText&gt;

4. f:attribute标签传递

4.1页面到Bean的参数传递

页面中设置参数：

&lt;h:form&gt;

&lt;h:commandButton action="#{paramBean.test3}" value="Test11" actionListener="#{paramBean.changeName}"&gt;

&lt;f:attribute name="name" value="hujilie"/&gt;

&lt;/h:commandButton&gt;

&lt;h:commandLink action="#{paramBean.test3}" value="Test12" actionListener="#{paramBean.changeName}"&gt;

&lt;f:attribute name="name"