I have problem with populating select values using Struts 2. It is simple sample application that I am writing with purpose to learn Struts 2. Notice part:
<s:select label="Select owner" name="owners" headerKey="-1" headerValue="-- Please Select --" list="persons" listValue="firstName" listKey="id" emptyOption="true" />
I have list persons (java.util.List<Person>) in mystuff.action.PersonActio
n.
This doesn't work, causing error about list location. Exact question:
How to bind any list (in s:select tag) with some list in some class. I would appriciate some example please.
Here is part of code that I use:
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@page import="mystuff.action.Per
sonAction"
%>
<%@page import="mystuff.service.Pe
rsonServic
eImpl"%>
<html>
<head>
<s:head theme="ajax" debug="true"/>
<script type="text/javascript">
dojo.event.topic.subscribe
("/savePer
son", function(data, type, request) {
if(type == "load") {
dojo.byId("personId").valu
e = "";
dojo.byId("firstName").val
ue = "";
dojo.byId("lastName").valu
e = "";
dojo.byId("email").value = "";
}
});
dojo.event.topic.subscribe
("/editPer
son", function(data, type, request) {
if(type == "before") {
var id = data.split("_")[1];
var tr = dojo.byId("personRow_"+id)
;
var tds = tr.getElementsByTagName("t
d");
dojo.byId("personId").valu
e = id;
dojo.byId("firstName").val
ue = dojo.string.trim(dojo.dom.
textConten
t(tds[0]))
;
dojo.byId("lastName").valu
e = dojo.string.trim(dojo.dom.
textConten
t(tds[1]))
;
dojo.byId("email").value = dojo.string.trim(dojo.dom.
textConten
t(tds[2]))
;
}
});
dojo.event.topic.subscribe
("/saveBoo
k", function(data, type, request) {
if(type == "load") {
dojo.byId("bookId").value = "";
dojo.byId("title").value = "";
dojo.byId("author").value = "";
dojo.byId("pagesNo").value
= "";
}
});
dojo.event.topic.subscribe
("/editBoo
k", function(data, type, request) {
if(type == "before") {
var id = data.split("_")[1];
var tr = dojo.byId("bookRow_"+id);
var tds1 = tr.getElementsByTagName("t
d");
dojo.byId("bookId").value = id;
dojo.byId("title").value = dojo.string.trim(dojo.dom.
textConten
t(tds1[0])
);
dojo.byId("author").value = dojo.string.trim(dojo.dom.
textConten
t(tds1[1])
);
dojo.byId("pagesNo").value
= dojo.string.trim(dojo.dom.
textConten
t(tds1[2])
);
}
});
</script>
</head>
<body>
<s:url action="listPerson" id="descrsUrl"/>
<s:url action="listBook" id="descrsUrlBook"/>
<s:url action="selectTag" id="descrsUrlTag"/>
<table border="1" cellspacing="3">
<tr>
<td>
<!-- First tag only define link for refresh and signal that will be sent when link is followed -->
<div style="text-align: right;">
<s:a theme="ajax" notifyTopics="/refreshPers
on">Refres
h</s:a>
</div>
<!-- This link defines ajax div that will be loaded on the page load and every time that listen topic signal is received
href is referenced by earlier defined descrsUrl=listenPerson. Action listenPerson is defined in struts.xml, as well as its result
-->
<s:div id="persons" theme="ajax" href="%{descrsUrl}" loadingText="Loading..." listenTopics="/refreshPers
on"/>
</td>
<td>
<div style="text-align: right;">
<s:a theme="ajax" notifyTopics="/refreshBook
">Refresh<
/s:a>
</div>
<s:div id="books" theme="ajax" href="%{descrsUrlBook}" loadingText="Loading..." listenTopics="/refreshBook
"/>
</td>
</tr>
<tr>
<td>
<p>Person Data</p>
<s:form action="savePerson" validate="true">
<s:textfield id="personId" name="person.id" cssStyle="display:none"/>
<s:textfield id="firstName" label="First Name" name="person.firstName"/>
<s:textfield id="lastName" label="Last Name" name="person.lastName"/>
<s:textfield id="email" label="E-mail" name="person.email"/>
<s:submit theme="ajax" targets="persons" notifyTopics="/savePerson"
/>
</s:form>
</td>
<td>
<p>Book Data</p>
<s:form action="saveBook" validate="true">
<s:textfield id="bookId" name="book.id" cssStyle="display:none"/>
<s:textfield id="title" label="Book Title" name="book.title"/>
<s:textfield id="author" label="Book Author" name="book.author"/>
<s:textfield id="pagesNo" label="Book pages no" name="book.pagesNo"/>
<s:submit theme="ajax" targets="books" notifyTopics="/saveBook"/>
<s:select label="Select owner" name="owners" headerKey="-1" headerValue="-- Please Select --" list="persons" listValue="firstName" listKey="id" emptyOption="true" />
</s:form>
</td>
</tr>
</table>
</body>
</html>