Java 在AJAX响应中返回JSP?
我有一个Action类,它有一个调用Bean的方法,并根据一些输入在DB中设置一些数据 动作类:Java 在AJAX响应中返回JSP?,java,javascript,jquery,jsp,struts2,Java,Javascript,Jquery,Jsp,Struts2,我有一个Action类,它有一个调用Bean的方法,并根据一些输入在DB中设置一些数据 动作类: try{ slsConfigureTspThresholdRemote = this.getSlsConfigureTspThresholdRemote(); slsConfigureTspThresholdRemote.setThresholdParameters(circleId, tspId, thresholdTypeFlag, thresholdParametersList
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;
如果有错误
我返回一个错误。jsp
其中显示操作错误
,如果显示
,则返回相同的输入页面
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>
$.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 ialert('updated DB')
中更新,或者如果异常,那么我希望我的Error.jsp
加载到ErrorDiv
中
所以问题是:
如果我的数据中未发生异常
,则返回ConfigureTspThresholdInput.jsp
,如果存在异常,则在数据中返回Error.jsp
。我应该如何区分数据中有哪些jsp,以便相应地在errorDiv
中加载Error.jsp
我试着在网上搜索。我阅读了JSON插件的statusCode
和ErrorCode
,但我不知道数据部分是如何填充的(我发出警报并得到了一个XML文档)
编辑:从答案来看,我想人们误解了这个问题。让我说清楚
当我说exception时,我并不是说AJAX请求本身无法完成或失败
我的意思是,如果bean中有异常,我将返回error
,相应地,在strust.xml
中,我将返回error.jsp
。
所以我知道,控制将成功实现AJAX请求的
success
。到达那里后,如何处理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!!!");
}
});
我不能百分之百肯定我理解你的问题。我从中得到的是: 如果没有错误并且一切顺利,你希望你的站点提醒“更新的数据库”; 否则您希望您的站点显示错误页面 您应该使用HTTP状态代码来报告请求是成功还是失败。使用5XX状态代码表示服务器错误。() 当ajax请求收到错误状态代码时,它将调用错误函数:
$.ajax({
type: 'POST',
traditional: true,
url: '/gma/updateThresholdParameters.action',
data:
{
circleId: circleId,
tspId: tspId,
thresholdTypeFlag: thresholdTypeFlag,
thresholdParameters: thresholdParameters
},
success: function () {
alert("DB updated")
},
error: function () {
Redirect to your error page instead of returning it.
}
});
我希望这就是您想要的。您应该使用速记来保持代码整洁
$('#OkDiv').load('/gma/updateThresholdParameters.action',{
circleId: circleId,
tspId: tspId,
thresholdTypeFlag: thresholdTypeFlag,
thresholdParameters: thresholdParameters
},function(a,c,xhr){if(c=='error')$('#ErrorDiv').html(xhr.responseHTML);});
请编辑我的问题请编辑我的问题如果有错误,那么就可以了。但是,如果成功,那么这将把我的结果页面加载到我的ErrorDiv,我不想这样。如果你是对的,现在就试试吧。在全局范围内,您最好使用
http://api.jquery.com/ajaxComplete/