背景:
使用EasyUI 树控件,加载单位树,需求需要同步加载,开始采用ajax请求数据,这样每次都需要请求一次数据,严重影响性能。现改为 web 启动时获取数据,存入application中,前台页面获取数据加载单位树。
之前代码:
$('#orgTree').tree({ url:'orginfo/searchOrgTree?searchKey='+encodeURIComponent(searchKey), onClick : zTreeOnClick, onDblClick : closeOrgWin, onLoadSuccess:function(){ //找到顶层节点并展开 var rootNode=$('#orgTree').tree('getRoot'); $('#orgTree').tree('expand', rootNode.target); } });
现在代码:
web.xml 中添加:
<servlet> <servlet-name>InitOrgTreeData</servlet-name> <servlet-class>ustcsoft.common.st.InitOrgTreeData</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
对应的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 数据转换错误");
}
}
}
前台页面的处理:
<%
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>
因工作繁忙,略作记录,相对粗糙,方便自己查阅。
同时,欢迎大家指正。