我想创建一个导航到不同URL的按钮,并在URL中传递一些请求参数.outputLink工作,但我想要一个按钮,commandButton看起来不错,但我可以传递参数.
有解决方案吗?
Bal*_*usC 17
在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>
Run Code Online (Sandbox Code Playgroud)
(h:form此处不需要h:outputLink).这将创建一个按钮otherViewId.jsf?param1=value1¶m2=value2.
但是如果你还没有使用JSF 2.0,那么你最好的方法就是抓住CSS来设置链接的样式.
<h:outputLink styleClass="button">
Run Code Online (Sandbox Code Playgroud)
喜欢的东西
a.button {
display: inline-block;
background: lightgray;
border: 2px outset lightgray;
cursor: default;
}
a.button:active {
border-style: inset;
}
Run Code Online (Sandbox Code Playgroud)
使用关联的按钮action,这是辅助bean中的方法您可以在辅助bean中设置params,并在按下按钮时从链接的方法读取它们action.action方法应返回a 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>
Run Code Online (Sandbox Code Playgroud)
支持豆:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这个例子来自这里(BalusC博客,可能他会来告诉你检查那个链接,但我更快!:P)
当然,为了实现这一点,必须将bean设置为session scoped.如果您想要它,request scoped您可以按照此处的步骤操作
| 归档时间: |
|
| 查看次数: |
15132 次 |
| 最近记录: |