簡體   English   中英

如何從Java servlet中獲取數據並將其放在現有的html頁面上

[英]how to take data from a Java servlet and put it on an existent html page

編輯:我已經閱讀了這個問題“如何使用Servlet和Ajax?” 它談論在同一頁面中生成某些內容,我想將數據從該頁面獲取到servlet,然后重定向到另一個頁面並填充一些現有的文本區域。

嗨,我是stackoverflow的新手,也是編程的新手。 我正在做一個Web應用程序,我必須在其中搜索從html頁到db的內容,這不是問題。

問題是當ajax調用產生數據時,我也必須創建一個按鈕,該按鈕也將我重定向到現有的html頁面,與此同時,按鈕必須將數據發送到另一個頁面中,以加載到servlet(我有已經做到了,它現在生成json,但我不知道如何將其插入到另一頁中),我也不知道如何對servlet說“嘿,從搜索頁面中獲取這點數據,找到記錄並在編輯頁面中加載我想要的數據!”。

我寧願不使用JSP,但如果這樣做是唯一的方法。

這些是搜索功能中的按鈕:

<form>
    <button name="delA" class="btn btn-danger" type="submit" value="' + data[i].key + '" formaction="delete" formmethod="post" onclick="clickedConfirm(event)"></button>
       <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
          <button name="row" class="btn btn-warning" type="submit" value="' + data[i].key + '" href="http://localhost:8080/label-editor/edit.html" formaction="edit" formmethod="get"></button>
       <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
</form>

這是編輯servlet:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {



    LabelManager lm = new LabelManager();
    JSONSerializer js = new JSONSerializer();
    String json = "";

    String paramRow = req.getParameter("row");
    ServletOutputStream out = resp.getOutputStream();

    if(paramRow != null){
    Collection<LabelModel> label = null;                        

    label = lm.searchLabelK(paramRow);

        json = js

                .exclude("*.class")
                .exclude("locale.ISO3Language")
                .exclude("locale.baseLocale")
                .exclude("locale.country")
                .exclude("locale.displayCountry")
                .exclude("locale.displayLanguage")
                .exclude("locale.displayScript")
                .exclude("locale.displayVariant")
                .exclude("locale.language")
                .exclude("locale.localeExtensions")
                .exclude("locale.script")
                .exclude("locale.variant")
                .serialize(label);

        resp.setContentType("application/json");

        out.print(json);
        out.flush();

    }
}

我當時正在考慮像這樣的ajax調用,但是我不知道如何完成它,找不到任何可以幫助您的東西:

function editlabel() {
    $.ajax({
        url : 'edit',
        data : {
            row : getUrlParameter('row')
        },
        success : function(data, status, xhr) {
            $('#edit').submit(function(response) {
                if (data.length > 0) {

                    $('#key').val(data[0].key)

                    console.log("stampo il titolo");

                    for (var i = 1; i < data.key.length; i++) {

                        var row = data[i];

                        $('#val' + i).val(row.value)

                        console.log("ciclo " + row);

                    }

                }
            });

        },
        error : function(req, status, err) {
            console.log('Something went wrong', status, err);
        }
    });
}

謝謝您的時間和幫助ps對不起,我的英語不好

按鈕:

<button title="Modifica label" name="id" class="btn btn-warning" onclick="edit(\'' + data[i].id + '\')">

功能:

function edit(id){
window.location = 'new.html?id=' + id;
};

然后在HTML頁面上的函數onload,我將使用子字符串重定向:

function editlabel() {
var param = window.location.search.substr(1);
if(param != ""){
var k = param.split("id=")[1];
var id = decodeURI(k) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM