私は Jforum に基づいて Web サイトを開発していますが、私の Web ページには json リターンが必要です。だから私はこれを満たすサーブレットを作成します。私の問題は、この code() をどこに置くかです -- net.jfourm.util.mine は良い場所ですか? jfourmに合うように変更するにはどうすればよいですか。ありがとう。
package net.jforum.util.mine;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
public class JsonServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public JsonServlet()
{
super();
}
public void init() throws ServletException
{
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
int node = Integer.parseInt(request.getParameter("node"));
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
JSONArray ja = new JSONArray();
ja = this.getMenu(node);
out.println(ja.toString());
}
protected void doGet(HttpServletRequest request,HttpServletResponse response )throws ServletException,IOException
{
doPost(request,response);
}
public JSONArray getMenu(int node)
{
List<MenuBean> list = new ArrayList<MenuBean>();
MenuBean m[][] = new MenuBean[4][3];
MenuBean l0[] = {new MenuBean("1", "service", false, "folder"), new MenuBean("2", "system", false, "folder"), new MenuBean("3", "statistics", false, "folder")};
MenuBean l1[] = {new MenuBean("21000", "aaaa", true, "file", "test/show/arg1/arg2.page"), new MenuBean("22000", "aaaa", true, "file"), new MenuBean("23000", "hhhh", true, "file")};
MenuBean l2[] = {new MenuBean("11000", "cccc", true, "file"), new MenuBean("12000", "eeee", true, "file"), new MenuBean("13000", "gggg", true, "file")};
MenuBean l3[] = {new MenuBean("31000", "bbbb", true, "file"), new MenuBean("32000", "dddd", true, "file")};
m[0] = l0;
m[1] = l1;
m[2] = l2;
m[3] = l3;
for(int i = 0; i < m[node].length; i++){
list.add(m[node][i]);
}
JSONArray ja = JSONArray.fromObject(list);
return ja;
}
}