public class ElecSystemDDLAction extends BaseAction<ElecSystemDDL>{ ElecSystemDDL elecSystemDDL=this.getModel(); //注入數據字典service@Resource(name=IElecSystemDDLService.SERVICE_NAME) IElecSystemDDLService elecSystemDDLService; /** * @Name: home * @Description: 跳轉到數據字典頁面 * @Parameters: 無 * @Return: String:跳轉到system/dictionaryIndex.jsp*/public String home(){// 1:查詢數據庫中已有的數據類型,返回List<ElecSystemDDL>集合 List<ElecSystemDDL> list=elecSystemDDLService.findSystemDDLListByDistinct(); // 2:將ElecSystemDDL對象放入request中 request.setAttribute("list", list);return "home"; } }
public interface IElecSystemDDLService {public static final String SERVICE_NAME="cn.elec.service.impl.ElecSystemDDLServiceImpl"; List<ElecSystemDDL> findSystemDDLListByDistinct(); }
public class ElecSystemDDLServiceImpl implements IElecSystemDDLService{//數據字典Dao@Resource(name=IElecSystemDDLDao.SERVICE_NAME)private IElecSystemDDLDao elecSystemDDLDao; /** * @Name: findSystemDDLListByDistinct * @Description: 查詢數據庫中已有的數據類型,去掉重復值 * @Parameters: 無 * @Return: List:存儲數據類型集合*/@Override @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)public List<ElecSystemDDL> findSystemDDLListByDistinct() { List<ElecSystemDDL> list=elecSystemDDLDao.findSystemDDLListByDistinct();return list; } }
public interface IElecSystemDDLDao extends ICommonDao<ElecSystemDDL> {public static final String SERVICE_NAME="cn.elec.dao.imp.ElecSystemDDLImpl"; List<ElecSystemDDL> findSystemDDLListByDistinct(); }
@Repository(IElecSystemDDLDao.SERVICE_NAME)public class ElecSystemDDLImpl extends ICommonDaoImpl<ElecSystemDDL> implements IElecSystemDDLDao{/** * @Name: findSystemDDLListByDistinct * @Description: 查詢數據庫中已有的數據類型,去掉重復值 * @Parameters: 無 * @Return: List:存儲數據類型集合*/@Overridepublic List<ElecSystemDDL> findSystemDDLListByDistinct() {//返回List集合List<ElecSystemDDL> systemList = new ArrayList<ElecSystemDDL>();/**方法一:投影查詢一個字段返回List<Object>中 String hql="SELECT DISTINCT o.keyword FROM ElecSystemDDL o"; List<Object> list = this.getHibernateTemplate().find(hql); //組織頁面返回結果 if(list!=null&&list.size()>0){ for(Object obj:list){ ElecSystemDDL elecSystemDDL = new ElecSystemDDL(); //設置數據類型,并添加到systemList elecSystemDDL.setKeyword(obj.toString()); systemList.add(elecSystemDDL); } }*///方法二:使用hql語句直接將投影查詢的字段放置到對象中String hql="SELECT DISTINCT new cn.elec.domain.ElecSystemDDL(o.keyword) FROM ElecSystemDDL o"; systemList=this.getHibernateTemplate().find(hql);return systemList; } }
其中第二種查詢方案需要在ElecSystemDDL中創建一個帶參得的構造方法:
public ElecSystemDDL() {//無參構造方法} public ElecSystemDDL(String keyword) {//帶參構造方法this.keyword=keyword; }
因為在Actionl類中將List<ElecSystemDDL>集合放入request對象中,屬性名為list。當dictionary.jsp頁面獲得request.list之后需要將ElecSystemDDL對象的keyword屬性遍歷出來,放入下拉選項框中,有兩種方案:
<tr> <td class="ta_01" align="right" width="35%" >類型列表:</td> <td class="ta_01" align="left" width="30%" > <!-- 方案一 <select name="keyword" class="bg" style="width:180px" onchange="changetype()"> <option value="jerrynew"></option> <s:iterator value="#request.list" var="sys"> <option value="<s:property value="#sys.keyword"/>"> <s:property value="#sys.keyword"/> </option> </s:iterator> </select> --> <!-- 方案二 --> <s:select list="#request.list" name="keyword" id="keyword" listKey="keyword" listValue="keyword" headerKey="jerrynew" headerValue="" cssClass="bg" cssStyle="width:180px" onchange="changetype()"> </s:select> </td> </tr>
測試,在數據字典首頁功能頁面中,下拉菜單能正確顯示類型名稱。
1.hql和sql語句的投影查詢:
(1)如果投影查詢是一個字段,此時返回List<Object>,例如
String hql = "SELECT DISTINCT o.keyword FROM ElecSystemDDL o"; List<Object> list = this.getHibernateTemplate().find(hql);
(2)如果投影查詢是多個字段,此時返回List<Object[]>,例如
String hql = "SELECT DISTINCT o.keyword,o.ddlName FROM ElecSystemDDL o"; List<Object[]> list = this.getHibernateTemplate().find(hql);
(3)如果投影查詢是多個字段,此時返回List<Object[]>,例如
String hql = "SELECT o,o.ddlName FROM ElecSystemDDL o"; List<Object[]> list = this.getHibernateTemplate().find(hql);
數組的第一個值,是一個ElecSystemDDL的對象,數組的第二個值表示字段ddlName的值。
(4)如果投影查詢是一個對象,此時返回List<ElecSystemDDL>,例如
String hql = "SELECT o FROM ElecSystemDDL o"; List<ElecSystemDDL> list = this.getHibernateTemplate().find(hql);
(5)如果是hql語句,使用hql語句直接將投影查詢的字段放置到對象中,例如
String hql = "SELECT DISTINCT new cn.itcast.elec.domain.ElecSystemDDL(o.keyword) FROM ElecSystemDDL o"; List<ElecSystemDDL> list = this.getHibernateTemplate().find(hql);
(1)方案一:使用<s:iterator>遍歷<option>
<select name="keyword" class="bg" style="width:180px" onchange="changetype()"> <option value="jerrynew"></option> <s:iterator value="#request.list" var="system"> <option value="<s:property value="#system.keyword"/>"> <s:property value="#system.keyword"/> </option> </s:iterator> </select>
(2)方案二:使用<s:select>
<s:select list="#request.list" name="keyword" id="keyword"listKey="keyword" listValue="keyword"headerKey="jerrynew" headerValue=""cssClass="bg" cssStyle="width:180px" onchange="changetype()"> </s:select>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com