在您第一次加载JSF页面时生成一个List。它是一个@PostConstruct方法。然而,它导致我的变量"Title"在我的"FilmResultBean"为空,我不明白为什么。
相关代码:DataTableBean:
@Component
@ManagedBean(name= DataTableBean.BEAN_NAME)
@Scope("request")
public class DataTableBean implements Serializable {
public static final String BEAN_NAME = "dataTableBean";
public static Logger logger = Logger.getLogger(DataTableBean.class);
@Autowired
@ManagedProperty(value = "filmResultBean")
FilmResultBean resultBean;
@Autowired
FilmBo filmBo;
private List<FilmResultBean> filmList;
private List<String> categoryList;
private String categoryName;
private String titleInput;
/* Default Constructor */
public DataTableBean() {
}
/*Queries the Database for a list of Film Objects and returns the films in
* a list of type "FilmResultBean"
*/
@PostConstruct
public void init(){
filmList = filmBo.generateFilmList(resultBean);
}
public void searchByBoth(){
filmList = filmBo.searchByBoth(resultBean);
logger.info("FilmList Inside SEARCHBYBOTH: " + filmList);
}
public String getTitleInput() {
return titleInput;
}
public void setTitleInput(String titleInput) {
this.titleInput = titleInput;
}
public List<String> getCategoryList() {
return categoryList;
}
public void setCategoryList(List<String> categoryList) {
this.categoryList = categoryList;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public void searchFilms(){
}
public List<FilmResultBean> getFilmList() {
return filmList;
}
public void setFilmList(List<FilmResultBean> filmList) {
this.filmList = filmList;
}
}
JSF页面:(值是什么回来作为空,只有当我使用@PostConstruct)
<ace:textEntry value="#{filmResultBean.title}">
<ace:ajax listener="#{dataTableBean.searchByBoth}" execute="@this" render="form" />
</ace:textEntry>
特定方法被@PostConstruct调用:
@Override
public List<FilmResultBean> generateFilmList(FilmResultBean films) {
Session session = getCurrentSession();
List<FilmResultBean> filmList;
Film film = new Film();
Query query = null;
films.setTitle(film.getTitle());
films.setRating(film.getRating());
films.setRentalRate(film.getRentalRate());
String hql;
hql = "from Film";
query = session.createQuery(hql);
logger.info("Query: " + query);
filmList = (query.list());
return filmList;
}
FilmResultBean:
@Component
@ManagedBean
@SessionScoped
@Scope("session")
public class FilmResultBean implements Serializable {
BigDecimal rentalRate;
Short length;
String rating;
String title;
String category;
public FilmResultBean() {
}
public FilmResultBean(BigDecimal rentalRate, Short length, String rating,
String title, String category) {
this.rentalRate = rentalRate;
this.length = length;
this.rating = rating;
this.title = title;
this.category = category;
}
public BigDecimal getRentalRate() {
return rentalRate;
}
public void setRentalRate(BigDecimal rentalRate) {
this.rentalRate = rentalRate;
}
public Short getLength() {
return length;
}
public void setLength(Short length) {
this.length = length;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
@Override
public String toString() {
return title;
}
}
日志信息:
23:51:41,013 INFO om.cooksys.training.dao.impl.FilmDaoImpl: 82 - Title: null
23:51:41,013 INFO om.cooksys.training.dao.impl.FilmDaoImpl: 83 - Category: null
23:51:41,038 INFO om.cooksys.training.dao.impl.FilmDaoImpl: 92 - Query: QueryImpl(select new com.cooksys.training.FilmResultBean(fc.film.rentalRate, fc.film.length, fc.film.rating, fc.film.title, fc.category.name) from Film f join f.filmCategories fc where f.title like :title)
Hibernate:
select
film2_.rental_rate as col_0_0_,
film2_.length as col_1_0_,
film2_.rating as col_2_0_,
film2_.title as col_3_0_,
category6_.name as col_4_0_
from
sakila.film film0_
inner join
sakila.film_category filmcatego1_
on film0_.film_id=filmcatego1_.film_id,
sakila.film film2_,
sakila.category category6_
where
filmcatego1_.film_id=film2_.film_id
and filmcatego1_.category_id=category6_.category_id
and (
film0_.title like ?
)
23:51:41,045 INFO com.cooksys.training.DataTableBean: 63 - FilmList Inside SEARCHBYBOTH: []
似乎您的变量(和其他像Category
) 是 null
,因为托管bean作为属性的注入没有正确执行,您必须通过JSF EL表达式指定其值:@ManagedProperty("#{filmResultBean}")
。
我认为不建议为bean和java类混合使用Spring和JSF注释,只需保留其中一个即可。
在下面的方法中,您试图通过空电影实例设置films
的属性。方法参数films
在方法中被发现未使用,它的用途是什么?
@Override
public List<FilmResultBean> generateFilmList(FilmResultBean films) {
...
Film film = new Film();
...
films.setTitle(film.getTitle());
films.setRating(film.getRating());
films.setRentalRate(film.getRentalRate());
...
}