some basic queries regarding value stack in struts 2? - struts2

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.

Related

How to find an object by a property from a List of List of objects in dart?

Hello I am new to dart and trying to find an item by property name in a list of list.
class Product{
String id;
String title;
Product(this.id,this.title);
}
void main(){
List<List<Product>> allProdcuts=[
//New Prodcuts
[
Product("1","Hammer"),
Product("3","Nails"),
Product("2","Screws"),
],
futureItems,
//Old Prodcuts
[
Product("4","Rock"),
Product("5","Paper"),
Product("6","Scissor"),
],
//Rare Items
[
Product("7","Plank"),
Product("8","Wires"),
Product("9","Box"),
],
];
print(allProdcuts.where((itemsList)=>itemsList.contains((product)=>product.title='Wires')));
//Returns ()
}
I have tried using for a single List:
List<Product> futureItems= [
Product("101","Galactic Hammer"),
Product("301","Galactic Nails"),
Product("201","Galactic Screws"),
];
print(newProduct.firstWhere((p)=>p.title=='Hammer'));
//Instance of 'Product'
Also tried this:
print(allProdcuts.map((itemList)=>itemList.firstWhere((p)=>p.title=='Nails')));
// Bad state: No elementError: Bad state: No element.
But there is an element with the title='Nails'.I don't understand what I am doing wrong.
You are calling itemList.firstWhere((p)=>p.title=='Nails') on each list, also the ones with no element with title "Nails". Since firstWhere throws if there is no matching value, it does that for two of your three lists. Also, in the example, itemsList.contains(...) does not take a callback, so you are just checking whether a function is in the list, which it isn't. You might want to use any for that, but it won't solve the problem here.
To do this efficiently, I'd probably create helper function:
Product findByTitle(List<List<Product>> allProducts, String title) {
for (var products in allProducts) {
for (var product in products) {
if (product.title == title) return product;
}
}
// Or return `null`.
throw ArgumentError.value(title, "title", "No element with that title");
}
The return in the middle allows you to skip out of the double iteration the moment you have a match, something which is harder to do with firstWhere/map/forEach etc.
One alternative solutions would be:
var product = allProducts.expand((l) => l.where((p) => p.title == title)).first;
which finds all the products with the given title and flattens them into a single iterable, then picks the first one (if there are any). Because iterables are lazy, it will actually stop at the first match.
There are many ways to solve this.
One example is to use the forEach() method:
allProdcuts.forEach(
(List<Product> l)=>l.forEach(
(Product p){
if (p.title=="Nails")
print(p.id);
}
)
);
The for each method receives a function and applies this function to every element on the list. If you have a lists of lists, you can do this twice to get a function applied to each element of the sub lists.
The above code prints 3, which is the desired result.
Another solution would be to flatten the list first, so you can have an easier search later.
print(allProdcuts.any((innerListOfProducts) =>
innerListOfProducts.any((product) => product.title == 'Wires')));
This code will return true if 'Wires' is in the inner list, and false otherwise.

SAPUI5 - complex model binding

I have this json model:
model/data.json
{
"orders" : [
{
"header" : { "id" : "00001", "description" : "This is the first order" },
"items" : [
{ "name" : "Red Book","id" : "XXYYZZ" },
{ "name" : "Yellow Book", "id" : "AACCXX" },
{ "name" : "Black Book", "id" : "UUEEAA" },
]
},
{
// another order with header + items
},
.....
]
}
and I'm assigning it onInit to the view, like this:
var model = new sap.ui.model.json.JSONModel("model/data.json");
sap.ui.getCore().setModel(reqModel);
I'm trying to display a list of orders in the first view (showing the id), like this:
var list = new sap.m.List({
id: "mainList",
items: []
});
var items = new sap.m.ActionListItem({
text : "{id}",
press : [ //click handler, onclick load the order details page ]
});
list.bindItems("/orders", items);
.... // add list to the page etc etc
What I cannot do, is connect each order to its header->id.. I tried
text: "/header/{id}"
text: "{/header/id}"
in the items declaration, and
list.bindItems("/orders/header", items)
in the list binding, but none of them works.. The id value is not displayed, even though a "blank" list item is shown..
Any idea? What am I doing wrong?
Thank you
The solution was one of those I tried (but I don't know why it didn't work at that time)
text: "{/header/id}"
The ListItem acts as a Template for a list/array of objects. That's why you bind it against an array structure in your data:
list.bindItems("/orders", itemTemplate)
That makes bindings of the ListItem relative to /orders and therefore your item should look like this without leading '/' (absolute paths would look like this /orders/0/header/id asf.):
var itemTemplate = new sap.m.ActionListItem({
text : "{header/id}",
press : [ //click handler, onclick load the order details page ]
});
Not quite sure how you made it work the way you have shown... May be it's not as picky as I thought.
Btw: For whatever reason the ResourceModel builds an exception of that syntax. You can always omit the leading '/' when dealing with ResourceModels (probably because they do not allow nested structures).
BR
Chris
Cannot add comments yet, therefore an answer to you solved Problem, that could answer the initial problem. (And inform People using that example in any way)
In the current code listing you use the variable "reqModel" to set the model, but the variable with the model in it is named "model" in the line before. Maybe that was the first reason why both of your examles would not work?
Perhaps this error was cleared on rewriting some passages while testing.
greetings! -nx

sapui5 table shows only the same record [duplicate]

This question already has an answer here:
OData Model Not Working
(1 answer)
Closed 20 days ago.
I have created a webservice and trying to bind data using oData protocol in SAPUI5.
I have created a table:
createContent : function(oController) {
jQuery.sap.require("sap.ui.table.Table");
//Create table control with properties
var oTable = new sap.ui.table.Table({
width : "100%",
rowHeight : 50,
title : "Lst of Items",
selectionMode : sap.ui.table.SelectionMode.None
});
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "PO Number"
}),
template : new sap.ui.commons.TextView({
text : "{PoNumber}"
}),
}
));
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "Item"
}),
template : new sap.ui.commons.TextView({
text : "{PoItem}"
}),
}
));
//Filter values for a certain PO
var aFilter = [];
aFilter.push( new sap.ui.model.Filter("PoNumber", sap.ui.model.FilterOperator.EQ, "4500000043") );
oTable.bindRows({
path: "/PurchaseOrderItemCollection",
filters: aFilter
});
return oTable;
}
The output should be as follows:
PONumber POItem
4500000043 0010
4500000043 0020
But what I get is:
PONumber POItem
4500000043 0020
4500000043 0020
So it shows the last item twice and doesn't show the first item. If I put a break point in my web service code then it is populated correctly.
The data model is created in the following way:
var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, false, "user", "passw");
sap.ui.getCore().setModel(oModel);
I have encountered this. Problem is with your data model. Ensure that for the entity both PO number and PO item are marked as keys. Refresh any metadata cache, ensure that both properties appear as keys and try again. It should work.
Thanks
Krishna
My understanding is every entity/entry in the collection should have a unique id <entry><id>...</id></entry>.
And in my case, the returned collection had no ids set for the entities. So the bound ui element finds multiple objects with same id (in this case empty id) and ends up displaying value which it finds the last.
The same should apply even if the id is same across all entities.
Hope it helps, if you have not already found what the problem is.
Thanks,

Wix populate listbox

I am trying to fill in a ListBox with CustomAction and it's not going well.
I try to figure out the session.Database.Tables but have no idea how to start.
I've created a listbox like this
<Control Id="ListBox1" Type="ListBox" Sorted="no" Indirect="no" Property="LISTBOXVALUESONE" X="10" Y="50" Width="150" Height="180">
<ListBox Property="LISTBOXVALUESONE">
<ListItem Text="ARGHH!" Value="1"/>
</ListBox>
</Control>
But I cant see the property in my verbrose log or anything about an table so I guess I have to create an table in customAction and populate it?
I see my ARGHH! in the list so it should exsist but how do I access the values? And add new ones?
Found more examples and stuff in C++ but i would like to make the CustomAction in C#
EDIT
Database db = session.Database;
string sqlInsertTemp = db.Tables["ListBox"].SqlInsertString + " TEMPORARY";
View view = db.OpenView(sqlInsertTemp );
view.Execute( new Record( new object[] { "LISTBOXVALUESONE", 2, "2", "One" } ));
view.Close();
Thanks to Christopher I got it to work with adding an value.
db.Tables["ListBox"] should remain the same and name the type not the id as i taught
And on this line view.Execute( new Record( new object[] { "LISTBOXVALUESONE", 2, "2", "One" } ));
you put your Listbox Property and then the placement of the value "one" we insert
The two "2"s is what I figure the placement we want it on and I already have an test value on 1
my "ARGHH!" so I put the new on 2 and dont know the details but...
I got an Table Update error and, one dublicate value error if i put 2,1 or 1,2 in the customaction!
I wrote a blog article about 5 years ago that might help you:
How DTF is going to help me become a better .NET Developer
You want to make sure your built MSI has a ListBox table otherwise the SQL won't work when it tries to generate the temp rows dynamically at runtime. If the ListBox element doesn't do this for you, the EnsureTable element will.
The actual C# looks something like:
Database db = session.Database;
string sqlInsertTemp = db.Tables["ListBox"].SqlInsertString + " TEMPORARY";
View view = db.OpenView(sqlInsertTemp );
view.Execute( new Record( new object[] { "TESTPROP", 1, "1", "One" } ));
view.Close();
Note this is an old code example and doesn't properly take advantage of using statements and IDisposable.
Add one record to list box:
private void AddRecordToListBox(string listBoxPropertyName, int index, string text, string value)
{
View view = session.Database.OpenView("SELECT * FROM ListBox");
view.Execute();
Record record = session.Database.CreateRecord(4);
record.SetString(1, listBoxPropertyName);
record.SetInteger(2, index);
record.SetString(3, value);
record.SetString(4, text);
view.Modify(ViewModifyMode.InsertTemporary, record);
view.Close();
}
Fill ListBox:
private void FillListBox()
{
var dict = SomeDict();
int index = 1;
foreach (var element in dict)
{
AddRecordToListBox(ListBoxName, index, element.Key, element.Value);
index++;
}
}
Clear ListBox
private void ClearListBox(string listBoxPropertyName)
{
var command = String.Format("DELETE FROM ListBox WHERE ListBox.Property='{0}'", listBoxPropertyName);
View view = session.Database.OpenView(command);
view.Execute();
view.Close();
}

how do i iterate the tables parameters which is present under the main table?

In lua ,im calling a function which returns a table variable that contains many parameter internally..but when i get that value i couldnt access the paramter which is present in the table. I can see the tables parameter in the original function in the form of
[[table:0x0989]]
{
[[table:0x23456]]
str = "hello"
width = 180
},
[[table:0x23489]]
{
str1 = "world"
}
it shows like this.but when it returns once i can able to get the top address of table like [[table:0x0989]]..when i tried acessing the tables which is present inside the main table.it is showing a nil value...how do i call that ?? can anyone help me??
If I'm reading it correctly you're doing this:
function my_function ()
--do something
return ({a=1, b=2, c=3})
end
From that you should be able to do this:
my_table = my_function()
then
print(my_table.a) --=> 1
print(my_table.b) --=> 2
print(my_table.c) --=> 3

Resources