我目前正在学习JSF,并且在设置器未被调用时遇到了一些问题。我正在编写一个非常简单的程序,它由.XHTML文件和.java类组成。该计划的目的是选择日,月和年,然后显示您的选择。据我了解JSF,以下内容应该调用底层的setter并在适当的字段中保存适当的值:
<h:selectOneMenu value ="#{bean.selectedDay}" onchange = "submit()">
<f:selectItems value ="#{bean.days}"/>
</h:selectOneMenu>
在此示例中,字段&#34; selectedDay&#34;永远不会分配任何价值。我真的很想知道为什么会这样。我目前正在运行Windows 8并将Eclipse与Glassfish一起使用。完整代码可以在下面看到。 谢谢你的时间。
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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>
<title>kalenderting</title>
</h:head>
<h:body>
<h:form>
<h:selectOneMenu value ="#{bean.selectedDay}" onchange = "submit()">
<f:selectItems value ="#{opg2.days}"/>
</h:selectOneMenu>
<h:selectOneMenu value ="#{bean.selectedMonth}" onchange = "submit()" >
<f:selectItems value ="#{bean.months}"/>
</h:selectOneMenu>
<br/>
#{bean.date}
</h:form>
</h:body>
豆子:
@Named
@SessionScoped
public class Bean implements Serializable {
private List<Integer> days;
private List<String> months;
private List<Integer> years;
private int selectedDay ;
private String selectedMonth;
public Bean()
{
months = new ArrayList<String>();
years = new ArrayList<Integer>();
days = new ArrayList<Integer>();
days.add(1);
days.add(2);
months.add("januar");
months.add("februar");
for( int i = 1990; i<=2020; i++)
{
years.add(i);
}
}
public List<Integer> getDays() {
return days;
}
public void setDays(List<Integer> days) {
this.days = days;
}
public List<String> getMonths() {
return months;
}
public void setMonths(List<String> months) {
this.months = months;
}
public List<Integer> getYears() {
return years;
}
public void setYears(List<Integer> years) {
this.years = years;
}
public int getSelectedDay() {
return selectedDay;
}
public void setSelectedDay(int selectedDay) {
this.selectedDay = selectedDay;
}
public String getSelectedMonth() {
return selectedMonth;
}
public void setSelectedMonth(String selectedMonth) {
this.selectedMonth = selectedMonth;
}
public String getDate()
{
return selectedDay + ". " + selectedMonth;
}