JSF教程 – JSF参数示例
我们可以使用f:param标签将参数传递给组件或传递请求参数。
以下代码显示如何将参数传递到UI组件。
<h:outputFormat value="Hello {0}!.">
<f:param value="World" />
</h:outputFormat>
以下代码传递请求参数及其名称。
<h:commandButton id="submit"
value="Show Message" action="#{userData.showResult}">
<f:param name="username" value="JSF 2.0 User" />
</h:commandButton>
标签属性
| 属性 | 描述 |
|---|---|
| id | 组件的标识符 |
| binding | 引用可以在backing bean中使用的组件 |
| name | 参数组件的可选名称 |
| value | 存储在组件中的值 |
例子
以下代码来自demo.xhtml。
<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:body>
<h:form id="form">
Enter your name :
<h:inputText size="10" value="#{user.name}" />
<br /><br />
<h:commandButton id="submitButton"
value="Submit - US" action="#{user.outcome}">
<f:param name="country" value="United States" />
</h:commandButton>
</h:form>
</h:body>
</html>
以下代码来自result.xhtml。
<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:body>
<h3>
<h:outputFormat value="Hello,{0}. You are from {1}.">
<f:param value="#{user.name}" />
<f:param value="#{user.country}" />
</h:outputFormat>
</h3>
</h:body>
</html>
下面的代码来自UserBean.java。
package cn.w3cschool.common;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String name;
public String country;
public String outcome(){
FacesContext fc = FacesContext.getCurrentInstance();
this.country = getCountryParam(fc);
return "result";
}
//get value from "f:param"
public String getCountryParam(FacesContext fc){
Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
return params.get("country");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
下载 Param.zip
运行
将生成的WAR文件从目标文件夹复制到Tomcat部署文件夹,并运行Tomcat-Install-folder / bin / startup.bat。
Tomcat完成启动后,在浏览器地址栏中键入以下URL。
http://localhost:8080/simple-webapp/demo.xhtml

国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码













