public class TestBean {
private boolean booleanProperty = true;
public boolean getBooleanProperty() {
return (booleanProperty);
}
public void setBooleanProperty(boolean booleanProperty) {
this.booleanProperty = booleanProperty;
}
private int intProperty = 123;
public int getIntProperty() {
return (this.intProperty);
}
public void setIntProperty(int intProperty) {
this.intProperty = intProperty;
}
private float floatProperty = (float) 123.0;
public float getFloatProperty() {
return (this.floatProperty);
}
public void setFloatProperty(float floatProperty) {
this.floatProperty = floatProperty;
}
private String stringArray[] =
{ “String 0”, “String 1”, “String 2”, “String 3”, “String 4” };
public String[] getStringArray() {
return (this.stringArray);
}
public void setStringArray(String stringArray[]) {
this.stringArray = stringArray;
}
private String stringProperty = “This is a string”;
public String getStringProperty() {
return (this.stringProperty);
}
public void setStringProperty(String stringProperty) {
this.stringProperty = stringProperty;
}
private Collection beanCollection = null;
public Collection getBeanCollection() {
if (beanCollection == null) {
Vector entries = new Vector(10);
entries.add(“Value 0”);
entries.add(“Value 1”);
entries.add(“Value 2”);
entries.add(“Value 3”);
entries.add(“Value 4”);
entries.add(“Value 5”);
entries.add(“Value 6”);
entries.add(“Value 7”);
entries.add(“Value 8”);
entries.add(“Value 9”);
beanCollection = entries;
}
return (beanCollection);
}
public void setBeanCollection(Collection beanCollection) {
this.beanCollection = beanCollection;
}
}
private boolean booleanProperty = true;
public boolean getBooleanProperty() {
return (booleanProperty);
}
public void setBooleanProperty(boolean booleanProperty) {
this.booleanProperty = booleanProperty;
}
private int intProperty = 123;
public int getIntProperty() {
return (this.intProperty);
}
public void setIntProperty(int intProperty) {
this.intProperty = intProperty;
}
private float floatProperty = (float) 123.0;
public float getFloatProperty() {
return (this.floatProperty);
}
public void setFloatProperty(float floatProperty) {
this.floatProperty = floatProperty;
}
private String stringArray[] =
{ “String 0”, “String 1”, “String 2”, “String 3”, “String 4” };
public String[] getStringArray() {
return (this.stringArray);
}
public void setStringArray(String stringArray[]) {
this.stringArray = stringArray;
}
private String stringProperty = “This is a string”;
public String getStringProperty() {
return (this.stringProperty);
}
public void setStringProperty(String stringProperty) {
this.stringProperty = stringProperty;
}
private Collection beanCollection = null;
public Collection getBeanCollection() {
if (beanCollection == null) {
Vector entries = new Vector(10);
entries.add(“Value 0”);
entries.add(“Value 1”);
entries.add(“Value 2”);
entries.add(“Value 3”);
entries.add(“Value 4”);
entries.add(“Value 5”);
entries.add(“Value 6”);
entries.add(“Value 7”);
entries.add(“Value 8”);
entries.add(“Value 9”);
beanCollection = entries;
}
return (beanCollection);
}
public void setBeanCollection(Collection beanCollection) {
this.beanCollection = beanCollection;
}
}
• Dalam file index.jsp tambahkan skrip EL dan JSTL seperti kode berikut:
<%@page contentType=”text/html”%>
<%@page pageEncoding=”UTF-8″%>
<%@taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Test Using EL & JSTL</title>
</head>
<body>
<%@page contentType=”text/html”%>
<%@page pageEncoding=”UTF-8″%>
<%@taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Test Using EL & JSTL</title>
</head>
<body>
Test EL & JSTL Tag
<jsp:useBean id=”test” scope=”page” class=”jeni.jstl.TestBean”/>
Property intProperty dari class TestBean = ${test.intProperty}
<br/><br/>
Property stringProperty dari class TestBean = ${test.stringProperty}
<br/><br/>
Property booleanProperty dari class TestBean = ${test.booleanProperty}
<br/><br/>
Isi property floatProperty dari class testBean = ${test.floatProperty}
<br/><br/>
Property stringArray dari class TestBean = ${test.stringArray[0]} –
${test.stringArray[1]} – ${test.stringArray[2]}
<br/><br/>
Isi property beanCollection dari class testBean = ${test.beanCollection}
<br/><br/>
Mengakses isi beanCollection dari class testBean dengan syntaks c:forEach
items=”$\{test.beanCollection\}” var=”nes”
<br/>
<c:set var=”count” value=”0″/>
<c:forEach items=”${test.beanCollection}” var=”nes”>
Foreach ke ${count} = ${nes}
<c:choose>
<c:when test=”${count eq 7}”>
—> Baru saja mengeksekusi loop ke-7
</c:when>
<c:otherwise>
—> Mengeksekusi Otherwise
</c:otherwise>
</c:choose>
<c:set var=”count” value=”${count+1}”/>
<br/>
</c:forEach>
</body>
</html>
Tidak ada komentar:
Posting Komentar