Ste*_*nov 3 java xhtml jsf redirect
我的问题是bean中参数的值为null
在xhtml中我有这个代码:
<h:commandLink action="#{navigation.editNews}" value="#{new.title}">
<f:param name="newsId" value="#{new.id}" />
</h:commandLink>
Run Code Online (Sandbox Code Playgroud)
在导航中,我重定向到news.xhtml
public String editNews(){
return "editNews";
}
Run Code Online (Sandbox Code Playgroud)
这是faces-config.xml中的代码
<navigation-case>
<from-action>#{navigation.editNews}</from-action>
<from-outcome>editNews</from-outcome>
<to-view-id>/news.xhtml</to-view-id>
<redirect />
</navigation-case>
Run Code Online (Sandbox Code Playgroud)
当我在news.xhtml中按下一个按钮时,我有一个bean调用方法,我尝试获取param,但它是null
FacesContext fc = FacesContext.getCurrentInstance();
Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
String = params.get("newsId");
Run Code Online (Sandbox Code Playgroud)
在<f:param>增加了一个请求参数.因此,此参数的生命周期恰好为一个HTTP请求.您<redirect/>的导航案例中有一个基本上指示Web 浏览器在给定位置发送新请求.此新请求不再包含该参数.
你基本上有两个选择:
摆脱<redirect />导航案例.
改为使其成为正常的GET请求.如果您使用的是JSF2,请<h:link>改用.
<h:link action="news" value="#{news.title}">
<f:param name="newsId" value="#{news.id}" />
</h:link>
Run Code Online (Sandbox Code Playgroud)
或者如果你仍然使用JSF 1.x(由于新的隐式导航功能,导航案例的使用少了或多了一些,因为它们在JSF 2中是多余的;或者你必须阅读过时的教程/书籍/答案JSF 1.x;在您的问题上缺少JSF 2.0标记是可疑的),然后使用普通<a>链接.
<a href="news.xhtml?newsId=#{news.id}">#{news.title}</a>
Run Code Online (Sandbox Code Playgroud)