自己做了一个下拉列表,能够动态从数据库获取数据,但无法排除下拉列表里重复的数据,求解决方法页面代码如下
<td>作者:</td>
<td align="left">
<select name="author" id="author">
<option value="">全部</option>
<%
List <BooksInformation> allAuthor = service.getAllAuthor();
for(BooksInformation Author : allAuthor){
%>
<option value=<%=Author.getAuthor() %> <%=Author.equals(Author.getAuthor())?"selected":"" %>><%=Author.getAuthor()%></option>
<%} %>
</select>
</td>后台
public List<BooksInformation> getAllAuthor() throws DqMisException {
// TODO Auto-generated method stub
String hql="form "+BooksInformation.class.getName()+"as c where 1=1";
return HibernateServiceUtil.query(hql);
}
<td>作者:</td>
<td align="left">
<select name="author" id="author">
<option value="">全部</option>
<%
List <BooksInformation> allAuthor = service.getAllAuthor();
for(BooksInformation Author : allAuthor){
%>
<option value=<%=Author.getAuthor() %> <%=Author.equals(Author.getAuthor())?"selected":"" %>><%=Author.getAuthor()%></option>
<%} %>
</select>
</td>后台
public List<BooksInformation> getAllAuthor() throws DqMisException {
// TODO Auto-generated method stub
String hql="form "+BooksInformation.class.getName()+"as c where 1=1";
return HibernateServiceUtil.query(hql);
}
projectionList.add(Projections.property("id"));
projectionList.add(Projections.property("name"));
c.setProjection(Projections.distinct(projectionList));
List result = c.list();
试试。。看可以不。。
你不要拿个重复 在那个 字段前面加上distinct
//你直接这样写就行
tring hql="select distinct 不允许重复的字段 form "+BooksInformation.class.getName()+"as c where 1=1";
tring hql="select distinct 不允许重复的字段 form "+BooksInformation.class.getName()+"as c where 1=1";
难道说是对大小写敏感?