我目前正在学习 JSF,并且遇到了一些不调用 setter 的问题。我正在编写一个非常简单的程序,它由一个 .XHTML 文件和一个 .java 类组成。该程序的重点是选择日、月和年,然后显示您的选择。据我了解JSF,以下应调用底层设置器并将适当的值保存在适当的字段中:
<h:selectOneMenu value ="#{bean.selectedDay}" onchange = "submit()">
<f:selectItems value ="#{bean.days}"/>
</h:selectOneMenu>
在此示例中,从未为字段“selectedDay”分配任何值。我真的很想了解为什么会这样。我目前正在运行 Windows 8 并使用带有 Glassfish 的 Eclipse。完整的代码可以在下面看到。感谢您的时间。
<?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;
}