虽然你可以很容易地在网上找到这些信息的零碎。让我们把它放在同一页上。
Ans 1::Include the js file of JQuery and Write your ajax call。
$(document).ready(function() {
$.ajax({
type: "POST", //Default is GET
cache : false,
data: sendingData, //Data you need to send if in JSON format
dataType: 'json', //If json is required
contentType: 'application/json; charset=utf-8',
url: "MYACTIONNAME.action", //URL you need to pass
success: function(value) {
alert(value.properties);
},
error: function (xhr, ajaxOptions, thrownError) {
$(".errors").html("Please Try Again");
//console.log(xhr.status + thrownError);
}
});
});
那是为了发送 JSON 并从动作类中检索 JSON。
可以在此处找到 JSON 的配置以及其他答案
答案 2::
$(document).ready(function() {
$("#div").load("MYActionName.action"); //can do that through $.ajax also
});
如果您只想显示加载的 HTML(converted JSP) 的一部分,请使用
$("#divToReplaceWithNewOne").hide().load('MyActionName.action #newDivWhichWillbeFilled').fadeIn(1000); //with animation
Ans 3:: 这完全取决于你想要什么。如果你想要pdf或其他东西,你必须配置。
a) 整个 JSP
<action name="MYActionName" class="MYActionNameBean" method ="execute">
<result name="success" type="dispatcher">
<param name="location">/jsp/MyNewPage.jsp</param>
</result>
</action>
b) 仅通过 ActionClass 处理流和设置内容
<action name="MYActionName" class="MYActionNameBean" method="execute">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
方法::
public String execution() throws Exception {
try{
PrintWriter outWriter = null;
StringBuffer sbf = new StringBuffer("");
HttpServletResponse httpResponse = ServletActionContext.getResponse();
try {
outWriter = httpResponse.getWriter();
sbf.append("String to be sent to View");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(outWriter!=null){
httpResponse.setContentType("text/html");
outWriter.println(sbf.toString());
outWriter.flush();
outWriter.close();
}
}
}catch (Exception e) {
throw new MyOwnException(e);
}
return null;
}
c) 以 JSON 作为结果。(struts2-json 插件)
<action name="MYActionName" class="MYActionNameBean" method="execute">
<result type="json"></result>
</action>
d) PDF
<action name="DownloadPdf" class="PrintPdfActionBean" method="executeViewPdf">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment; filename="${fileName}.pdf"</param> //filename is variable in action class
<param name="bufferSize">4096</param>
</result>
</action>
答::4。这就是所有 JQuery 在成功/完成/延迟回调中,您可以将您的 js 示例片段::
success: function(value) {
var respStat = data.status;
var errorStat = data.errorsMessages;
if(respStat =="success"){
responseMsg.removeClass().addClass('successMessage').html("Your changes have been saved successfully.").fadeOut(4000);
}
else{
responseMsg.removeClass().addClass('errorMessage').html(errorStat[0]).show();
}
},