この情報の断片はネット上で簡単に見つけることができますが。同じページに入れましょう。
回答 1::JQuery の js ファイルをインクルードし、ajax 呼び出しを記述します。
$(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 (変換された JSP) の一部のみを表示する場合は、
$("#divToReplaceWithNewOne").hide().load('MyActionName.action #newDivWhichWillbeFilled').fadeIn(1000); //with animation
回答 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();
}
},