MissingPropertyException: No such property: * for class: java.util.LinkedHashMap$Entry - grails

lets say i have this:
ArrayList maps = [ ]
Map map = [:]
my controller i did that:
List.each {
myList -> map = [key1:value1,key2:value2,key3:value3]
maps << map
}
return render ( template: "myTemplate" , model: [arrayList:maps])
I'm passing this arrayList of maps to my GSP and iterating through it so i assign the values of each map to elements.
i did something like this in my gsp.
<g:each in="${arrayList}" var="map">
<g:select from="${someList}" optionValue="${map.get('key1')}" optionKey="key"/>
<input type="text" id="textBox" value="${map.get('key2')}"/>
</g:each>
i am getting this error ! which says:
ERROR errors.GrailsExceptionResolver - MissingPropertyException occurred when processing request: [POST] .....
No such property: myValue for class: java.util.LinkedHashMap$Entry. Stacktrace follows:
groovy.lang.MissingPropertyException: No such property: myValue for class: java.util.LinkedHashMap$Entry
at Users_**_Projects_**_grails_app_views__myGsp_gsp.run(_myGsp.gsp:6)
at org.grails.plugins.web.rest.api.ControllersRestApi.render(ControllersRestApi.groovy:53)
at se.su.it.vfu.ConfigController$$EPLhPshc.myFunction(myController.groovy:428)
myGsp.gsp:6: is actually the "select" row provided in the gsp code
and 428 in my controller is the return render () row
myValue is actually a map value!
I am iterating through the arrayList and the first map is map1 looks like this
[key1: myValue , key2: otherValue , key3 : someOtherValue]

You have the following in your GSP:
<g:select from="${someList}" optionValue="${map.get('key1')}" optionKey="key"/>
That is going to be the problem. The value that you assign to optionValue should be the name of a property on the elements in someList. That property will be used when generating the "value" of the individual elements in the list. In your case it looks like map.get('key1') evaluates to myValue so the select tag is going to try and retrieve the value of the myValue property for each element in the list.
See http://grails.github.io/grails-doc/3.0.4/ref/Tags/select.html for more details.
I hope that helps.

Related

UI5 List Binding not showing content

I have a list object which is bound to a UI5 List element. However the values are not showing. Please take a look at my code.
UI/xml Code:
<List id="statementList" headerText="Statements"
items="{ path: 'statementListModel>/' }">
<StandardListItem title="{importance}" description="{importance}"/>
</List>
Binding in JS:
var result = JSON.parse(aData.responseData);
that.getView().byId("statementList").setModel(new JSONModel(), "statementListModel");
that.getView().byId("statementList").getModel("statementListModel").setData(result.statementList);
The list object is built like this:
result= {
statementList = [
{
importance = "ASD",
...
},
{
importance = "BDS",
...
}
]
}
However it is just not showing the content. The list has the correct size so the binding somewhat works but the content binding does not work:
Thanks for any help!
You have to add the model name EVERYWHERE:
<StandardListItem title="{statementListModel>importance}" description="{statementListModel>importance}"/>

Grails controller not getting current value of a dropdown in a controller

I have a dropdown (g:select) in my view that displays a list from a domain class. I want to println whatever value that is in it whenever a save button is clicked. Below is the script in the view
<g:select params="[tablename: storehouseInstance?.tablename]" name="tablename" from="${Warehouse.list()}" optionKey="id" optionValue="${Warehouse.list()}" value="tablename"
noSelection="['':'Choose Table']"
onchange="${remoteFunction (
controller: 'warehouse',
action: 'updateSelect',
params: "'warehouse.id=' + this.value + storehouseInstance?.tablename" ,
param: "this.value",
update: [success:'fieldSelection'] //update: 'fieldselection'
)}"/>
And in my controller i have:
def save() {
println("params are "+ params)
println("Saving table name: " + params.tablename)
}
I am getting tablename to be '1' instead of the value that should be something like 'MYTESTVALUE'
Any help please.
Assuming the warehouse's name property is name, try this instead:
<g:select params="[tablename: storehouseInstance?.tablename]"
name="tablename"
from="${Warehouse.list()}"
optionKey="name" // I changed this line
optionValue="name" // I changed this line too
value="tablename"
noSelection="['':'Choose Table']"
onchange="${remoteFunction (
controller: 'warehouse',
action: 'updateSelect',
params: "'warehouse.id=' + this.value + storehouseInstance?.tablename" ,
param: "this.value",
update: [success:'fieldSelection']
)}"/>
In a g:select the parameter that is sent to the server is defined by optionKey and the value that is displayed in the dropdown is defined by optionValue
Aside
From an MVC purity point-of-view, you shouldn't be doing queries in GSPs, e.g. Warehouse.list(). Instead you should retrieve the data you need in a service/contoller and pass it via the model to the view (GSP).

some basic queries regarding value stack in struts 2?

I am new to strut 2 though I have worked on struts 1.2.In one of the pexisting project jsp file I have following code:
<script type="text/javascript">
var relationshipData = { // line1
page : '<s:property value="displayPage" />', // line2
records : '<s:property value="customerRelations.size" />', // line3
rows : [ <s:iterator value="customerRelations" status="iterStatus"> // line4
{ id : '<s:property value="relationId" />',
cell : [ '<s:property value="relationDesc" escapeJavaScript="true" />' ] } <s:if test="!#iterStatus.last">,</s:if> //line5
</s:iterator>] // line6
};
</script>
Request is coming CustomerRelationAction.java which has method getCustomerRelations() and getRelationId().
here are the questions :-
I put breakpoint inside method getCustomerRelations().i see flow is coming four time inside this method. Two times at line 3 and another two times at line 4.
As per my understanding flow should come only 1 time i.e at line 3. Once it completes getCustomerRelations at line 3 , should not put its value in value stack so that
it can refer to it nextime it is refered (like it is being reffered at line 14 again)?
getCustomerRelations() method returns the list of CustomerRelationData objects where CustomerRelationData class also contains the getRelationId() method.Now at line
5 we are refering value="relationId at line 5. On Which object(CustomerRelationAction.java or CustomerRelationData), getRelationId() method will be called?
even i am not sure will the list object CustomerRelationData will be present on value stack or not?If yes at which line it will be put in value stack?
Now the iterator completes at line 6.After that,now i refer the code <s:property value="relationId" /> again, On Which object(CustomerRelationAction.java or CustomerRelationData),
getRelationId() method will be called?
1) I don't know why you think calling for a property of customerRelations and then using customerRelations in an iterator tag would only call getCustomerRelations() once; you're using it twice, so at a minimum it'll get called twice.
If you want to keep a reference to it, use <s:set> to create a new reference to the collection. I don't see a point to doing so, however, unless your getter is doing something time-consuming.
I don't see the same behavior. Given the question's <script> snippet, it renders thusly (assuming a dummy, three-element list with sample data):
<script type="text/javascript">
var relationshipData = { // line1
records : '3', // line3
rows : [ // line4
{ id : '1',
cell : [ 'desc 1' ] } , //line5
// line4
{ id : '2',
cell : [ 'desc 2' ] } , //line5
// line4
{ id : '3',
cell : [ 'desc 3' ] } //line5
] // line6
};
</script>
And the log output, with a debug statement in the getter, is this:
2012-01-19 13:58:10,552 DEBUG [TextExampleAction.java:18] : Enter.
2012-01-19 13:58:10,571 DEBUG [TextExampleAction.java:18] : Enter.
I'm more likely to believe the JSP/JS/etc. at this point.
2) The iterator tag puts each object on the top of the stack, as described in the tag docs. The top of the stack is the first object that will be used to get the value of relationId. If it isn't found on the stack top, OGNL will traverse the value stack until either the property is found, or there's no more stack.
3) See the previous answer: once you're out of the iterator, there's no longer a customer relation on the stack, and you're back to the action.

Problem with Grails g:each tag

I'm struggling to get a g:each tag to work. What I'm passing to the view is a list of hashmaps (something like this [ [ : ] , [ : ] ] ).
Each hashmap is of the form [location: somelocation , artist: someartist].
The code is the following:
CONTROLLER
in the controller I'm passing the following:
[searchedResults : results.searchedResults]
VIEW
<g:each status="i" in="${searchedResults}" var="results">
if(results.location!=null){
var point${results.location.id} = new google.maps.LatLng(${results.location.lat}, ${results.location.lng});
var myMarkerOptions${results.location.id} = {
position: point${results.location.id},
map: map
};
var marker${results.location.id} = new google.maps.Marker(myMarkerOptions${results.location.id});
}
</g:each>
Any ideas why this wouldn't work?
Thanks!
GrailsGuy is right in that you can't write groovy code in the body of an each tag like that. But let me try and convert it to something for you, since it looks like your doing some javascript in there as well...I think all you need to fix is your if statement
<g:each status="i" in="${searchedResults}" var="results">
<g:if test="${results.location}">
//everything else seems like it would work, assuming your javascript
// code is accurate
</g:if>
</g:each>

Map problem when passing it as model to view in grails

In a controller, I have populated a map that has a string as key and a list as value; in the gsp, I try to show them like this:
<g:each in="${sector}" var="entry" >
<br/>${entry.key}<br/>
<g:each in="${entry.value}" var="item" >
${item.name}<br/>
</g:each>
</g:each>
The problem is that item is considered as string, so I get the exception
Error 500: Error evaluating expression [item.name] on line [11]:
groovy.lang.MissingPropertyException: No such property: name for class:
java.lang.String
Any hints on how to fix it other than doing the find for the item explicitly in the gsp ?
I did the poc which works fine for me
class Item{
String name
}
List items = [new Item(name:"laptop" ,
new Item(name:"mouse")]
Map sector =[:]
sector.manager = items
sector.manager.each{item->
println item.name
}
Even If it doesn't work, Try declaring Map sector as
Map < String, List < Item > > sector =[:]

Resources