【发布时间】:2013-04-17 17:05:58
【问题描述】:
我有一个简单的页面,它与 @RequestScoped 支持 bean 相关联。我从传递参数“项目”的其他页面进入此页面。因此,当我进入正确的页面时,我的网址类似于 contextRoot/faces/jsf.xhtml?project=123。
查看:
<f:metadata>
<f:viewParam name="project" value="#{entityBean.projectId}" />
</f:metadata>
...
<p:commandButton value="#{msg['button.add']}"
actionListener="#{entityBean.addNewEntity((entityName),(entityDescritpion))}"
ajax="true" update=":projectDetailForm"/>
支持 bean:
@Named("entityBean")
@RequestScoped
public class EntityBean implements Serializable{
private String projectId;
@PostConstruct
public void init() {
params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
for (Map.Entry<String, String> entry : params.entrySet()) {
System.out.println(entry.getKey() + " / " + entry.getValue());
}
if (params.get("project") != null) {
projectId = params.get("project");
} else {
HttpServletRequest request =
(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
String projectId = request.getParameter("project");
}
}
//projectId getter and setter
//public void addNewEntity(String name, String desc) {}
}
第一次打开页面时一切正常。 GET 参数已成功处理。但是,由于 bean 是请求范围的,它在请求结束时被销毁,并在后续回发时重新创建。在这些回发期间,GET 参数不再可用,即使它在浏览器地址栏中可见。我尝试了三种获取参数的方法
来自f:viewParam 和ExternalContext 甚至来自ServletContext,但我无法获得这些参数。
我不想将@RequestScoped 更改为@SessionsScoped 并且我不能使用@ViewScoped,因为我正在使用CDI bean,我不想混合它们。
【问题讨论】:
-
将 Bean 的范围从“请求”更改为“视图”。
-
@kshitij:不幸的是,OP 使用 CDI 而不是 JSF 来管理 bean。所以特定于 JSF 的视图范围不起作用。 OP还在问题的最后一句中明确提到了它。