checkbox - list elements - java

my checkbox is not showing the values of my list as "checkable" things.
Its just saying "publication.keywords" :(
But my list is not empty, as I can show it in an iterator.
Please help me. Whats my mistake?
This is my Checkbox
<s:checkbox key="publication.keyword" list="keywords.{name}" />
Its working with the Iterator
<s:iterator value="keywords">
<tr>
<td><s:property value="name" /><br></td>
</tr>
</s:iterator>

The syntax is wrong.
Looking at your iterator, the checkboxlist should be like :
<s:checkboxlist list="keywords" listValue="name" listKey="name"/>
The 'listKey' attribute specifies what will be submitted, if that checkbox is checked. I have used name for now, you can change it to what you require at the backend.
For e.g. if
List<MyBean> keywords
and
class MyBean
{
String name;
Integer id;
//getters and setters
}
then, the s:checkbox can be something like :
<s:checkboxlist list="keywords" listValue="name" listKey="id"/>

Related

Access a list within a dropdown in Javascript

Im trying to access a list within a dropdown in Javascript. So I have an object called System (which is the object stored in the dropdown) and this has a List of Collections. I am new to Javascript and not sure what the correct syntax should be to access the list.
So this is the form that contains the dropdown:
<form:form id="systemForm" method="post" action="/application/SystemSave" commandName="systemForm">
<form:select path="uSelectedSystemId" id="uSystemId">
<c:forEach var="system" items="${systemsList}">
<form:option value="${system.id}" label="${system.name}" />
</c:forEach>
</form:select>
</form:form>
and this is the setup of the System object (from Java):
public class System {
private Long id;
private String name;
private List<Collection> collections;
}
and this is the setup of the SystemForm used to store the values:
public class SystemForm {
private String fUpdateSystemName;
private String uSelectedSystemId;
private List<Collection> uCollections;
}
I guess I need some sort of hidden field within the loop that stores the values but im not sure of the correct syntax. I could just rehit the database and get all Collections based on the systemID but I dont think this is the correct way of doing things seeing as the information should already be available.
Any help much appreciated!
try this
<form:form id="systemForm" method="post" action="/application/SystemSave" commandName="systemForm">
<select id="uSystemId">
<c:forEach var="system" items="${systemsList}">
<option value="${system.id}" label="${system.name}" />
</c:forEach>
</select>
</form:form>
i dont know exactly this will meet your requirement or not.

Struts logic:iterate input field

I currently have the following code and the data is displayed fine.
<logic:iterate name="myList" id="product" indexId="iteration" type="com.mycompany.MyBean">
<tr>
<td> <bean:write name="product" property="weight"/> </td>
<td> <bean:write name="product" property="sku"/> </td>
<td> <bean:write name="product" property="quantity"/> </td>
</tr>
</logic:iterate>
But now I need to make the "quantity" part modifiable. The user should be able to update that field, press submit and when its sent to the server, "myList" should automatically update with the new quantities.
I've tried searching for help on this but all I keep finding is examples on how to display data only, not modify it. Any help would be appreciated.
So this is tricky, because there are many things to get done in order for it to work. First, declare your tags inside the iterator with the html tags, with attribute INDEXED=TRUE and an ID DIFFERENT THAN THE NAME, i also took out the "indexId" attribute to use the simple "index" word for the index:
<logic:iterate name="myList" id="myListI" type="com.mycompany.MyBean">
<tr>
<td> <html:input name="myListI" property="weight" indexed="true"/> </td>
<td> <html:input name="myListI" property="sku" indexed="true"/> </td>
<td> <html:input name="myListI" property="quantity" indexed="true"/> </td>
</tr>
after that, in order for struts to be able to get and set the attributes of your beans, you need to declare EXTRA get and set methods inside your collection object, using the name you wrote in the id of the iterate tag. In this case, you would write 2 extra get and set methods for the "myListI" :
public void setMyListI(int index, myBean value){
this.myList.add(value);
}
public myBean getMyListI(int index){
return this.myList.get(index);
}
I think Th0rndikes answer is mostly correct. My implementation is slightly different, so it might be worth trying this as well.
Form
private List<Parameter> activeParameters;
public List<Parameter> getActiveParameters() {
return activeParameters;
}
public Parameter getParam(int index){
return this.activeParameters.get(index);
}
JSP
<logic:iterate name="MyForm" property="activeParameters" id="param">
<tr>
<td><bean:write name="param" property="prompt"/></td>
<td><html:text name="param" property="value" indexed="true"/></td>
</tr>
</logic:iterate>
In summary, I didn't use Type in the iterate tag, using the property tag instead. In the bean adding a getter with matched the name of the iterate ID in the JSP (param) with an index as a method parameter did the trick.
Take a look at this: http://wiki.apache.org/struts/StrutsCatalogLazyList
Indexed Properties
Struts html tags have an indexed attribute which will generate the
appropriate html to populate a collection of beans when the form is
submitted. The trick is to name the id attribute to the same as the
indexed property.
For example the following jsp...
<logic:iterate name="skillsForm" property="skills" id="skills">
<html:text name="skills" property="skillId" indexed="true"/>
</logic:iterate>
...will generate the following html
<input type="text" name="skills[0].skillId value="..."/>
<input type="text" name="skills[1].skillId value="..."/>
....
<input type="text" name="skills[n].skillId value="..."/>
When the form is submitted BeanUtils will first call the
getSkills(index) method to retrieve the indexed bean followed by
setSkillId(..) on the retrieved bean.
Theoretically, the indexed attribute of the struts html tags could be used for this:
Valid only inside of logic:iterate tag. If true then name of the html tag will be rendered as "id[34].propertyName". Number in brackets will be generated for every iteration and taken from ancestor logic:iterate tag.
But, there is no corresponding indexed attribute on the html:errors tag, which limits its usefulness. Also, the required combination of id, name and property attributes can be rather confusing.
I found it easier to use jsp scriptlets to generate the property name including the iteration index. The following code requires that your form has a string array property "quantity".
<% int idx=0; %>
<logic:iterate ...>
<html:text property='<%= "quantity[" + idx + "]" %>'/>
<html:errors property='<%= "quantity[" + idx + "]" %>'/>
<% i++; %>
</logic:iterate>

Retrieve value from checkbox

I have jsp code like this
<c:forEach items="${requestScope.XX}" var="x">
<tr>
<td><input type="checkbox" value="${x.xID}" name="x"></td>
<td> ${x.name}</td>
</tr>
</c:forEach>
I am not able to retrieve value of checkbox in servlets.
My servlet code is here :
String xId=request.getParameter("x");
May i know where i am going wrong?
Requirement is that only one checkbox is checked. So no need of array in servlets
does it shows more than one checkbox with name x ? as it is in c:forEach if so then
String xId=request.getParameter("x");
will take value of first checkbox every time.
Use HttpServletRequest#getParameterValues() instead.
String[] checked = request.getParameterValues("x");
// ...

Updating the value of a iterator tag in struts2

I have a jsp in which I have a drop down as
<s:select name="newQuestion.CertificationId" list="certificationList"
listKey="certificationId" listValue="certificationName"
headerKey="" headerValue="Select Certification"
label="Certification Name"
onchange="getQuestionsList(this.value)" />
When the dropdown value changes I can getQuestionsList. In the javascript function I submit to an action class where I modify the value of a questionList which is displayed in my JSP via an iterator.
The values of the questionList contain all questions and when I select a value from the above drop down I need to populate only those questions which belong to the id selected in the drop down. (I query the DB to load the questions in action class.)
Initially when the page is loaded I have all questions in questionList but after selecting a value from drop down I have the updated questions in the action class.
For displaying the values of question list I use a iterator tag
<div id="questionDetails" class="registrationDetails" style="display: none;">
<span><b>Question List</b></span>
<br>
<table class="registrationDetailsTable">
<tr class="tabledataheader">
<td>Question Id</td>
<td>Question Description</td>
</tr>
<s:iterator value="questionList">
<tr class="tabledatarow">
<td><s:property value="questionId" /></td>
<td><s:property value="questionDesc" /></td>
</tr>
</s:iterator>
</table>
</div>
The div is initially hidden and on select of a value in drop down I need to display the values of questionList which is taking old values as the page is not reloaded.
When I again come back to this jsp I am not seeing the new value as it is not getting updated.
Any heads up please
Why don't you try Struts2 Jquery Plugin. Here is the showcase with code.
What you need is
<sj:select ...> tag.
Let me know if you still need an example.
You would need to make few changes to the code. Let me list them down:
Move the following part in your initial JSP to another jsp, say questionList.jsp. The below part should be the only thing which should be inside the newly created jsp.
<span><b>Question List</b></span>
<br>
<table class="registrationDetailsTable">
<tr class="tabledataheader">
<td>Question Id</td>
<td>Question Description</td>
</tr>
<s:iterator value="questionList">
<tr class="tabledatarow">
<td><s:property value="questionId" /></td>
<td><s:property value="questionDesc" /></td>
</tr>
</s:iterator>
</table>
In your main page, you would need to replace the above code part with
<jsp:include page="questionList.jsp" flush="true"/>
Now deploy and see if everything is in place as before.
In your getQuestionsList() javascript function, make an ajax call to another action mapping say showQuestionList.action?certification=12. Here, 12 will be the id of the certification, which you will handle in the action. If you use certification as an action property, add a variable with the same name with getter and setter methods.
In your action method say showQuestionList(), retrieve the questionList and assign to the action variable.
On return of SUCCESS of the above method, the result should send only questionList.jsp. This will have the part to list just the necessary fields.
In the getQuestionsList() javascript function, after the ajax call being success, get the data and put it into the div with id questionDetails.
Show the div.

Accessing Map attribute with dynamic key in Struts 2 OGNL

I have a list of Strings that are attribute names and a Map.
I'm trying to access a model(ex.project) in the map using attribute name in string list.
Here is what I have now.
<s:iterator value="theMap" var="element">
<tr>
<s:iterator value="attributeList" var="attrName">
<td><p><s:property value="#element.Project.#attrName" /></p></td>
</s:iterator>
</tr>
</s:iterator>
If I hard code the attribute name it works fine:
<td><p><s:property value="#element.Project.projectName" /></p></td>
Any advice is appreciated.
Using OGNL <s:property value="#element.Project[#attrName]" />

Categories