[英]Struts 2 action response on jsp
我想在jsp頁面上獲取Action的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代碼作為響應...謝謝!
returnCode
而不是return_code
),並且可能是Integer,因為它包含一個數字; 永遠不要吞下例外;
public class MyAction implements ServletResponseAware { private Integer returnCode; public Integer getReturnCode(){ return returnCode; } public final String execute() throws IOException { try { /* do something... */ returnCode = 1; } catch (Exception e) { e.printStackTrace(); returnCode = 0; } return "success"; } }
在struts.xml中,將"success"
結果映射到僅包含打印輸出值的JSP:
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="struts-tags.tld"%> <s:property value="returnCode" />
然后返回的JSP將是單個值。 但這是不好的做法,很容易導致錯誤(例如額外的空格)。 然后改變你的方法......
使用Struts2 JSON插件將returnCode
設置為root
對象(記得將dataType
從html
更改為json
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.