我试图通过url传递参数..并返回0.
这是ManagedBean ...一个带有两个变量的简单bean。
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class UserBean {
@ManagedProperty("#{param.id}")
private int id;
private String name;
public int getId() {
System.out.println(id);
return id;
}
public void setId(int id) {
System.out.println(id);
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public UserBean() {
super();
}
public String createUrl()
{
return "yoga.xhtml?id=267";
}
}
以下是传递参数的xhtml页面...我使用commandButton重定向到带有查询参数的页面
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<head>
</head>
<h:body>
<f:metadata>
<f:viewParam name="id" value="#{userBean.id}" />
</f:metadata>
<h:form>
<h:commandButton action = "yoga.xhtml?id=345" value = "click here to pass the paramter">
</h:commandButton>
</h:form>
</h:body>
</html>
这就是我重定向页面以显示传递的ID
的地方<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
Passed Parameter is
<h1>#{userBean.id}</h1>
</h:body>
</html>