How to use Java constantns as a parameter in a Struts 2 OGNL tag - tags

I am ussing to recover a property from an object User in the session. The following expression works correctly:
<s:property value="#session.ATRB_SESSION_USER.getAttribute('ATTRIBUTE_USER_NAME')"/>
but those strings ATRB_SESSION_USER and ATTRIBUTE_USER_NAME are constatns defined in a class. How can I use the constant instead of the string? I'd like to do something like this
<s:property value="#session.<%=Constants.ATRB_SESSION_USER%>.getAttribute(<%=Constants.ATTRIBUTE_USER_NAME%>)"/>
Anyone knows how can I do that?
TIA

You can use #class#field OGNL syntax to refer to static fields. The class name in #class should be fully qualified.

Related

AEM Slightly - How to call java method whose name is stored in variable - expression inside expression doesn’t work in sightly

In sightly I have initialized the sling model class. Now, I want to call the method from that class. But method name is read from some variable (basically I read method name from other location). When I use that variable it doesn’t work. I know sightly doesn’t allow expression inside an expression, so would like to know if there is an alternative to fit this need.
<sly data-sly-use.detailsModel="org.svc.core.model.DetailsModel"/>
${detailsModel.{methodNameVariable}} - doesn’t work (if the method name is read from some variable)
${detailsModel.methodName} - works (if put the method name directly there)
That really dependes on how you structured your data model and what you want to do with it.
If you want to do something like an if ... else that can be achieved with:
<sly data-sly-test="${condition}">${model.someMethod}</sly>
<sly data-sly-test="${!condition}">${model.otherMethod}</sly>
If you want to do something like a switch ... case:
<sly data-sly-test="${condition1}">${model.method1}</sly>
<sly data-sly-test="${condition2}">${model.method2}</sly>
...
If you want to have a trully dynamic method names then you’re better off with a model that returns a map of values instead of having a number of properties/methods and use the dynamic method name as a key:
${model.details[detailName]}

"Use of unresolved identifier" using the new Swift 2.2 syntax for selectors on implicit setter

Migrating my code to Swift 2.2, I have a property var activeTextField:UITextfield? and the selector I was using was "setActiveTextField:". This method does not exist explicitly in my swift code.
With the new syntax, #selector(setActiveTextField) doesn't work: Use of unresolved identifier
I know I could use Selector("setActiveTextField:") but I'd loose the benefit of the new swift selectors.
So, what's the new way of doing this?
The issue here is that you're working with a property, not a method. This has two problems:
The ObjC setter/getter method pair for a property is generated at run time.
The #selector expression requires a Swift function/method reference.
When Swift compiles your source, it doesn't know that the ObjC -(UITextField*)activeTextField and -(void)setActiveTextField:(UITextField*)field methods will exist, so it can't generate function references for them. Since it can't generate a function reference, it doesn't have something it can use for the #selector expression.
For now, there isn't a way to use #selector to access property getters/setters — using Selector(...) with a string constant is your only option.
(This is actually just a new face on a longstanding known issue... it's the same reason you also can't pass a property getter/setter to something like map or filter. I think I've seen something on http://bugs.swift.org about this, but I'm not finding it at the moment.)
In Swift 3, SE-0064 was accepted to solve this problem. Now, you would generate that setter like so:
#selector(setter: activeTextField)

Passing selector in data-sly-resource in sightly

I am not able to pass the selector parameter in the data-sly-resource (when parameter is coming through some external object like java or through some list object). It does not work even though if i initialize a variable using "use" statement and then pass in data-sly-resource statement. However this is working fine when i am passing the hard coded values.
For example , below code works because it has hard coded value 'test value'
<sly data-sly-test="${item.tileReference}"
data-sly-resource.="${item.tileReference # wcmmode='disabled', selectors='test value',
resourceType='abc/tiles/generic-tile'}">
</sly>
However, below code doesn't pass the parameter value because it is coming through some object. here, 'tile.tileCount' holds the value which i have verified by printing in the html.
<sly data-sly-test="${item.tileReference}"
data-sly-resource.="${item.tileReference # wcmmode='disabled', selectors=tile.tileCount,
resourceType='abc/tiles/generic-tile'}">
</sly>
Turns out that Sightly accepts only String as selectors and passing an int value ignores the selector.
Looks like tileCount is an integer, try using a String value instead to fix the issue (the getter method of tileCount can be made to return a String instead of an int).

In Tritium, what's the difference between add_class and attribute

I can use add_class("classname") to add a class attribute to one of my elements, but I can also use attribute("class", "classname") to do the same.
What's the difference between the two functions? Any gotchas?
Yup, the tritium function add_class(...) will append the given argument to the class attribute in the node you're currently in (also prepending a space to separate it from other class names).
On the other hand, calling attribute("class", "classname") will actually clobber whatever class names already existed with the value you provided.
Below is an example illustrating both in tritium tester:
http://tritium.moovweb.com/43ecf5fdbc4bf6b07312372724df5a2522474cc3

How can getter/setter code be generated automatically for a class in Pharo or Squeak?

I have a long list of instance variables to create for a class that I want to generate the code for, rather than do it by hand. The list comes from an existing SQL database. My intention is to do it all in a pure object-oriented way with Smalltalk first, and as I learn more, save the data back to the database and work from it directly.
Is there a way of passing the list of names to method that will generate them and add them to the class definition?
In fact is there a way of adding or modifying class definitions dynamically in Smalltalk? I suspect there must and I would like to know a best practices approach.
Update: What I have in mind is more like passing a list of the instance variables to a method that will create them automatically.
It is more like:
addVariablesAndAccessors className: MyClass variablesList: ('aaaa', 'bbbb', 'cccc')
which will then result in a call to
AddVariables className: MyClass variableList: ('aaaa' 'bbbb' cccc')
and
generateAccessors className: MyClass variableList: ('aaaa' 'bbbb' cccc')
In OmniBrowser with the refactoring tools loaded you select the class and in the context menu Refactor class > Accessors.
Alternatively, if you only want to create an accessor for a single variable, select Refactor instance/class variable > Accessor, and select the variable you want to access.
In Squeak, you have Behavior>>addInstVarName: aString, so for instance, you could do something like:
String addInstVarName: 'foo'
Squeak also has refactoring support to generate accessors automatically. You can either use it directly or have a look at AbstractInstanceVariableRefactoring>>createAccessors to get some inspiration on how to implement your own ;-)
Another quite hacky but not so uncommon solution would be to just generate the instance variables, but instead of adding accessors, you overwrite doesNotUnderstand:, which gets called when an undefined selector is sent to your objects. There, you could check if you have an instance variable named according to the message, and return / change it if it is the case. Otherwise you just do super doesNotUnderstand: aMessage.
Regarding your comment: Classes are objects, too, so you don't have to do anything special to use them as parameters. On which class you add it is totally up to you and doesn't really matter. So a method to add instance variables could look like this:
addVariablesNamed: aCollection on: aClass
aCollection do: [:each | aClass addInstVarName: each]
and you could call it like this:
yourObject addVariablesNamed: #('foo' 'bar' 'baz') on: ClassX
You can find examples on how to generate accessor methods in the class CreateAccessorsForVariableRefactoring
In Squeak, open a Browser on the class. If you "right click" (I can never remember the button colours) the class name in the class list you'll get the standard context menu - "browse full (b)", and so on. Select "more..." and you'll see "create inst var accessors". Select that, and you'll get basic getters and setters for the instance variables.