Ray*_*Ray 4 jsf redirect myfaces jsf-2
我认为有一个索引页面(在我的例子中是index.xhtml)是一个很好的做法.我想在索引页面上传递一些操作(例如在struts中:<c:redirect url="list.do" />我转到struts动作类,没有任何链接和按钮)我知道如果我想使用导航我应该使用commandLink-s或按钮).我可以<h:commandButton>用onclick javascript函数编写,但我觉得这不是最好的选择.
我是JSF的新手(使用JSF 2.0),我需要你的建议.从索引页重定向到控制器中的操作的最佳实践是什么?
///新版本
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<ui:insert name="metadata"/>
<f:viewParam name="action" value="listItems.xtml"/>
<f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" />
<h:body></h:body>
</f:view>
</html>
public class ForwardBean {
private String action;
// getter, setter
public void navigate(PhaseEvent event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
String outcome = action;
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用JSF preRenderView事件以下列方式重定向到另一个页面,
在index.xhtml文件中
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<ui:insert name="metadata"/>
<f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" />
<h:body></h:body>
</f:view>
</html>
Run Code Online (Sandbox Code Playgroud)
在托管bean中, 第一种方式是
public class yourClass{
FacesContext fc = FacesContext.getCurrentInstance();
ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)fc.getApplication().getNavigationHandler();
public void methodInManagedBean() throws IOException {
nav.performNavigation("list.do");//add your URL here, instead of list.do
}
}
Run Code Online (Sandbox Code Playgroud)
或者你可以使用第二种方式
public class yourClass{
public void methodInManagedBean() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect("list.do");//add your URL here, instead of list.do
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17181 次 |
| 最近记录: |