我正在开发一个简单的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上进行循环?
这是我的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);
}
}
-----------------------------------
答案 0 :(得分:1)
我不太确定我完全理解你想要达到的目标。
如果您只需要使用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)