代码之家  ›  专栏  ›  技术社区  ›  DD.

jsf命令按钮url参数

jsf
  •  7
  • DD.  · 技术社区  · 14 年前

    我想做一个按钮,导航到一个不同的URL,并在URL中传递一些请求参数。输出链接工作,但我想要一个按钮,命令按钮看起来不错,但我可以传递参数。

    有解决办法吗?

    2 回复  |  直到 11 年前
        1
  •  17
  •   BalusC    14 年前

    这个 h:commandButton 不开火 GET 请求,但 POST 请求,所以您不能使用它。如果您已经在JSF 2.0上,并且目标页面在同一上下文中,那么您可以使用 h:button 为此:

    <h:button value="press here" outcome="otherViewId">
        <f:param name="param1" value="value1" />
        <f:param name="param2" value="value2" />
    </h:button>
    

    (不) h:form 这里和中一样需要 h:outputLink )这将创建一个按钮, otherViewId.jsf?param1=value1&param2=value2 .

    但是,如果您还不在JSF 2.0上,那么您最好的方法就是抓住CSS,像按钮一样设计链接的样式。

    <h:outputLink styleClass="button">
    

    有点像

    a.button {
        display: inline-block;
        background: lightgray;
        border: 2px outset lightgray;
        cursor: default;
    }
    a.button:active {
        border-style: inset;
    }
    
        2
  •  5
  •   pakore    14 年前

    用你关联的按钮 action ,这是backing bean中的一种方法 您可以在backing bean中设置参数,并在按下按钮时从链接到的方法中读取它们。 行动 . action方法应返回 String ,导航处理程序将根据中的配置读取,以检查是否必须移动到新页。 faces-config.xml .

    <h:form>
        <h:commandButton value="Press here" action="#{myBean.action}">
            <f:setPropertyActionListener target="#{myBean.propertyName1}" value="propertyValue1" />
            <f:setPropertyActionListener target="#{myBean.propertyName2}" value="propertyValue2" />
        </h:commandButton>
    </h:form>
    

    背豆:

    package mypackage;
    
    
    public class MyBean {
    
        // Init --------------------------------------------------------------------------------------
    
        private String propertyName1;
        private String propertyName2;
    
        // Actions -----------------------------------------------------------------------------------
    
        public void action() {
            System.out.println("propertyName1: " + propertyName1);
            System.out.println("propertyName2: " + propertyName2);
        }
    
        // Setters -----------------------------------------------------------------------------------
    
        public void setPropertyName1(String propertyName1) {
            this.propertyName1 = propertyName1;
        }
    
        public void setPropertyName2(String propertyName2) {
            this.propertyName2 = propertyName2;
        }
    
    }
    

    这个例子取自 here (俾路支博客,可能他会来告诉你检查链接,但我更快!P)

    当然,要做到这一点,必须将bean设置为 session scoped . 如果你想的话 request scoped 你可以按照步骤 here