想要實現介面的一個小功能,下拉框讀取資料庫資訊,顯示在街面上,點擊所選中的值,儲存到資料庫中。
下面看下介面效果:
首先介紹下介面jsp代碼的實現:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><div class="formitem"><div class="f_label"><span class="red">*</span>使用者名稱稱:</div><div class="f_item"></span>
<span style="font-family:KaiTi_GB2312;font-size:18px;"> <!--這裡是將從資料庫中讀取到的資料,顯示到介面上--></span>
<span style="font-family:KaiTi_GB2312;font-size:18px;"><input type="text" id="realname" name="realname" /> <input type="hidden"name="allotuuid" id="allotuuid" /> <input type="hidden" id="userid" name="userid" /></div></div></span>
javascrip實現
<span style="font-family:KaiTi_GB2312;font-size:18px;">$(function() {createSelect('realname', {valueFieldID:"userid",//擷取隱藏欄位idtextField: 'realname',//介面文字框顯示的值selectBoxHeight: 200,valueField:'userid',url:'${path}/AssetQueryUsername/queryRealname.json',checkbox:false});</span>
valuefield,相當於擷取使用者名稱所對應的id,將其傳到後台,進行操作()
Action層
<span style="font-family:KaiTi_GB2312;font-size:18px;">/**** * 查詢使用者姓名 * * @author * */@Controller@RequestMapping(value = "/AssetQueryUsername")public class AssetQueryUsernameAction extends BaseAction {@Autowiredprivate AssetQueryUsernameService assetQueryUsernameService;/*** * 查詢資產發放人員名稱 */@RequestMapping(value = "/queryRealname.json", method = {RequestMethod.GET, RequestMethod.POST })@ResponseBodypublic Object queryRealname(SnUser snUser, HttpServletRequest request) {List data = (List) assetQueryUsernameService.queryRealname(snUser,request);return data;}}</span>
Service層
<span style="font-family:KaiTi_GB2312;font-size:18px;">@Servicepublic class AssetQueryUsernameService extends BaseService<SnUser, String> {/*** * 查詢資產發放人員名稱 * * @param realname * @param userid * @param request * @return */public List queryRealname(SnUser snUser,HttpServletRequest request) {String hql = " from SnUser where userlevel='0'";// 查詢使用者物件List<SnUser> listUser = this.listByHql(hql);// 返回list集合return listUser;}}</span>
這裡用到了SpringMvc技術,後續文章會具體介紹,一個簡單的小功能就實現了,動手試試,很簡單~