我有一个网络应用程序,用户可以直接发送到某些特定页面(例如他可以查看或编辑项目的页面).为此,我们提供了一个特定的网址.这些网址位于外电流网络的应用程序(即,它们可以存在于另一网络的应用程序,或电子邮件).
网址看起来像http://myserver/my-app/forward.jsf?action=XXX¶m=YYY,其中:
from-outcome任何JSF的行动navigation-case中faces-config.xml.例如,我可以拥有这些网址:
http://myserver/my-app/forward.jsf?action=viewItem&actionParam=1234http://myserver/my-app/forward.jsf?action=editItem&actionParam=1234当然,我有一个Java类(bean),它将检查一些安全性约束(即允许用户查看/编辑相应的项目?),然后将用户重定向到正确的页面(例如edit.xhtml,view.xhtml或access-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>
Run Code Online (Sandbox Code Playgroud)
如您所见,我<h:inputHidden>在Java bean中绑定了两个组件.它们将用于存储both action和actionParamrequest参数(使用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);
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案正在运行,但我有两个问题:
所以我的问题是如何重构这个重定向/转发功能?
技术信息
Java 1.6,JSF 1.2,Facelets,Richfaces
Bal*_*usC 30
将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>
Run Code Online (Sandbox Code Playgroud)
这样请求forward.jsf?action=outcome1&actionParam=123将让JSF设置action和actionParam参数as action和actionParam属性ForwardBean.
创建一个小视图forward.xhtml(非常小以至于它适合默认响应缓冲区(通常为2KB),以便导航处理程序可以重置它,否则你需要增加servletcontainer配置中的响应缓冲区),这将调用bean方法beforePhase.的f:view:
<!DOCTYPE html>
<html xmlns:f="http://java.sun.com/jsf/core">
<f:view beforePhase="#{forward.navigate}" />
</html>
Run Code Online (Sandbox Code Playgroud)
该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.
}
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用forward.xhtmlas
<!DOCTYPE html>
<html>#{forward}</html>
Run Code Online (Sandbox Code Playgroud)
并更新navigate()要调用的方法@PostConstruct(将在bean构造和所有托管属性设置之后调用):
@PostConstruct
public void navigate() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
它具有相同的效果,但视图方面并非真正自我记录.所有它基本上都是打印ForwardBean#toString()(并因此隐含地构造bean,如果还不存在).
注意JSF2用户,有一种更简洁的传递参数的方式,<f:viewParam>以及更强大的方法来处理重定向/导航<f:event type="preRenderView">.另见:
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();
response.sendRedirect("somePage.jsp");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
127410 次 |
| 最近记录: |