Java 使用Struts2中的AJAX基于另一个选择菜单填充一个选择菜单
我第一次尝试在Struts2中使用AJAX。因此,我没有确切的想法 有两个s,一个保存在页面加载中加载的国家列表,另一个保存与从国家菜单中选择的国家相对应的国家列表 因此,状态菜单应该在由国家菜单触发的onchange JavaScript事件上初始化 此类JavaScript函数的不完整版本如下所示Java 使用Struts2中的AJAX基于另一个选择菜单填充一个选择菜单,java,jquery,ajax,jsp,struts2,Java,Jquery,Ajax,Jsp,Struts2,我第一次尝试在Struts2中使用AJAX。因此,我没有确切的想法 有两个s,一个保存在页面加载中加载的国家列表,另一个保存与从国家菜单中选择的国家相对应的国家列表 因此,状态菜单应该在由国家菜单触发的onchange JavaScript事件上初始化 此类JavaScript函数的不完整版本如下所示 var timeout; var request; function getStates(countryId) { if(!request) { if(count
var timeout;
var request;
function getStates(countryId)
{
if(!request)
{
if(countryId===""||countryId===null||countryId===undefined||isNaN(countryId))
{
$('#stateList').html("Write an empty <select>");
alert("Please select an appropriate option.");
return;
}
request = $.ajax({
datatype:"json",
type: "GET",
contentType: "application/json",
url: "PopulateStateList.action?countryId="+countryId,
success: function(response)
{
if(typeof response==='object'&&response instanceof Array) //Array or something else.
{
$('#stateList').html(writeResponseSomeWay);
$('#temp').remove();
}
},
complete: function()
{
timeout = request = null;
},
error: function(request, status, error)
{
if(status!=="timeout"&&status!=="abort")
{
alert(status+" : "+error);
}
}
});
timeout = setTimeout(function() {
if(request)
{
request.abort();
alert("The request has been timed out.");
}
}, 300000); //5 minutes
}
}
List<StateTable>stateTables=new ArrayList<StateTable>(); //Getter only.
@Action(value = "PopulateStateList",
results = {
@Result(name=ActionSupport.SUCCESS, location="City.jsp"),
@Result(name = ActionSupport.INPUT, location = "City.jsp")},
interceptorRefs={@InterceptorRef(value="modelParamsPrepareParamsStack", params={"params.acceptParamNames", "countryId", "validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "getStateList"})})
public String getStateList() throws Exception
{
System.out.println("countryId = "+countryId);
stateTables=springService.findStatesByCountryId(countryId);
return ActionSupport.SUCCESS;
}
$('#btnMap').click(function() {
$.get('/TestMVC/AjaxMapServlet', function(responseJson) {
var $select = $('#mapSelect');
$select.find('option').remove();
$('<option>').val("-1").text("Select").appendTo($select)
$.each(responseJson, function(key, value) {
$('<option>').val(key).text(value).appendTo($select);
});
});
})
<input type="button" id="btnMap" name="btnMap" value="Map"/><br/>
<select id="mapSelect" name="mapSelect"><option>Select</option></select>
当在其菜单中选择一个国家并检索作为查询字符串参数提供的countryId时,将调用此方法,但如何返回/映射此列表、状态表以初始化/填充状态菜单
我之前在SpringMVC中与Jackson和Gson一起使用过JSON。例如,使用Gson,为了简单起见,JSON响应可以使用Servlet简单地映射如下
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
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;
@WebServlet(name = "AjaxMapServlet", urlPatterns = {"/AjaxMapServlet"})
public class AjaxMapServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Map<String, String>map=new HashMap<String, String>();
map.put("1", "India");
map.put("2", "America");
map.put("3", "England");
map.put("4", "Japan");
map.put("5", "Germany");
Type type=new TypeToken<Map<String, String>>(){}.getType();
Gson gson=new Gson();
String jsonString=gson.toJson(map, type);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonString);
}
}
将在按下给定按钮btnMap时填充
Struts2呢?如何基于另一个来填充?与Struts2中的方法相同,但是可以使用操作来代替servlet。比如说
@Action(value="/PopulateStateList", results=@Result(type="json", params = {"contentType", "application/json", "root", "map"}))
public class AjaxMapAction extends ActionSupport {
Long countryId; //getter and setter
Map<String, String> map=new HashMap<String, String>();
public Map<String, String> getMap() {
return map;
}
@Override
public String execute() throws Exception {
map.put("1", "India");
map.put("2", "America");
map.put("3", "England");
map.put("4", "Japan");
map.put("5", "Germany");
return SUCCESS;
}
}
现在,您可以在客户端上使用JSON
success: function(response)
{
if(typeof response==='object')
{
var $select = $('#state');
$select.find('option').remove();
$('<option>').val("-1").text("Select").appendTo($select)
$.each(response, function(key, value) {
$('<option>').val(key).text(value).appendTo($select);
}
},
与在Struts2中执行操作的方法相同,但是可以使用操作代替servlet。比如说
@Action(value="/PopulateStateList", results=@Result(type="json", params = {"contentType", "application/json", "root", "map"}))
public class AjaxMapAction extends ActionSupport {
Long countryId; //getter and setter
Map<String, String> map=new HashMap<String, String>();
public Map<String, String> getMap() {
return map;
}
@Override
public String execute() throws Exception {
map.put("1", "India");
map.put("2", "America");
map.put("3", "England");
map.put("4", "Japan");
map.put("5", "Germany");
return SUCCESS;
}
}
现在,您可以在客户端上使用JSON
success: function(response)
{
if(typeof response==='object')
{
var $select = $('#state');
$select.find('option').remove();
$('<option>').val("-1").text("Select").appendTo($select)
$.each(response, function(key, value) {
$('<option>').val(key).text(value).appendTo($select);
}
},