The tools created this field in the entity:
@Entity
@Table(name = "movies")
public class Movie implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idmovie;
// etc
@Temporal(TemporalType.DATE)
@Column(name = "year_of_release")
@Past(message = "Back from the future ?") // added by me
@NotNull(message = "Please enter the year of the movie") // added by me
private Date yearOfRelease;
}
I want to enter the year from a form - I can't find anything that produces a year drop down and that converts it to an YEAR field (getting Data truncation
exceptions).
The closest I got :
<h:outputLabel for="yearOfRelease">Year of release</h:outputLabel>
<h:inputText id="yearOfRelease"
value="#{movieController.movie.yearOfRelease}" redisplay="true"
converterMessage="Please enter date in yyyy-MM-dd format">
<f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>
<h:message id="yearOfReleaseMessage" for="yearOfRelease" />
Do not seem to find a converter to tell jsf to convert to year.
Is integer my only option ?
Also ideally I would like a dropdown. I found things like
<h:selectOneListbox value="#{form.year}" size="5">
<f:selectItem itemValue="1900" itemLabel="1900"/>
<f:selectItem itemValue="1901" itemLabel="1901"/> ... </h:selectOneListbox>
but surely there is a way to have this automated ?
I am new to JSF and I'd rather stay vanilla JavaEE (no faces libraries). I am on glassfish 4.
Partial answer
// YEAR
private static final short MIN_YEAR = 1901;
private static final short MAX_YEAR = 2014; // (short) new
// java.util.Date().getYear(); // does not play with message
private static final List<Short> YEARS = new ArrayList<>(MAX_YEAR
- MIN_YEAR + 1);
static {
for (short i = MIN_YEAR; i <= MAX_YEAR; ++i) {
YEARS.add(i);
}
}
private static final String MIN_MSG = "Min release year: " + MIN_YEAR;
private static final String MAX_MSG = "Max release year: " + MAX_YEAR;
@Column(name = "year_of_release")
@NotNull(message = "Please enter the year of release of the movie")
@Min(value = 1901, message = MIN_MSG)
@Max(value = 2014, message = MAX_MSG)
private short yearOfRelease = 2014;
public List<Short> getYears() {
return YEARS;
}
And in the form:
<h:selectOneListbox id="yearOfRelease" redisplay="true"
value="#{movieController.movie.yearOfRelease}" size="8">
<f:selectItems value="#{movieController.movie.years}" var="entry"
itemValue="#{entry}" itemLabel="#{entry}" />
<f:ajax event="blur" render="yearOfReleaseMessage" />
</h:selectOneListbox>
If someone can come up with something more elegant (like some ready made Year dropdown that validates @Past and converts to something nice for a MySQL Year datatype) I would gladly accept it.