我想在Spring MVC项目中用javascript对象设置jstl/jsp对象的值,这个问题可能已经提出了,但我在网上找不到任何令人满意的答案,我刚刚开始使用Spring MVC,所以这可能是一个愚蠢的问题,简而言之,我想用Array/Object javascript的内容填充jstl ModelAtribute集合的内容。
以下是我想要使用的java类:
Marker.java
public class Marker {
private String name;
private String url;
private String latitude;
private String longitude;
private String popupContent;
public Marker(){}
public Marker(String name,String url,String latitude,String longitude,String popupContent){
this.name = name;
this.url = url;
this.latitude = latitude;
this.longitude = longitude;
this.popupContent = popupContent;
}
//Getter and setter
}
MarkerList.java
@JsonIgnoreProperties(ignoreUnknown = true)
public class MarkerList implements Serializable {
List<Marker> markers;
public List<Marker> getMarkers() {
return markers;
}
public void setMarkers(List<Marker> markers) {
this.markers= markers;
}
}
ControllerMarker.java
@Controller
@PreAuthorize("hasRole('ROLE_USER')")
public class MarkerController {
//Autowired and setting of all other variables.
//Call the page JSP.
@RequestMapping(value="/map",method= RequestMethod.GET)
public String loadMap2(Model model){
model.addAttribute("markerList",markerList);
return "riconciliazione2/mappa/leafletMap";
}
@RequestMapping(value="/map3",method = RequestMethod.POST)
public String result4(@RequestParam(required=false, value="urlParam")String url,
@ModelAttribute(value="markerList") MarkerList arrayParam
){
//I want to find a object MarkerList populate with some Marker.
return "redirect:/map13";
}
Test.jsp
//some code html....
<script>
markerList = [];
marker = {name:'test',url:'test',latitude:'test',longitude:'test',popupContent:'test'};
markerList.push(marker);
<!--How populate the JSTL "markerList" variable??????, i want if possible doing something like this. -->
var index = 0;
for(var mark in markerList){
<c:set var="${marker.name}" scope="session" value="${mark.name.toString()}"/>
<!-- Other parameter -->
<c:out value="${marker.name}" />
<!-- Other parameter -->
<!-- How can i add the new Marker with the value setted ???-->
${markerList}.add(${marker})
}
</script>
<!-- I want to use the new content of the markerList object with some operation -->
<c:forEach items="${markerList}" var="idMarker">
<input id="nameForm" name="nameParam" type="hidden" value="<c:out value="${idMarker.name}" />"/>
</c:forEach>
<c:url var="url" value="/map3" />
<form:form action="${url}" method="post" id="/map3" modelAttribute="markerList">
<form:input type="hidden" name="markerList" value=${markerList}/>
<input type="text" name="urlParam" value="" title="urlParam"/>
<input type="submit" name="urlFormParam" value="urlForm" />
</form:form>
<c:url var="url" value="/map3" />
<form:form action="${url}" method="post" id="/map3" modelAttribute="markerList">
<form:input type="hidden" name="markerList" value=${markerList}/>
<input type="text" name="urlParam" value="" title="urlParam"/>
<input type="submit" name="urlFormParam" value="urlForm" />
</form:form>
//some code html....
如何将javascript对象"markrLis"t"发送"到Spring MVC的控制器ModelAttribute"MarkerList"?
注意:我知道我可以像json响应一样发布javascript对象,并使用Spring MVC的"RequestBody"one_answers"ResponseBody"注释来接收json响应。
更新注意:所以没有任何ajax调用(我知道我很受伤)。
我问这个问题只是为了好奇。
提前Ty。
您可以进行ajax调用。
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var response = JSON.parse(xmlhttp.responseText);
...
//process response code
...
}
}
xmlhttp.open("GET", "URL of spring", true);
xmlhttp.send();
如果您使用jquery:
$.ajax({
type: "GET",
url: "URL from spring",
success: function( response ){
...
// Code to process the response
...
}
});
更新
你可以动态生成一个表单,然后提交它。或者添加到HTML 的主体中
<script>
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");
var i = document.createElement("input"); //input element, text
i.setAttribute('type',"text");
i.setAttribute('name',"username");
var s = document.createElement("input"); //input element, Submit button
s.setAttribute('type',"submit");
s.setAttribute('value',"Submit");
f.appendChild(i);
f.appendChild(s);
//and some more input elements here
//and dont forget to add a submit button
document.getElementsByTagName('body')[0].appendChild(f);
</script>