亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

更改 Java 的 HTML 輸出

更改 Java 的 HTML 輸出

慕桂英546537 2023-09-18 16:34:55
我有以下代碼:@Controllerpublic class GatesController {    @RequestMapping ("/gates")    public static String qualityGates(String x) throws IOException {        try {            System.out.println("\n------QualityGates------");            URL toConnect = new URL(x);            HttpURLConnection con = (HttpURLConnection) toConnect.openConnection();            System.out.println("Sending 'GET' request to URL : " + x);            BufferedReader in = new BufferedReader(                    new InputStreamReader(con.getInputStream()));            String inputLine;            StringBuffer response = new StringBuffer();            while ((inputLine = in.readLine()) != null) {                response.append(inputLine);            }            in.close();            //Cast the JSON-File to a JSONObject            JSONObject res = new JSONObject(response.toString());            JSONArray gates = new JSONArray(res.getJSONObject("projectStatus").getJSONArray("conditions").toString());            JSONObject test = new JSONObject(res.getJSONObject("projectStatus").toString());            String a = ("\nThe current Project-Status is: " + test.get("status") + "\n");            String b = "";            for (int i = 0; i < gates.length(); i++) {                String status = gates.getJSONObject(i).getString("status");                String metric = gates.getJSONObject(i).getString("metricKey");                b = b + ("<\b>Status: " + status + " | Metric: " + metric);            }            System.out.println(a+b);            return a + b;        } catch (Exception e) {            System.out.println(e);            return String.format("Error");        }    }問題是目前該網站如下所示:網站_當前但我希望每個指標都換行,而不是在一行中。正如您所看到的,我嘗試在字符串內涵處添加 <\b> 您知道如何解決這個問題嗎?這是我的第一個網絡應用程序,我有點卡住了。我感謝每一個幫助!
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

你的“<\b>”打破了它。如果刪除它并添加換行符“\n”,它應該可以工作。像這樣:


String a = ("\nThe current Project-Status is: " + test.get("status") + "\n");

String b = "";

for (int i = 0; i < gates.length(); i++) {

   status = gates.getJSONObject(i).getString("status");

   String metric = gates.getJSONObject(i).getString("metricKey");

   b = b + ("Status: " + status + " | Metric: " + metric + "\n");

}

您還返回純文本。因此,要正確顯示它,請添加“products =”text/plain”以返回格式化的字符串。


@GetMapping(value = "/gates", produces = "text/plain")

然后你的輸出將顯示換行符。這樣您就可以應用進一步的格式。


查看完整回答
反對 回復 2023-09-18
  • 1 回答
  • 0 關注
  • 105 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號