使用jquery ajax调用将spring命令输出重定向到spring控制器的textarea

时间:2018-04-10 09:18:52

标签: jquery ajax spring-mvc spring-web

我希望ping命令输出显示在网页上的textarea中。我有我的控制器方法:

@RequestMapping(method = RequestMethod.POST, value = "testInternet.html")
@ResponseBody
public String testInternetConnection() {
    String result = "error";
    String destinationIpAddress = "192.168.1.64";

    //String command = "ping -c 5 ";//for linux
    String command = "ping ";
    String content = "";

    try {

        Process p = Runtime.getRuntime().exec(command+destinationIpAddress);
        BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String s = "";
        while ((s = inputStream.readLine()) != null) {

            //System.out.println("sending "+s);
            //return s;
            content = content + s + System.lineSeparator();
        }

        System.out.println("Result "+content);
        return content;

    } catch (Exception e) {
        result = "error";
        e.printStackTrace();
    }

    return result;
}

我的Ajax jquery调用是这样的

$('#testInternet').on('click', function(event) {
        event.preventDefault();
        console.log("inside test internet Connection");

        $.ajax({
            url : 'testInternet.html',
            type : 'POST',
            processData : false,
            async : false,
            contentType : "application/json; charset=utf-8",
            success : function(result) {
                console.log(result);
                    $('#ip_output').val(result);//setting output from controller to textarea
                    var nodeList = document.querySelectorAll('.mdl-textfield');
            Array.prototype.forEach.call(nodeList,function(elem) {
                    elem.MaterialTextfield.checkDirty();
                });
            },
            error : function(jqXHR, textStatus, errorThrown) {
                alert('error');
            }
        });

    });

这里我现在得到完整的输出现在我想要从控制器逐行输出,以便在textarea中附加,就像它在命令提示符上显示一样。所以任何人都可以帮我解释如何实现这一点。

0 个答案:

没有答案