在 JSP 中,我可以使用下面的代码从操作类获取 JSON。有人可以告诉我是否有一种方法可以在不使用 Ajax 的情况下从操作类接收响应(JSON)?
$.ajax({
url:"/StrutsExample1/helloWorld.do",
data:params,
type: "post",
dataType:"json",
success:function(response){
$.each(response,function(i){
$.each(response[i],function(key,value){
$('#info').html(key+" "+value);})
})
},
error: function(e){ alert("fail " + e);}
})
更新:
以下代码用于操作类中。
response.setContentType("text/json; charset=utf-8");
JSONObject json = createJsonObject(); //to construct the JSON
PrintWriter out = response.getWriter();
out.print(json);
out.flush();
return mapping.findForward("success");
在 struts-config.xml 中,我在操作映射中有以下内容:
<action path="/helloWorld" type="test.action.HelloWorldAction" name="helloWorldForm">
<forward name="success" path="/index.jsp" />
然后,在 JSP 文件中,而不是像上面那样使用 Ajax 调用,它看起来像:
$(function(response){
$.each(response,function(i){
$.each(response[i],function(key,value){
$('#info').html(key+" "+value);})
})
})
但是,执行时键和值未定义。 JSON 在操作类中正确构造。请帮忙指出如何将JSON传递给jsp。
最佳答案
是的。你可以用正常的方式做。通过提交页面,您可以到达操作类并将您的响应设置为
response.setContentType("application/json");
关于jquery - 将 JSON 从操作类传递到 JSP,而不在 struts 中使用 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19666908/