在h:commandLink操作中传递查询参数值

时间:2014-09-12 20:45:21

标签: jsf-2 el

尝试通过jsf commandLink操作将查询参数值(参数名称targetURL)传递给后端bean方法。

当我在下面执行时(使用围绕param.targetURL的引用),文字param.targetURL将传递给方法:

<h:commandLink id="targetURLLink" action="#{commonTaskBean.redirectTo('param.targetURL')}"  style="display:none" />

当我在下面做时(没有引用围绕param.targetURL),

<h:commandLink id="targetURLLink" action="#{commonTaskBean.redirectTo(param.targetURL)}"  style="display:none" />

我收到此错误NullPointerException:

 9/15/14 9:20:44:529 CDT] 00000039 SystemErr     R java.lang.NullPointerException
[9/15/14 9:20:44:529 CDT] 00000039 SystemErr     R  at org.apache.el.util.ReflectionUtil.paramString(ReflectionUtil.java:315)
[9/15/14 9:20:44:529 CDT] 00000039 SystemErr     R  at org.apache.el.util.ReflectionUtil.getMethod(ReflectionUtil.java:234)
[9/15/14 9:20:44:529 CDT] 00000039 SystemErr     R  at org.apache.el.parser.AstValue.invoke(AstValue.java:255)
[9/15/14 9:20:44:529 CDT] 00000039 SystemErr     R  at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
[9/15/14 9:20:44:529 CDT] 00000039 SystemErr     R  at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at javax.faces.component.UICommand.broadcast(UICommand.java:120)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:973)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:275)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1285)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:711)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
[9/15/14 9:20:44:530 CDT] 00000039 SystemErr     R  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)

我将上面的代码更改为如下所示:

<h:commandLink id="targetURLLink" actionListener="#{commonTaskBean.redirectTo}" style="display:none" >
    <f:attribute name="targetURL" value="#{param.targetURL}" />
</h:commandLink>    

但是后端bean获得了一个空值:

String targetURL = (String) event.getComponent().getAttributes().get("targetURL");

这很奇怪,因为下面在屏幕上正确显示了targetURL param的值:

<h:outputText value="#{param.targetURL}"/>

1 个答案:

答案 0 :(得分:0)

你的问题是双重的。

首先,您遇到的例外情况是由相对古老版本的Apache EL中的错误引起的。它无法正确处理作为方法参数传递的null。升级EL实现(或者通常更合适的是容器实现)应该解决它。

其次,您似乎对传递使用表单打开页面时可用的请求参数感兴趣,而不是在页面中提交表单时可用的请求参数中。因此,将#{param}作为EL方法参数方法引用是根本错误的。 #{param}引用当前请求的请求参数映射。在提交表单时,这就是表单提交请求本身。您需要<f:param>。它是在第一次呈现带有表单的页面时进行评估的(作为证据,您实际上可以在HTML源代码中看到它)。

<h:commandLink ... action="#{commonTaskBean.redirectTo}">
    <f:param name="targetURL" value="#{param.targetURL}" />
</h:commandLink>
public void redirectTo() throws IOException {
    // ...

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    String targetURL = ec.getRequestParameterMap().get("targetURL");
    ec.redirect(targetURL);
}