私は、Javaスクリプトを使用してポップアップウィンドウを作成しました。私はJavaメソッドから返されるそのポップアップウィンドウに文字列を表示したい。私はコントローラを作成し、パラメータをマップしました。しかし、文字列が正しく表示されていません。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";
}
}
ポップアップウィンドウに文字列値「PINString」を表示されているJavaスクリプト機能「のMyFunctionを()」を使用して作成しました。 IDEとJBossサーバーとしてIntelliJを使用します。
に編集: 関連する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>
'hidden' HTML要素の値 - –
@ScaryWombat明確ではない、この値を読み込み、表示するために使用jqueryのまたはJavaScript。どの値を格納すればいいですか – sndu
'model.addAttribute(" random_number "、print_val);' JSTLを '$ {random_number} 'のように使用します。 –