【问题标题】:Spring MVC - Set\Populate the content of a JSTL\JSP object with a Japascript object [duplicate]Spring MVC - 使用 Japascript 对象设置\填充 JSTL\JSP 对象的内容 [重复]
【发布时间】:2015-12-10 13:18:52
【问题描述】:

我想在 Spring MVC 项目中通过 javascript 对象设置 jstl/jsp 对象的值,这个问题可能已经提出,但我在网上找不到任何满意的答案,我刚开始使用Spring MVC 所以也许这是一个愚蠢的问题,简而言之,我想用 Array/Object javascript 的内容填充 jstl ModelAtrribute 集合的内容。

这些是我想要使用的 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 对象“markerLis”t“发送”到 Spring MVC 的控制器 ModelAttribute“MarkerList”?

注意:我知道我可以像 json 响应一样发布 javascript 对象,并使用 Spring MVC 的“RequestBody”和“ResponseBody”注释来接收 json 响应。

更新说明:所以没有任何 ajax 调用(我知道我很伤自己)。

我问这个问题只是出于好奇。

Ty 提前。

【问题讨论】:

  • 您不能直接执行此操作,因为 JSP 在服务器端进行评估,而 HTML+JS 在用户浏览器中进行评估。

标签: javascript spring jsp spring-mvc jstl


【解决方案1】:

您可以进行 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>

【讨论】:

  • Ty 为了快速响应,抱歉我忘了提到我需要找到没有任何 ajax 调用的解决方案的问题,我只是想知道是否存在一种将 javascript 对象“投射”到的方法一个 JSTL 对象并在 Spring 的“模型属性”上获取结果。
  • K,这是我可以接受的解决方案,但可以肯定的是,我无法直接在 JSTL 对象 "${fullname}" 上设置 "username" 字段,如下所示:$ {fullname.username} = "新值";用javascript代码?
  • 如果表单中已经存在字段,您可以使用 *** document.getElementById("id").value("new value"); *** 如果你使用 JQuery *** $( "#id" ),val("new value"); ***w3schools.com/jsref/met_doc_getelementbyid.asp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 2019-04-11
相关资源
最近更新 更多