gpt4 book ai didi

java - 如何在 JSF 1.x 中对页面加载进行重定向

转载 作者:IT老高 更新时间:2023-10-28 21:06:20 27 4
gpt4 key购买 nike

我有一个网络应用程序,可以将用户直接发送到某些特定页面(例如他可以查看或编辑项目的页面)。为此,我们提供了一个特定的网址。这些 url 位于当前网络应用程序外部(即它们可以存在于另一个网络应用程序或电子邮件中)。

网址看起来像 http://myserver/my-app/forward.jsf?action=XXX&param=YYY ,其中:

  • action 表示用户将被重定向到的页面。您可以将其视为 from-outcome navigation-case 中的任何 JSF 操作在 faces-config.xml .
  • actionParam是上一个 Action 的参数(一般是item ID)

例如,我可以拥有这些类型的网址:

  • http://myserver/my-app/forward.jsf?action=viewItem&actionParam=1234
  • http://myserver/my-app/forward.jsf?action=editItem&actionParam=1234

当然,我有一个 Java 类(bean),它会检查一些安全约束(即是否允许用户查看/编辑相应的项目?)然后将用户重定向到正确的页面(例如 edit.xhtml , view.xhtmlaccess-denied.xhtml )。


当前实现

目前,我们有一个基本的方法来完成转发。当用户单击该链接时,将调用以下 XHTML 页面:

<html>
<body id="forwardForm">
<h:inputHidden id="myAction" binding="#{forwardBean.hiddenAction}"/>
<h:inputHidden id="myParam" binding="#{forwardBean.hiddenActionParam}"/>
<h:commandButton id="forwardBtn" actionListener="#{forwardBean.doForward}" style="display: none;"/>
</body>
<script type="text/javascript">
document.getElementById('forwardForm:forwardBtn').click();
</script>
</html>

如你所见,我绑定(bind)了两个<h:inputHidden>我的 Java bean 中的组件。它们将用于存储 action 的值和 actionParam请求参数(使用 FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("actiontParam"); )。我还提供doForward渲染页面时将立即调用的方法,它将(再次)将用户重定向到真实页面。方法是:

public void doForward(ActionEvent evt) {
FacesContext facesContext = FacesContext.getCurrentInstance();
String redirect = // define the navigation rule that must be used in order to redirect the user to the adequate page...
NavigationHandler myNav = facesContext.getApplication().getNavigationHandler();
myNav.handleNavigation(facesContext, null, redirect);
}

此解决方案有效,但我有两个问题:

  • 我不喜欢它的实现方式。我确信我可以拥有更简单的东西(使用 Servlet?)。
  • 此解决方案使用 Javascript,我不得使用 Javascript(因为此转发可能由不支持 Javascript 的老黑莓用​​户使用)。

所以我的问题是如何重构这个重定向/转发功能?

技术资料

Java 1.6、JSF 1.2、Facelets、Richfaces

最佳答案

将 GET 查询参数设置为 faces-config.xml 中的托管属性这样您就无需手动收集它们:

<managed-bean>
<managed-bean-name>forward</managed-bean-name>
<managed-bean-class>com.example.ForwardBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>action</property-name>
<value>#{param.action}</value>
</managed-property>
<managed-property>
<property-name>actionParam</property-name>
<value>#{param.actionParam}</value>
</managed-property>
</managed-bean>

这样请求forward.jsf?action=outcome1&actionParam=123会让 JSF 设置 actionactionParam参数为 actionactionParam ForwardBean 的属性.

创建一个小 View forward.xhtml (非常小以至于它适合默认响应缓冲区(通常为 2KB),因此可以由导航处理程序重置,否则您必须在 servletcontainer 的配置中增加响应缓冲区),它调用 beforePhase 上的 bean 方法的f:view :

<!DOCTYPE html>
<html xmlns:f="http://java.sun.com/jsf/core">
<f:view beforePhase="#{forward.navigate}" />
</html>

ForwardBean可以是这样的:

public class ForwardBean {
private String action;
private String actionParam;

public void navigate(PhaseEvent event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
String outcome = action; // Do your thing?
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome);
}

// Add/generate the usual boilerplate.
}

navigation-rule不言而喻(注意 <redirect /> 条目会在封面下执行 ExternalContext#redirect() 而不是 ExternalContext#dispatch()):

<navigation-rule>
<navigation-case>
<from-outcome>outcome1</from-outcome>
<to-view-id>/outcome1.xhtml</to-view-id>
<redirect />
</navigation-case>
<navigation-case>
<from-outcome>outcome2</from-outcome>
<to-view-id>/outcome2.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>

另一种方法是使用 forward.xhtml作为

<!DOCTYPE html>
<html>#{forward}</html>

并更新 navigate()要在 @PostConstruct 上调用的方法(将在 bean 的构造和所有托管属性设置后调用):

@PostConstruct
public void navigate() {
// ...
}

它具有相同的效果,但是 View 端并不是真正的 self 记录。它基本上所做的只是打印ForwardBean#toString() (并且在此隐式构造 bean,如果尚未存在)。


对于 JSF2 用户,请注意,使用 <f:viewParam> 传递参数有一种更简洁的方式。和更强大的 <f:event type="preRenderView"> 处理重定向/导航的方法.另见:

关于java - 如何在 JSF 1.x 中对页面加载进行重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4032825/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com