Reputation: 168
I am working on a simple spring mvc application which populates a dropdown box based on another. I am sending my pojo class to jsp page - 'info' . which contains list of objects .
I want to populate the 2nd list bases on the first list with the data of the list .
<!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>
How can i do for loop on privateMeth ?
here is my pojo classes
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);
}
}
-----------------------------------
Upvotes: 0
Views: 1340
Reputation: 1065
I'm not quite sure I understand completely what you are trying to achieve.
If you just need to populate the list with the return value from a getter Method, then you could use something like this:
<%@ 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>
for further reference: http://www.tutorialspoint.com/jsp/jsp_java_beans.htm (Access Java Beans from JSP)
how to Use <c:forEach> in scripts tag on JSP page? (Use of foreach in JSP)
Upvotes: 1