权限控制也很简单,这里没有跟数据库相连。
以下是Server端代码,为了Demo合在了一个文件中,正式应用完全可以分开。
程序生成的Json Tree与EasyUI的Tree完全无缝吻合
package com.baosight.imap.rest;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.baosight.imap.rest.domain.JsonSession;
import com.baosight.imap.rest.domain.TreeNode;
import com.baosight.imap.json.core.JsonParseException;
import com.baosight.imap.json.databind.JsonMapper;
import com.baosight.imap.json.databind.JsonMappingException;
import com.baosight.imap.rest.domain.LoginInfo;
/**
* Servlet implementation class IMapService
*/
@WebServlet("/iMapService")
public class IMapService extends HttpServlet {
private static final long serialVersionUID = 1L;
private JsonMapper jsonMapper = new JsonMapper();
private Map<String, JsonSession> session = new HashMap<String, JsonSession>();
/**
* @see HttpServlet#HttpServlet()
*/
public IMapService() {
super();
// TODO Auto-generated constructor stub
}
private TreeNode[] getTestTree() throws JsonParseException,
JsonMappingException, IOException {
String tree = "[{\"id\":-1,\"state\":\"open\",\"text\":\"所有图层\",\"iconCls\":\"\",\"children\":[{\"id\":0,\"state\":\"open\",\"text\":\"灌溉管道\",\"iconCls\":\"\",\"children\":[{\"id\":1,\"state\":\"open\",\"text\":\"灌溉管道井\",\"iconCls\":\"icon-legend1\",\"children\":[],\"checked\":true},{\"id\":2,\"state\":\"open\",\"text\":\"灌溉管道\",\"iconCls\":\"icon-legend2\",\"children\":[],\"checked\":true},{\"id\":3,\"state\":\"open\",\"text\":\"灌溉延伸管道\",\"iconCls\":\"icon-legend3\",\"children\":[],\"checked\":true},{\"id\":4,\"state\":\"open\",\"text\":\"灌溉井\",\"iconCls\":\"icon-legend4\",\"children\":[],\"checked\":true}],\"checked\":true},{\"id\":5,\"state\":\"open\",\"text\":\"信息管道\",\"iconCls\":\"\",\"children\":[{\"id\":6,\"state\":\"open\",\"text\":\"信息管道井\",\"iconCls\":\"icon-legend6\",\"children\":[],\"checked\":true},{\"id\":7,\"state\":\"open\",\"text\":\"信息管道\",\"iconCls\":\"icon-legend7\",\"children\":[],\"checked\":true}],\"checked\":true},{\"id\":8,\"state\":\"open\",\"text\":\"上水管道\",\"iconCls\":\"\",\"children\":[{\"id\":9,\"state\":\"open\",\"text\":\"上水管道\",\"iconCls\":\"icon-legend9\",\"children\":[],\"checked\":true},{\"id\":10,\"state\":\"open\",\"text\":\"上水红井\",\"iconCls\":\"icon-legend10\",\"children\":[],\"checked\":true},{\"id\":11,\"state\":\"open\",\"text\":\"上水黑井\",\"iconCls\":\"icon-legend11\",\"children\":[],\"checked\":true},{\"id\":12,\"state\":\"open\",\"text\":\"上水红井线\",\"iconCls\":\"icon-legend12\",\"children\":[],\"checked\":true},{\"id\":13,\"state\":\"open\",\"text\":\"上水黑井线\",\"iconCls\":\"icon-legend13\",\"children\":[],\"checked\":true}],\"checked\":true},{\"id\":14,\"state\":\"open\",\"text\":\"燃气管道\",\"iconCls\":\"\",\"children\":[{\"id\":15,\"state\":\"open\",\"text\":\"燃气管道\",\"iconCls\":\"icon-legend15\",\"children\":[],\"checked\":true},{\"id\":16,\"state\":\"open\",\"text\":\"燃气管道井\",\"iconCls\":\"icon-legend16\",\"children\":[],\"checked\":true},{\"id\":17,\"state\":\"open\",\"text\":\"燃气管道\",\"iconCls\":\"icon-legend17\",\"children\":[],\"checked\":true}],\"checked\":true},{\"id\":18,\"state\":\"open\",\"text\":\"电力管道\",\"iconCls\":\"\",\"children\":[{\"id\":19,\"state\":\"open\",\"text\":\"电力管道\",\"iconCls\":\"icon-legend19\",\"children\":[],\"checked\":true},{\"id\":20,\"state\":\"open\",\"text\":\"电力管道井\",\"iconCls\":\"icon-legend20\",\"children\":[],\"checked\":true}],\"checked\":true},{\"id\":21,\"state\":\"open\",\"text\":\"污水管道\",\"iconCls\":\"\",\"children\":[{\"id\":22,\"state\":\"open\",\"text\":\"污水管道井外框\",\"iconCls\":\"icon-legend22\",\"children\":[],\"checked\":true},{\"id\":23,\"state\":\"open\",\"text\":\"污水管道园井\",\"iconCls\":\"icon-legend23\",\"children\":[],\"checked\":true},{\"id\":24,\"state\":\"open\",\"text\":\"污水管道方井\",\"iconCls\":\"icon-legend24\",\"children\":[],\"checked\":true},{\"id\":25,\"state\":\"open\",\"text\":\"污水管道\",\"iconCls\":\"icon-legend25\",\"children\":[],\"checked\":true}],\"checked\":true},{\"id\":26,\"state\":\"open\",\"text\":\"雨水管道\",\"iconCls\":\"\",\"children\":[{\"id\":27,\"state\":\"open\",\"text\":\"雨水管道井\",\"iconCls\":\"icon-legend27\",\"children\":[],\"checked\":true},{\"id\":28,\"state\":\"open\",\"text\":\"雨水管道其他井\",\"iconCls\":\"icon-legend28\",\"children\":[],\"checked\":true},{\"id\":29,\"state\":\"open\",\"text\":\"雨水管道\",\"iconCls\":\"icon-legend29\",\"children\":[],\"checked\":true},{\"id\":30,\"state\":\"open\",\"text\":\"雨水管道线\",\"iconCls\":\"icon-legend30\",\"children\":[],\"checked\":true}],\"checked\":true},{\"id\":31,\"state\":\"open\",\"text\":\"基础地图\",\"iconCls\":\"\",\"children\":[{\"id\":32,\"state\":\"open\",\"text\":\"地块边界\",\"iconCls\":\"icon-legend32\",\"children\":[],\"checked\":true},{\"id\":33,\"state\":\"open\",\"text\":\"西入口高速\",\"iconCls\":\"icon-legend33\",\"children\":[],\"checked\":true},{\"id\":34,\"state\":\"open\",\"text\":\"周边交通\",\"iconCls\":\"icon-legend34\",\"children\":[],\"checked\":true},{\"id\":35,\"state\":\"open\",\"text\":\"内湖\",\"iconCls\":\"icon-legend35\",\"children\":[],\"checked\":true},{\"id\":36,\"state\":\"open\",\"text\":\"围场河\",\"iconCls\":\"icon-legend36\",\"children\":[],\"checked\":true},{\"id\":37,\"state\":\"open\",\"text\":\"园区外围河道\",\"iconCls\":\"icon-legend37\",\"children\":[],\"checked\":true}],\"checked\":true}],\"checked\":true}]";
return jsonMapper.readValue(tree, TreeNode[].class);
}
protected void doLogin(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
LoginInfo info = jsonMapper.readValue(request.getInputStream(),
LoginInfo.class);
if (info.getLoginName().equals("admin")
&& info.getPassword().equals("admin")) {
JsonSession sess = new JsonSession(10);
String sessionID = sess.getSessionID();
session.put(sessionID, sess);
jsonMapper.writeValue(response.getOutputStream(), sessionID);
} else {
String error = "";
jsonMapper.writeValue(response.getOutputStream(), error);
}
}
protected void doList(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String[] serviceList = new String[4];
serviceList[0] = "login";
serviceList[1] = "logout";
serviceList[2] = "list";
serviceList[3] = "tree";
PrintWriter writer = response.getWriter();
jsonMapper.writeValue(writer, serviceList);
}
protected void doLogout(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String sessionID = jsonMapper.readValue(request.getInputStream(),
String.class);
if (session.containsKey(sessionID)) {
session.remove(sessionID);
}
}
protected void doTree(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String sessionID = jsonMapper.readValue(request.getInputStream(),
String.class);
if (session.containsKey(sessionID)) {
// TODO Auto-generated method stub
TreeNode[] aTree = getTestTree();
PrintWriter writer = response.getWriter();
jsonMapper.writeValue(writer, aTree);
}
}
protected void doProcess(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String sessionID = jsonMapper.readValue(request.getInputStream(),
String.class);
if (session.containsKey(sessionID)) {
JsonSession sess = session.get(sessionID);
if (!sess.isExpired()) {
long duration = sess.getDuration();
PrintWriter writer = response.getWriter();
jsonMapper.writeValue(writer, duration);
}
}
}
protected void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/plain;charset=utf-8");
}
String action = request.getParameter("action");
if (action != null) {
if (action.equalsIgnoreCase("login")) {
doLogin(request, response);
} else if (action.equalsIgnoreCase("logout")) {
doLogout(request, response);
} else if (action.equalsIgnoreCase("list")) {
doList(request, response);
} else if (action.equalsIgnoreCase("tree")) {
doTree(request, response);
} else {
doProcess(request, response);
}
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
process(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
process(request, response);
}
}