【发布时间】:2016-07-14 16:17:41
【问题描述】:
我需要在提交表单后导航到不同的页面,具体取决于用户在逗号分隔的数字字段中输入的值。
以下是获取comsep值并提交按钮的代码:
<h:inputText id="comsep" value="#{bean.comsep}" ></h:inputText>
<h:commandButton value="Submit" action ="bean.func"></h:commandButton>
这就是豆子:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
@ManagedBean
public class Bean
{
public String comsep;
public Bean()
{
}
public String func()
{
double avg = calculate();
if (avg == 10)
{ return "casea"; }
else
{ return "caseb"; }
}
public double calculate()
{
String[] pos = comsep.split(",");
double avg = 0;
for (int i = 0; i < pos.length; i++)
avg = avg + Integer.parseInt(pos[i]);
avg = avg / pos.length;
return avg;
}
public String getComsep() {
return comsep;
}
public void setComsep(String comsep) {
this.comsep = comsep;
}
}
这是 faces-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee h http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<navigation-rule>
<from-view-id>/Home.xhtml</from-view-id>
<navigation-case>
<from-action>#{bean.func}</from-action>
<from-outcome>casea</from-outcome>
<to-view-id>/Page1.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{bean.func}</from-action>
<from-outcome>caseb</from-outcome>
<to-view-id>/Page2.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
如果 avg == 10 则预期结果是 page1,否则为 page2,但这并没有发生。当我点击提交时,什么都没有发生,但 home.xhtml 被刷新了。
我对 JSF 还很陌生,对于新手的错误,如果有的话,我深表歉意。
【问题讨论】:
-
所以它适用于
h:inputtext?并且无需在标题中添加“标签”。同时总结标题中的问题。请从一些好的非 jsf 1.2 教程开始 -
@Kukeltje 不,它也不适用于 h:inputtext。
标签: jsf navigation xhtml