【问题标题】:Passing parameter to JSF action将参数传递给 JSF 动作
【发布时间】:2011-09-13 12:21:31
【问题描述】:

我正在使用 GlassFish 3.1,并尝试将参数传递给 commandButton 操作。以下是我的代码:

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" />

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" />

ManagedBean 类

package actionParam;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named("bean")
@RequestScoped
public class ActionParam {

    public ActionParam() {
        super();
    }

    public String submit(int param) {
        System.out.println("Submit using value " + param);
        return null;
    }

}

最后,

查看

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <meta http-equiv="Content-Type"
        content="text/html; charset=ISO-8859-1" />
    <title>Test Action Param</title>
</h:head>
<h:body>
    <h:form id="actionForm">
        <h:commandButton id="actionButton" value="Submit"
            action="#{bean.submit}">
            <f:param name="param" value="123"></f:param>
        </h:commandButton>
    </h:form>
</h:body>
</html>

当我点击提交按钮时,我得到 javax.el.MethodNotFoundException

如果我删除&lt;f:param ... /&gt;并传递参数如下,

.
:
        <h:commandButton id="actionButton" value="Submit"
            action="#{bean.submit(123)}">
        </h:commandButton>
:
.

它工作正常。

但我认为第一种方法(使用 f:param)是正确的。

传递参数的正确方法是什么?

提前致谢。

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

    &lt;f:param&gt; 设置 HTTP 请求参数,而不是操作方法参数。要获得它,您需要使用&lt;f:viewParam&gt;@ManagedProperty。在这种特殊情况下,后者更合适。您只需用 JSF 注释替换 CDI 注释,@ManagedProperty 即可工作:

    @ManagedBean(name="bean")
    @RequestScoped
    public class ActionParam {
    
        @ManagedProperty("#{param.param}")
        private Integer param;
    
        public String submit() {
            System.out.println("Submit using value " + param);
            return null;
        }
    
    }
    

    当您使用 web.xml&lt;web-app&gt; 根声明定义 Servlet 3.0 的 Servlet 3.0 容器(Tomcat 7、Glassfish 3、JBoss AS 6 等)时,您应该能够只传递参数EL 2.2(Servlet 3.0 的一部分)支持的 EL 直接进入 action 方法:

    <h:commandButton id="actionButton" value="Submit"
        action="#{bean.submit(123)}">
    </h:commandButton>
    

    public String submit(Integer param) {
        System.out.println("Submit using value " + param);
        return null;
    }
    

    如果您以旧的 Servlet 2.5 容器为目标,那么您应该仍然可以使用 JBoss EL 执行此操作。另请参阅此答案以了解安装和配置详细信息:Invoke direct methods or methods with arguments / variables / parameters in EL

    【讨论】:

    • 感谢 BalusC,我使用的是 GlassFish 3.1。所以,重申一下,我应该将参数直接传递给方法,而不是使用 f:param。我对正确的方法感到困惑,因为当我直接传递参数时,我的 IDE 一直显示 Facelet Validator 警告。 > EL 中的语法错误我将忽略该警告。
    • @Atul:eclipse 是你的 IDE 吗? stackoverflow.com/questions/21938029/…
    猜你喜欢
    • 2011-04-26
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    相关资源
    最近更新 更多