Xpages Combobox setting values in managed bean - xpages

I am trying to pass arguments to a managed bean.
The bean is configured and works, has two methods "getResponsible" and "setResponsible".
Calling "myLookup.responsible" works.
I cannot pass arguments to my bean, and can't figure out why.
The below code does not work.
<xp:comboBox id="comboBox1">
<xp:selectItems>
<xp:this.value><![CDATA[#{myLookup.setResponsible("Something")}]]>
</xp:this.value>
</xp:selectItems>
</xp:comboBox>
As soon as I type paranthesis ")", "(" or semicolon ";" i get error "Error in EL syntax". I guess I am making some fundamental mistake here.

The version of expression language does not allow parameters to easily be passed. This option may work http://blog.defrog.nl/2012/04/settings-bean-parameterized-method-call.html.
If parameters are required, I usually use SSJS, so:
#{javascript:myLookup.setResponsible("Something");
If the options won't vary during the page's life, you can always compute on page load, so:
${javascript:myLookup.setResponsible("Something");

I think you just made a simple "typo" as Paul stated indirectly in his reply. You wrote Javascript code but did not include the "javascript:" in the beginning of your expression.
If, however, you do want to use arguments with EL then have a look at this very interesting article. I haven't tried it out myself yet (but am going to do shortly) - but the two different examples (have a look at the comments) seem very interesting when you want to use EL. And I prefer EL over SSJS.
/John

Related

Making dynamicContent control dynamic

I'm having difficulty getting a dynamicContent control to work the way I would like it. I found this bit of code in pasteBin and I think it might just be what I need, but I would like to understand what it is doing before I try implementing it.
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
xmlns:xe="http://www.ibm.com/xsp/coreex">
<xp:button value="Switch!" id="switchButton">
<xp:eventHandler event="onclick" submit="true"
refreshMode="partial" refreshId="dynamicCustomControl">
<xp:this.action><![CDATA[#{javascript:viewScope.controlName = 'cc_test2.xsp';
getComponent('dynamicCustomControl').show(null)}]]></xp:this.action>
</xp:eventHandler></xp:button>
<xe:dynamicContent id="dynamicCustomControl">
<xp:include id="customControlInluder">
<xp:this.pageName>
<![CDATA[${javascript:viewScope.controlName||"cc_test1.xsp"}]]>
</xp:this.pageName>
</xp:include>
</xe:dynamicContent>
</xp:view>
In particular I don't understand the syntax of this line:
<![CDATA[${javascript:viewScope.controlName||"cc_test1.xsp"}]]>
I prefer to use viewScope.get("controlName") rather than the short form viewScope.controlName but I don't understand the significance of the || in this line of code.
also the line
<xp:include id="customControlInluder>
is probably an inconsequential typo.
The process looks fairly simple and would appears that it would do the job for me. Just want to make sure I understand it before going down that road.
Edit and update ---
This very brief code snippet might just be one of the best kept secrets around. I have just worked through it and each of the Custom Controls displayed withing the dynamicCustomControl contains a dynamicContent control. So was able to very nicely embedded a dynamicContent inside a dynamicContent. Which to this point I was never really able to get to work correctly. Now it works very smoothly with minimal fuss & muss. Thanks for the comments and assistance.
The JavaScript code viewScope.controlName||"cc_test1.xsp" returns as result the value of viewScope.controlName if it is not
0
null
undefined
NaN
"" or '' (=empty string)
false
Otherwise it returns "cc_test1.xsp". You can find a detailed explanation here.
This is a short and tricky way of
viewScope.controlName ? viewScope.controlName : "cc_test1.xsp"
or
if (viewScope.controlName) {viewScope.controlName} else {"cc_test1.xsp"}
The id in <xp:include id="customControlInluder"> is not used in code so it doesn't matter how it is called although "customControlIncluder" would sound much better.
I always use the short version for scope variables for getting and setting values and haven't had issues with that yet.
Bill, think it is just saying "use the viewscope control name, and if null then use cc_test1.xsp". If the first case is true, then the second isn't evaluated.
JavaScript OR (||) variable assignment explanation

why does xPage process my Comments as though they weren't

I got a strange behavior in XPage, with commented Code.
I had a textfield with a lot of code in it which outputs html where I hit this issue.
While developing I had a lot of trouble with some fields other fields so I decided -to get a better idea what the field is doing- to copy the code from the troublefields to a comment inside my textfield to have the other code at my sight. But then the xPage started to behave strange until I found the issue.
The Code below is a example what caused my issue, it has two text fields in it one which sets a scope var and also has a comment which sets the same var but commented, ant another one which shows the ScopeVar. I thought this would output 'where I am' in the second text box but instead I got the 'Huhu I am here'.
<xp:text escape="true" id="computedField7">
<xp:this.value><![CDATA[#{javascript://
sessionScope.put("findme","where i am");
/* #{javascript:sessionScope.put("findme","HuHu I am here!");} */
return sessionScope.findme;}]]></xp:this.value>
</xp:text>
<xp:br></xp:br>
<xp:text escape="true" id="computedField6"
value="#{javascript:return sessionScope.findme;}">
</xp:text>
In my original Code where I hit this issue I wanted to comment the old #{} el out to use a JavaScript instead but keep the el in comment in the middle of the code.. same result. It seams that if you use #{ or ${ in a comment it will always get computed!
Got this fixed in notes 9. I am currently using 8.5.3.
Update:
As a small note: Be careful when using the dojoAttribute queryExpr because the query Looks like SSJS "${0}" and also gets interpreted as SSJS. I now use this:
<xe:this.queryExpr><![CDATA[${javascript:"*$\{0}*";}]]></xe:this.queryExpr>
to make it work. Thanks to Paul Stephen Withers for the tip with the \{.
This is a funny bug.
It is caused by pre-processor functionality of JavaScript interpreter. Normally, you can write #{javascript:...} in CSJS code to replace parts of code before it gets put to rendered page.
In your case it is SSJS. Again, the interpreter replaces the #{javascript:...} inside your SSJS code and thinks, work is done. This way you see the code on rendered page instead the result of executed code.
As a workaround, just delete # from /* #{javascript... and it will work like expected.
Is there a reason for having #{javascript:...} nested inside #{javascript:...} in your example. I'd strongly recommend against that, I wouldn't expect it to give good results.
Please see my answer also to stopping getClientId() computations in JS code. I think you're expecting it to work in a way it's not designed and unlikely to be changed to work.
Properties are just strings read at runtime from left to right. If "#{javascript:", "${javascript:" or "#{.." are found, the resulting code is run. Wrapping "//" around them or "/" and "/" either side has no effect, nor would I expect it to. The whole string is not passed to the parser, just the bit after "#{javascript".
The benefits of this are that you can get better performance by combining languages because only part of it gets passed to the parser and literal string (which is what the "/*" bit is) is just passed to the browser as it is. It means you can include EL and SSJS in a single value. I'm not sure you'd be able to do that if the change you're looking for was made.
For queryExpr, I'd suggest using ${javascript:"${0}"} instead - just escaping the {. See p121 XPages Extension Library.

Get client ID of composite component itself for usage in JavaScript

I have got some interesting issue for you. I am creating a composite component. I know, that I can use ui:param for storing value and reuse it. But what if I store to this variable some relative value (#{component.namingContainer.clientId}) and want to reuse it as a constant? It is difficult to explain - here you have my code:
<ui:param name="rdfaComp" value="#{component.namingContainer.clientId}"/>
This is in the beginning of the page - I want to store exactly this ID and then use nothing else by this variable. But later if I reuse it inside elements with own IDs, it is bad. It is interesting, what happens - JSF doesn't take the same value, but a relative one. It reads once more #{component.namingContainer.clientId}, not the fixed value.
How to solve this, could you help me, please? THanks a lot.
UPDATED
There is still one important condition: the variable rdfaComp have to be available immediately, because I reuse it in a Javascript function (on the same page). Like this:
<h:commandButton onclick="return selectText('#{rdfaComp}:editor', ...
I am afraid, it is necessarry to use relative ID chains like this: #{component.namingContainer.parent.namingContainer.parent.clientId} etd.
But it is really awful. Is there another solution?
The <ui:param> indeed merely creates an alias and the EL expression is still deferred and evaluated on every access in the very same context as where it's been referenced (and thus not where it's been declared! that explains your concrete problem). Besides, the <ui:param> is designed to be used on <ui:include>, <ui:composition> and <ui:decorate> only.
Use <c:set> instead. It's capable of immediately evaluating an EL expression and storing its result in either request, view, session or application scope via the scope attribute.
<c:set var="rdfaComp" value="#{component.namingContainer.clientId}" scope="request" />
Update: you actually wanted to get the client ID of the composite component itself, here's how you can get it:
<c:set var="rdfaComp" value="#{cc.clientId}" scope="request" />
It is solved - and I must apologise once more :-). I didn't describe correctly the issue - what I really wanted to do. I just wanted to work with the full client ID of the composite component. Finally I found a solution: #{cc.namingContainer.clientId}
Thanks anyway, your answers were very helpful!

Why EL gives me the wrong object as parameter between parenthesis?

Here's the situation:
In a rich:dataTable in an a4j:form, i create a a4j:commandLink to select the values and pass it to the bean with the jboss el action syntax
action="#{bean.myaction(myparameter)}"
This works without problem.
But If I re-render the form to filter the datatable with an ajax call, when I select the value, it gives me the wrong results: the index from the selection, but the data from before the filtering.
Any ideas?
Thank you Zack for giving me the right solution in only 5 minutes.
I think passing parameter in the action between parenthesis is more elegant but, hey: this works. :)
Thank you a lot.
P.s. I'm editing the title too.
Try using:
<a4j:commandLink action="#{bean.myaction}">
<f:param name="myparameter" value="paramValue" />
</a4j:commandLink>
and then access that parameter in your action via the requestParameter("myparameter") through the FacesContext.
As a side-note, this isn't jboss EL, it's unified expression language (EL). It's just a feature of JSP/JSF in general, as specified by Sun.
In addition to the Zack's answer, I would say that if you need to extend the EL expressions in order to have the ability to call method with parameters, you can use the EL Functors library:
action="#{bean.myaction$[myparameter].action}"
Is your datatable populated using a Collection annotated with #DataModel ? If so try removing it from the context when filtering so that it gets re-requested.
eg.
//In filter method
Contexts.removeFromAllContexts("yourDataModelCollection");
Putting the dataTable in a <a4j:region> worked for me. This way, you can still use JBoss EL parameters.

parameterised jsp:includes of stripes actions?

I've been trying to solve this, and have been getting stuck, so I thought I'd ask.
Imagine two ActionBeans, A and B.
A.jsp has this section in it:
...
<jsp:include page="/B.action">
<jsp:param name="ponies" value="on"/>
</jsp:include>
<jsp:include page="/B.action">
<jsp:param name="ponies" value="off"/>
</jsp:include>
...
Take it as read that the B ActionBean does some terribly interesting stuff depending on whether the "ponies" parameter is set to either on or off.
The parameter string "ponies=on" is visible when you debug into the request, but it's not what's getting bound into the B ActionBean. Instead what's getting bound are the parameters to the original A.action.
Is there some way of getting the behaviour I want, or have I missed something fundamental?
So are you saying that in each case ${ponies} on your JSP page prints out "on"?
Because it sounds like you are confusing JSP parameters with Stripes action beans. Setting a JSP parameter simply sets a parameter on that JSP page, that you can reference as shown above, it doesn't actually set anything on the stripes action bean.
The reason that this wasn't working was because of massaging done by our implementation of HttpServletRequest.
It works fine with the "normal" implementation.

Resources