[英]Render a jsp page from code and get the rendered html output as a string
[英]Get JSON response from AJAX and put in HTML to be rendered in another JSP
我的Web應用程序中有一個jsp。 此jsp在網格中顯示json中的數據。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>ARGO</title>
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="jtable/themes/metro/blue/jtable.min.css"/>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="jtable/jquery.jtable.min.js"></script>
<script type="text/javascript" src="js/argo.js"></script>
<body><div id= "ProductTable"></div>
$('#ProductTable').jtable({
title: 'Table of people',
actions: {
listAction: '/bsnet/test.html'
},
fields: {
productid: {
title: "Organization",
width:"20%"
},
productname: {
title: "Role",
width: "10%"
},
unitcost: {
title: "Status",
width: "20%"
}
}
});
$('#ProductTable').jtable('load');
});
</script>
</html>
我正在使用jtable在網格中顯示內容。
JTable僅需要JSON響應。 它期望以以下格式響應:
{
"Result":"OK",
"Records":[
{"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"}}]}
我的Web服務未將“ Result”:“ OK”作為json響應的一部分發送。
因此,我決定在其間嵌入一個HTML,將其添加到響應中並將其發送到jtable jsp。
This is the HTML that I created:
<html>
<head>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
</head>
<body>
<!-- placing this in the body of the HTML -->
{"Result":"OK","Records":
<script type="text/javascript">
$(document).ready(function(){
// json file that contains the server response
// this will be replaced by a web service URL
$.getJSON("/test/data.json", function(data){
// adding the response to the body
$('body').append("<span>"+JSON.stringify(data)+"</span>}");
});
});
</script>
</body>
</html>
When I refer to the html in the jsp by adding it as part of the URL, I get the entire HTML content like: <html><head>....
but I want only the json response or the body content
{
"Result":"OK",
"Records":[
{"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"}]}
從jsp中的HTML。 那可能嗎?
在瀏覽器中單擊HTML時,可以看到所需的響應。
有幫助嗎???
好的,這是使用JSP腳本的最簡單解決方案:
將其放入包裝JSP中(僅此):
<%@ page import="java.net.URL" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.util.Scanner" %>
{
"Result":"OK",
"Records":
<%
final String yourServiceURL = "your_FULL_Url_with_http_and_stuff";
final String yourServiceEncoding = "UTF-8";
final InputStream stream = new URL(yourServiceURL).openStream();
final String text = new Scanner(stream, yourServiceEncoding ).useDelimiter("\\A").next();
out.write(text);
%>
}
<%
response.setContentType("application/json");
%>
如果從流中讀取失敗,您還可以向該代碼添加一些異常處理以生成“結果:錯誤”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.