How to solve IllegalArgumentException: Property is not indexed - java

I have a property in jsp like the following
<html:text property="sequenceNumbersMap[0]" styleId="sequenceNumbersMap[0]" value="0"/>
<html:text property="sequenceNumbersMap[1]" styleId="sequenceNumbersMap[1]" value="1"/>
<html:text property="sequenceNumbersMap[2]" styleId="sequenceNumbersMap[2]" value="2"/>
<html:text property="sequenceNumbersMap[3]" styleId="sequenceNumbersMap[3]" value="3"/>
and ActionForm has the property like
Map sequenceNumbersMap;
and getter/setter
public Map<Integer, Integer> getSequenceNumbersMap() {
return sequenceNumbersMap;
}
public void setSequenceNumbersMap(Map<Integer, Integer> sequenceNumbersMap) {
this.sequenceNumbersMap = sequenceNumbersMap;
}
but when i try to submit the jsp i get the following exception :
java.lang.IllegalArgumentException: Property 'sequenceNumbersMap' is not indexed
would someone help me to fix this issue?
Thanks

A map is not ordered so sequenceNumbersMap[i] does not mean anything. If you mean to get the value mapped to i rather than getting the i-th item in the map (which has no sense again), you can do it with sequenceNumbersMap.get(i).

You must either iterate through a map differently: How to loop through a HashMap in JSP?
Or use a different kind of data structure for sequence numbers (indexed, as it was pointed in the exception, e.g. java.util.List).

Related

In Struts2, dynamically generate form element name using a member value

My requirement: In Struts 2, dynamically generate form element name by using member value.
I had tried with: <s:textarea name="employee_<s:property value='employeeNumber'/>"/>
Resulting code in web browser: <textarea name="employee_<s:property value='employeeNumber'/>"></textarea>
My expectation in web browser code: <textarea name="employee_101"></textarea>. 101 as an employeeNumber is only used for demonstration only.
Please help, thanks!
In Struts2 you can always use OGNL expression in the Struts tag's attribute. Just provide the value from the value stack. You should know that the action bean is on top of it, and having a getter for employeeNumber should return desired value 101.
<s:textarea name="employee_%{employeeNumber}"/>
Also be aware of Struts doesn't allow nested tags in the attributes in favor of OGNL.
try this,it working fine.
MyAction.class
private int property_value;
public int getProperty_value() {
return 6;
}
public void setProperty_value(int property_value) {
this.property_value = property_value;
}
result.jsp
<s:set var="xyz" value="property_value" />
<s:textarea name="emap_%{#xyz}" />

How to Set a HashMap from jsp to Action

I have a HashMap in my Action class:
private Map<String, String> ids = new HashMap<String, String>();
In jsp I'm trying to set this hashmap like this:
<input type="text" name="ids[0].key" value="key">
<input type="text" name="ids[0]" value="value">
But when after submit, when I iterate over the map in the action like this:
if(ids!=null){
for(Map.Entry<String, String> entry : ids.entrySet()){
system.out.println(entry.getKey()+"-"+entry.getValue());
}
}
I only get "0-value" instead of "key-value"
How Can I do what I want? Can someone help me with this?
Trying to set values in a HashMap in a JSP file is a very bad idea. To stick with best practice and lead a happy life, you should revisit your design. You can post the data to the server side (All input values from JSP) and then get the values from request to store in a HashMap collection as per your requirement would be a better option.
You have keys of type String and therefore should be mapped like strings. For example
<input type="text" name="ids['0']" value="%{value}">
Then you will get key '0' and value that was provided by OGNL.
About indexed property names and createIfNull setting you can find in the docs Advanced Type Conversion.

struts2 get property with dynamic value in jsp

I have a problem with struts property, pl check my sample code.
in struts action class i have a property that is name2,
SampleAction.java
public class SampleAction extends ActionSupport
{
private String name2;// setter and getter is also there
}
if i use like below i ll get the value
<s:property value="name2"/>
I wan to get the name2 value in jsp in dynamic way,
but here i need pass the value 2 in dynamic way some thing like below..
<s:set name="nameNumber" value="2" />
<s:property value="name%{#nameNumber}"/>
How can i achieve that.
Thanks in Advance,
Prabhakar Manthena

Reading contents from application map dynamically IN JSP page. - struts 2.0

I am using ApplicationAware in my struts 2.0 action class. So, i placed an entry in then application map as
application.put("animalDTO", animalDTO);
animalDTO:
public class AnimalDTO {
private String name_N1;
private String name_N2;
private String name_N3;
private String name_N4;
// getters and setters
}
Now before going to my jsp page i am populating the values in each of the variables.
In JSP page: i am using iterator to get N1, N2 till N4.
Now i need to create s:textfield and populate these variables content DYNAMICALLY.
ie.
<s:iterator value="application.nameList" var="nameList">
<s:textfield theme="simple" value="%{application.animalDTO.name_#nameList}" />
</s:iterator>
Here, i am not getting any values populated.
If i hard code as -> value="%{application.animalDTO.**name_N1**}"
Then all the text-fields will be populated with value of name_N1, but that not what i need. I need for fetch the values dynamically.
Can anyone help me with this OGNL requirement?
Thanks in advance
=====================
I tried this also, i created string variable which is of the required ognl format, but i dont know how to make the string as a lookup to valuestack. This is what i did:
<s:iterator value="application.nameList" var="nameList">
<s:set var="varUrl" value="%{'application.animalDTO.name_' + #nameList}" />
<s:property value="varUrl" />
<s:textfield name="NAME__%{#nameList}" theme="simple" id="NAME-%{#nameList}" value="%{varUrl}" />
</s:iterator>
but instead the text box is getting values like:
application.animalDTO.name_1
application.animalDTO.name_2
application.animalDTO.name_3
application.animalDTO.name_4
what is need is the values corresponding to these variables.
Any idea how to convert this string as an object/key to lookup into valuestack.
Finally i got the solution. I tried many trail and error methods. And finally hit the answers. We can dynamically read the property values by doing:
<s:iterator value="application.nameList" var="nameList">
<s:textfield theme="simple" value="%{application.animalDTO['name_' + #nameList]}" />
</s:iterator>
I tried %{application.animalDTO['name_' + #nameList]} and it worked. :-)

Castor Collection Field to empty

My castor masrshaller have an XML output as below
root>
field1 /field1>
field2 /field2>
..........
fieldn>
collection>
field>
field>
..........
field>
/collection>
/root>
my mapping for the collection part is
field name="collectionObj" type="string" collection="arraylist">
bind-xml name="field" location="collection" node="element" />
/field>
The issue is when the I will always have the collectiObj is empty
whole collection>tag disappears from the XMl output. Instead I would like to display as collection/>. Is there a way other than writing some fieldhandler, say setting some property to handle this issue.
Couldn't find any alternative other than a custom field handler.. It is working fine...

Categories

Resources