「struts2開発」ajaxでjsonデータを取得して表示する方法
1.struts2.xml
<package name="jsonaction" namespace="/" extends="json-default">
<action name="getlist" class="com.changfa.action.ChangAction" method="getlist">
<result type="json"><param name="root">result</param></result>
</action>
</package>
2.Javaコード
package com.changfa.action;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
public class ChangAction implements Action{
private String result;
public String getlist(){
Map<String,String> chatroom = new HashMap<String,String>();
StringBuffer roomlist = new StringBuffer(“room");
StringBuffer memberlist = new StringBuffer(“member");
chatroom.put(“roomlist", roomlist.toString());
chatroom.put(“memberlist", memberlist.toString());
JSONObject jo = JSONObject.fromObject(chatroom);
this.result = jo.toString();
return SUCCESS;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}