我试图通过javascript创建一个变量,并在我的spring mvc控制器中使用该变量。不幸的是,我似乎无法弄明白,变量一直空着。
这是我设置变量的javascript:
<script language="JavaScript">
function checkBoxValues() {
checkboxes = document.getElementsByName('message');
var hopeThisWorks = "";
for (var i = 0, n = checkboxes.length; i < n; i++) {
if (checkboxes[i].checked == true) {
message = checkboxes[i].value;
var element = message.split(",");
resubmitMessage(element[0], "" + element[1] + "",element[2]);
var hopeThisWorks = "" + element[2] +"";
alert(hopeThisWorks);
}
}
}
</script>
然后我把这个变量添加到我的jsp作为输入,如下所示:
<input name="abc" type="hidden" value="${hopeThisWorks}">
这是我在spring mvc控制器中的代码:
@RequestMapping(method = RequestMethod.POST, value = "resubmit")
public String resubmit(@RequestParam(value = "abc") String param, Model model) {
model.addAttribute("messageList", param);
return RESUBMIT;
}
然后我调用jsp文件中的变量来显示消息,如下所示:
<table class="results table table-striped table-bordered table-condensed">
<thead>
<tr>
<td colspan="10">
<h5>Bulk Resubmit Query Results</h5>
</td>
</tr>
<tr>
<jsp:include page="../commons/a.jsp"/>
<jsp:include page="../commons/b.jsp"/>
<jsp:include page="../commons/c.jsp"/>
<jsp:include page="../commons/d.jsp"/>
<jsp:include page="../commons/e.jsp"/>
<jsp:include page="../commons/f.jsp"/>
<jsp:include page="../commons/g.jsp"/>
<jsp:include page="../commons/h.jsp"/>
</tr>
<p>Test <c:out value="${messageList}"/></p>
答案 0 :(得分:0)
您可以直接从javascript函数设置输入值。最好将id属性添加到输入:
<input id="abc" name="abc" type="hidden" value="">
然后在你的javascript函数中:
<script language="JavaScript">
function checkBoxValues() {
checkboxes = document.getElementsByName('message');
var hopeThisWorks = "";
for (var i = 0, n = checkboxes.length; i < n; i++) {
if (checkboxes[i].checked == true) {
message = checkboxes[i].value;
var element = message.split(",");
resubmitMessage(element[0], "" + element[1] + "",element[2]);
var hopeThisWorks = "" + element[2] +"";
document.getElementById("abc").value = hopeThisWorks;
}
}
}
</script>
现在变量将在提交表单时发送。
答案 1 :(得分:0)
在客户端和服务器端之间传递变量(或数据)的好方法是使用Ajax请求。 即使你在.jsp中,也可以使用ajax(使用Jquery或Angular它更容易)。这是一个例子:
function foo(){
var data = '{"key":"'+value+'"}'; // here you create your json containing the var that you want to send to your server
$.ajax({
type : "POST",
contentType : "application/json",
url : "/theLocalisation", // here you put the "Url" of your spring mvc controller
data : data,
timeout : 100000,
success : function(data) {
var json = JSON.parse(data);
//here it's the callback in the success case
},
error : function(e) {
console.log("ERROR: ", e);
},
done : function(e) {
}
});
}
服务器端:
@RequestMapping(value = "/theLocalisationn")
public String getTheStation(@RequestBody String data){
// here you will receive your
JSONObject jsonObj = new JSONObject(search); //transform the string in JSON to use it.
System.out.println(jsonObj);
//then do your magic here.
return null;
}
PS:我使用lib org.json和Jquery
希望这个例子能帮到你
答案 2 :(得分:0)
如果不发送HTTP请求,则无法设置controller属性的值, 因此,在您加载页面后,您可以通过控制器发送ajax请求处理它并修改属性(在这种情况下,您将无法在刷新之前显示新值),或者在设置值后将其发送到输入中使用JavaScript的输入。
<input name="abc" type="hidden" id="myinput" value="${hopeThisWorks}">
<script language="JavaScript">
function checkBoxValues() {
checkboxes = document.getElementsByName('message');
var hopeThisWorks = "";
for (var i = 0, n = checkboxes.length; i < n; i++) {
if (checkboxes[i].checked == true) {
message = checkboxes[i].value;
var element = message.split(",");
resubmitMessage(element[0], "" + element[1] + "",element[2]);
var hopeThisWorks = "" + element[2] +"";
$("#myinput").attr("value",hopeThisWorks);
alert(hopeThisWorks);
}
}
}
</script>