gpt4 book ai didi

javascript - 在jsp页面中循环对象列表

转载 作者:行者123 更新时间:2023-12-03 11:37:54 25 4
gpt4 key购买 nike

我正在开发一个简单的 spring mvc 应用程序,它填充一个基于另一个下拉框的下拉框。我正在将我的 pojo 类发送到 jsp 页面 - 'info' 。其中包含对象列表。

我想在第一个列表的基础上使用列表的数据填充第二个列表。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<title>HELLO WORLD</title>
</head>
<body>
<script>
$(document).ready(function(){


$("#selectApi").change(function(){
var x =$("#selectApi").val();
var privateMeth = '${apiInfo.getPrivateApiMethods()}';
var publicMeth = '${apiInfo.getPublicApiMethods()}';
if(x=="privateApi"){
$("#selectFunction").append(new Option('${apiInfo.getPrivateApiMethods().get(0).getMethodName()}', "val"))
}

**for(i in '${publicMeth}){
alert(i)

}**
alert("end")

});
});




</script>

First name: <input type="text" name="fname" value=${privateMethods}><br>
<select id ="selectApi">
<option>----select an api------</option>
<option value="privateApi">PrivateAPI</option>
<option value="publicApi">PublicAPI</option>
</select>
<br/>
<select id="selectFunction">
<option>----select a function------</option>
</select>


</body>
</html>

我如何在 privateMeth 上进行 for 循环?

这是我的 pojo 类

public class ApiInfo {

List<PrivateApiMethod> privateApiMethods;

List<PublicApiMethod> publicApiMethods;

public List<PrivateApiMethod> getPrivateApiMethods() {
return privateApiMethods;
}

public void setPrivateApiMethods(List<PrivateApiMethod> privateApiMethods) {
this.privateApiMethods = privateApiMethods;
}

public List<PublicApiMethod> getPublicApiMethods() {
return publicApiMethods;
}

public void setPublicApiMethods(List<PublicApiMethod> publicApiMethods) {
this.publicApiMethods = publicApiMethods;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}


------------------------

public class PrivateApiMethod {

String methodName;
String methodType;
String numOfParam;

public String getMethodName() {
return methodName;
}

public void setMethodName(String methodName) {
this.methodName = methodName;
}

public String getMethodType() {
return methodType;
}

public void setMethodType(String methodType) {
this.methodType = methodType;
}

public String getNumOfParam() {
return numOfParam;
}

public void setNumOfParam(String numOfParam) {
this.numOfParam = numOfParam;
}


@Override
public String toString() {
return ToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}
}
-----------------------------------

最佳答案

我不太确定我是否完全理解您想要实现的目标。

如果您只需要使用 getter 方法的返回值填充列表,那么您可以使用如下内容:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="apiinfo" class="your.package.ApiInfo" />
<c:forEach var="method" items="${apiinfo.privateApiMethods}">
<option>${method.methodName}</option>
</c:forEach>

进一步引用: http://www.tutorialspoint.com/jsp/jsp_java_beans.htm (从 JSP 访问 Java Bean)

how to Use <c:forEach> in scripts tag on JSP page? (JSP中foreach的使用)

关于javascript - 在jsp页面中循环对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26404718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com