EasyUI Tree tree is loaded synchronously and stored in application

7014 단어 application
background:
Use the EasyUI tree control to load the unit tree, the requirements need to be loaded synchronously, and start to use ajax to request data, so you need to request data every time, which seriously affects the performance. Now it is changed to obtain data when the web starts, store it in the application, and the foreground page obtains the data loading unit tree.
 
 
Code before:
 
$('#orgTree').tree({
		        url:'orginfo/searchOrgTree?searchKey='+encodeURIComponent(searchKey),
			onClick : zTreeOnClick,
			onDblClick : closeOrgWin,
			onLoadSuccess:function(){
				// 
				var rootNode=$('#orgTree').tree('getRoot');
				$('#orgTree').tree('expand', rootNode.target);
			}
		});
 
Code now:
 
Add to web.xml:
  <servlet>  
        <servlet-name>InitOrgTreeData</servlet-name>  
        <servlet-class>ustcsoft.common.st.InitOrgTreeData</servlet-class>  
        <load-on-startup>1</load-on-startup>  
  </servlet>
 
Corresponding Servlet:
package ustcsoft.common.st;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import ejb.common.st.Ejb3Factory;
import ejb.orginfo.st.QtyCOrganizationFacadeLocal;
import ejb.util.st.ComBoxTreeNode;

public class InitOrgTreeData extends HttpServlet {

	//EJB 
	public static Ejb3Factory factory;
	
	protected static Log logger = LogFactory.getLog(HttpServlet.class); 
	
	static {
		factory = Ejb3Factory.getInstance();
	}
	
	/**
	 * Constructor of the object.
	 */
	public InitOrgTreeData() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		
		logger.info("Get ORG TREE DATA HERE!");
		
		String searchKey="";
		List<ComBoxTreeNode> list = new ArrayList<ComBoxTreeNode>();
		QtyCOrganizationFacadeLocal OrgLocal = (QtyCOrganizationFacadeLocal) factory
				.getEJB("QtyCOrganizationFacade");
		List orgList = OrgLocal.findSearchOrgTree(searchKey);
		list=CommDataUtil.buildNodeTree(orgList);

		try {
			String result = JSONUtil.serialize(list);
			// application
			this.getServletContext().setAttribute("OrgData",result);
		} catch (JSONException e) {
		    logger.error("InitOrgTreeData  ");
		}
		
	}

}
 
Processing of the front page:
 
<%			
    String orgData=(String)application.getAttribute("OrgData");			
			
%>

<script language="javascript">

// , :var orgData='<%=orgData%>'
             var orgData=<%=orgData%>;
			$('#orgTree').tree({
 // ,url--->data
		//	url:'orginfo/searchOrgTree
                      ?searchKey='+encodeURIComponent(searchKey),
			data:orgData,
			onClick : zTreeOnClick,
			onDblClick : closeOrgWin,
			onLoadSuccess:function(){
				// 
				var rootNode=$('#orgTree').tree('getRoot');
				$('#orgTree').tree('expand', rootNode.target);
			}
		});
</script>
 
Due to the busy work, I made a little record, which is relatively rough, so it is convenient for my own reference.
At the same time, any corrections are welcome.

좋은 웹페이지 즐겨찾기