Implement dynamically obtaining the corresponding List collection from the database and displaying it in Easyui's combobox.
The effect achieved is as follows:
1. The table design of the database is shown in the figure
2. Fill in the relevant data in the database, as shown in the figure. As shown in the figure, [Legal Regulations] is the column to which it belongs, so its field parentid is 0. [Chinese citizens going abroad] and [Mainland residents traveling to Hong Kong and Macao] are categories of laws and regulations. Therefore, their field parentid corresponds to 1, and the field categoryid of [Legal and Regulations] is 1.
3. Related configuration: I have written in the previous blog //www.VeVB.COM/article/86381.htm
I won't write much here. Only post the key code.
4. Corresponding Action Code
package crj.portal.web.management.action;import java.io.IOException;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import net.sf.json.JsonConfig;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;import org.hibernate.Criteria;import org.hibernate.criterion.Order;import com.sun.tools.javac.util.Log;import crj.portal.web.management.model.Cpersontypetbl;import crj.portal.web.management.service.CategoryService;import crj.portal.web.management.service.ItemService;import crj.portal.web.management.service.UserService;public class ItemManageAction { Logger log=Logger.getLogger(this.getClass()); private String page; private String rows; private String lanmuid; private ItemService itemService;// Dependency injection// Pull-down box--query column public String categorytbl() throws Exception{ List list=itemService.queryLanMu(); this.toJsonArray(list); return null; } //Query the category owned by the column ID public String leibie() throws Exception{ List list=itemService.queryLeiBie(lanmuid); this.toJsonArray(list); return null; } public String toJsonArray(List list) throws IOException{ HttpServletResponse response = ServletActionContext.getResponse(); HttpServletRequest request = ServletActionContext.getRequest(); JSONArray json = JSONArray.fromObject(list); log.info("JSON format:" +json.toString()); response.setCharacterEncoding("utf-8");// Specify as utf-8 response.getWriter().write(json.toString());// Convert to JSOn format return null; } public String save() throws Exception { return this.alllist(); } public ItemService getItemService() { return itemService; } public void setItemService(ItemService itemService) { this.itemService = itemService; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } public String getRows() { return rows; } public void setRows(String rows) { this.rows = rows; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public CategoryService getCategoryService() { return categoryService; } public void setCategoryService(CategoryService categoryService) { this.categoryService = categoryService; } public String getLanmuid() { return lanmuid; } public void setLanmuid(String lanmuid) { this.lanmuid = lanmuid; } } 5. Corresponding interface code
public interface ItemService { // Pull-down box--Query column public List queryLanMu() throws Exception; // Pull-down box-Query category public List queryLeiBie(String ids) throws Exception; } 6. Corresponding interface implementation class code
public class ItemServiceImpl implements ItemService { Logger log = Logger.getLogger(this.getClass()); private SessionFactory sessionFactory; // Pull-down box--Query column public List queryLanMu() throws Exception { Criteria criteria=this.sessionFactory.getCurrentSession().createCriteria(Categorytbl.class); criteria.add(Restrictions.eq("parentid", 0)); criteria.addOrder(Order.asc("categoryid")); return criteria.list(); } //Pull-down box--Query category public List queryLeiBie(String ids) throws Exception { int i=Integer.parseInt(ids); Criteria criteria=this.sessionFactory.getCurrentSession().createCriteria(Categorytbl.class); criteria.add(Restrictions.eq("parentid", i)); criteria.addOrder(Order.asc("categoryid")); return criteria.list(); } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }} 7. Corresponding JSP code
<%@ page language="java" errorPage="/error.jsp" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags"%><% String path = request.getContextPath();%><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Information Management</title><!-- Introduce Jquery --><script type="text/javascript" src="<%=path%>/easyui_1.3.2/jquery-1.8.0.min.js" charset="utf-8"></script><!-- Introduce Jquery_easyui --><script type="text/javascript" src="<%=path%>/easyui_1.3.2/jquery.easyui.min.js" charset="utf-8"></script><!-- Introduce easyUi internationalization--Chinese--><script type="text/javascript" src="<%=path%>/easyui_1.3.2/locale/easyui-lang-zh_CN.js" charset="utf-8"></script><!-- Introduce easyUi default CSS format--blue--><link rel="stylesheet" type="text/css" href="<%=path%>/easyui_1.3.2/themes/default/easyui.css" /><!-- Introduce easyUi icon--><link rel="stylesheet" type="text/css" href="<%=path%>/easyui_1.3.2/themes/icon.css" /><script type="text/javascript"> /* Initialize the download table information*/ $(function() { // Select the control in the drop-down box. The content of the drop-down box is to dynamically query the database information $('#lanmu').combobox({ url:'itemManage!categorytbl', editable:false, //No editable state cache: false, panelHeight: 'auto',//Automatic height fit valueField:'categoryid', textField:'categoryName', onHidePanel: function(){ $("#leibie").combobox("setValue",''); var lanmuid = $('#lanmu').combobox('getValue'); $.ajax({ type: "POST", url: "itemManage!leibie?lanmuid="+lanmuid, cache: false, dataType : "json", success: function(data){ $("#leibie").combobox("loadData",data); } }); } }); $('#leibie').combobox({ //url:'itemManage!categorytbl', editable:false, //No editable state cache: false, panelHeight: 'auto',//Automatic height fit valueField:'categoryid', textField:'categoryName' }); });</script></head><body> <!-- Tool block--> <div id="tb" style="padding: 3px"> <form > <span> Column:</span> <select id="lanmu"> </select> <span> Category:</span> <select id="leibie"> </select> </form> </div> </body></html>The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.