我有一个 Action 类,它具有调用 Bean 并根据某些输入在数据库中设置一些数据的方法。
Action 类:
try{
slsConfigureTspThresholdRemote = this.getSlsConfigureTspThresholdRemote();
slsConfigureTspThresholdRemote.setThresholdParameters(circleId, tspId, thresholdTypeFlag, thresholdParametersList);
}
catch (Exception e){
addActionError(e.getMessage());
e.printStackTrace();
System.out.println("[AnalysisStatisticsAction] updateThresholdParameters: In catch Inside Constructor!!");
return ERROR;
}
return DISPLAY;
如果出现ERROR
,我会返回一个Error.jsp
,其中显示actionError
,如果DISPLAY
,则返回相同的输入页面。
strust.xml:
<action name="updateThresholdParameters"
class="cdot.oss.cmsat.gma.struts.ConfigureTspThresholdAction" method="updateThresholdParameters">
<result name="display">pages/ConfigureTspThresholdInput.jsp</result>
<result name="error">pages/Error.jsp</result>
</action>
我使用 strust2-json-plugin。我正在执行 AJAX 调用。
JS:
$.ajax({
type: 'POST',
traditional: true,
url: '/gma/updateThresholdParameters.action',
data:
{
circleId: circleId,
tspId: tspId,
thresholdTypeFlag: thresholdTypeFlag,
thresholdParameters: thresholdParameters
},
success: function(data){
alert('Updated DB');
},
error:function(data){
alert("something is not fine!!!");
}
});
因此,如果数据在 DB i alert('Updated DB')
中更新,或者出现异常,那么我希望将我的 Error.jsp
加载到 ErrorDiv 中
.
所以问题是:
如果我的data
中没有发生异常,则返回ConfigureTspThresholdInput.jsp
,如果存在异常,则在数据中返回Error.jsp
。我应该如何区分数据中有哪个jsp,以便相应地在errorDiv
中alert('Updated DB')
或加载Error.jsp
? ?
我尝试在网上搜索。我阅读了有关 JSON 插件的 statusCode
和 ErrorCode
的内容,但我不知道数据部分是如何填充的(我发出警报并获得了 XML 文档)。
编辑:从答案来看,我猜人们误解了这个问题。所以让我澄清一下。
当我说异常时,我并不是说 AJAX 请求本身无法完成或失败。
我的意思是,如果 bean 中出现异常,我会返回 error
,相应地,在我的 strust.xml
中,我会返回 Error.jsp
。
所以我知道控制会成功 AJAX 请求。到达那里后如何处理2个JSP?
最佳答案
使用下面的代码来跟踪ajax调用中的异常:
$.ajax({
type: 'POST',
traditional: true,
url: '/gma/updateThresholdParameters.action',
data:
{
circleId: circleId,
tspId: tspId,
thresholdTypeFlag: thresholdTypeFlag,
thresholdParameters: thresholdParameters
},
success: function(data){
alert('Updated DB');
},
error:function(data){
alert("something is not fine!!!");
}
});
关于java - 在 AJAX 响应中返回 JSP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20839044/