jsp上的java Struts 2动作响应
我想在jsp页面上获取操作的HTTPServletResponse,但我不知道如何
我对jsp的ajax调用:
jQuery.ajax({
url : 'actionURL',
type: "POST",
data : {input: "example"},
dataType: "html",
success: function(response) {
alert(response);
if (response == 1) {
jQuery("#message").html("done!");
}
}
});
我的行动课:
public class MyAction implements ServletResponseAware {
public final String execute() throws IOException {
String return_code = "1";
try {
something...
} catch (Exception e) {
return_code = "0";
}
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(return_code);
return "success";
}
}
如何仅访问jsp上的return_code变量?因为现在在脚本中警报整个页面的HTML代码作为响应。。。谢谢
# 1 楼答案
returnCode
而不是return_code
),并且可能是一个整数,因为它包含一个数字李>永远不要接受例外
在struts中。xml,将
"success"
结果映射到只包含打印出的值的JSP:然后返回的JSP将是单个值。但这是一种糟糕的做法,很容易导致错误(例如,额外的空格)。然后改变你的方法
使用Struts2 JSON plugin设置
returnCode
作为root
对象(记住将dataType
从html
更改为json
)