簡體   English   中英

如何將數據從Java方法發送到Java腳本函數?

[英]How to send data from Java method to java script function?

我已經使用Java腳本創建了一個彈出窗口。 我想在從Java方法返回的彈出窗口中顯示一個字符串。 我創建了一個控制器並映射了參數。 但是字符串顯示不正確。

這是我的Java腳本函數,它顯示彈出窗口,但未顯示字符串值。

<script>

function myFunction() {

    alert(${random_number});
}
</script>

這是我要獲取生成的字符串的java方法。

public class VerificationCode {

final static int numOfDigits = 4;
String PINString = "";
int randomPIN = 0;

public  String RandomNumber() {

    for(int i = 0; i<numOfDigits; i++){
        randomPIN = (int)(Math.random() * 8) + 1;
        PINString = PINString+String.valueOf(randomPIN);
    }

    return PINString;
}

}

這是我寫的控制器。 我不知道這是否正確,因為我對此經驗不足。

public class PopupController {

@RequestMapping(value = "/register-login", method = RequestMethod.POST)
public String viewUserRegistrationPage(ModelMap model) {
    VerificationCode vc = new VerificationCode();
    String print_val = vc.RandomNumber();
    model.addAttribute("random_number", print_val);
    //return "text";
    return "user-management";
}

}

我想做的是在使用Java腳本函數“ myFunction()”創建的彈出窗口中顯示字符串值“ PINString”。 我將IntelliJ用作我的IDE和JBoss服務器。

編輯:相關的HTML表單

<div class="form-group">
<div class="col-sm-8 col-sm-offset-4 col-md-offset-4" onclick="myFunction()">
<input type="submit" value="Register"
class="btn btn-primary btn-flat w-min-120"
data-toggle="modal" data-target="#myModal"/>                                                          
</div>
 <input type="hidden" id="myInput" value="${random_number}">
 <script>

//When the user clicks on div, open the popup
function myFunction() {
//var str = VerificationCode.RandomNumber();
alert("myInput");
}
   </script>
</div>

我想您需要更新JS函數:

 <script type="text/javascript"> function myFunction(){ var number=document.getElementById("myInput").getAttribute("value"); alert(number); } </script> 

在html頁面中應包含jstl標記並添加表達式:

<c:out value='${random_number}'/>

HTML代碼:

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <div class="form-group"> <div class="col-sm-8 col-sm-offset-4 col-md-offset-4" onclick="myFunction()"> <input type="submit" value="Register" class="btn btn-primary btn-flat w-min-120" data-toggle="modal" data-target="#myModal"/> </div> <input type="hidden" id="myInput" value="<c:out value='${random_number}'/>"> <script> //When the user clicks on div, open the popup function myFunction() { //var str = VerificationCode.RandomNumber(); alert("myInput"); } </script> </div> 

暫無
暫無

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

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