Expression Language is not working in the JSP Pages - tooltwist

Expression Language is not working in the JSP Pages. We are just using JSP Expression Tag but it can throw Exception when the expression result becomes null.
Ex: <%= h.getHtml() %> . Instead we can use ${h.html}

The JSP code generated in ToolTwist widgets you write is plain standard JSP code. While it is possible to use Java Beans, EL, tag libraries and various other features of JSP, most projects find that sticking with the most basic JSP expressions simplifies future maintainability, because the meaning of the code is clearest.
Most tags are designed to strip down the JSP code and make it more HTML-like, at the expense of using special semantics or moving the logic elsewhere. This separation is useful when the JSP will be maintained by a web designer who is not familiar with the application logic, but in the case of ToolTwist that objective is more or less redundant, because pages are changed using the Designer, not by editing JSP code.
Most widgets are written once, and then infrequently changed, so the important consideration is making the code easy to understand at a glance, without needing to think about or investigate any special semantics or tag library definitions. Using the simplest JSP syntax makes debugging and testing easier, and will be appreciated by any programmer who needs to look at the code in the future.
Note that in this context simplest means "easiest to understand" rather than "fewest lines of code."
Regarding your exception, remember that the code within <%= and %> is standard Java code. If the Java code throws an exception, then the JSP code throws an exception. In this case, getHtml() is a method written by you, so if you wish to use it as a string you should probably ensure it does not return null. Alternatively, check it's value before using it:
<%
String html = h.getHtml();
if (html == null) {
html = "";
}
%><%=html%>
Note that a common bad habit is to call the method twice. Best not to do this:
<%= (h.getHtml() == null) ? "" : h.getHtml() %>
Overall, I'd recommend that you change your getHtml() method to return "" where it currently returns null.

Related

Endeca Assembler: Customize response header

I want to insert a new (key -> value) pair in response header from Endeca Assembler. Is it possible to do this?
Thanks
First of all, I want to clarify some things because the terminology about Assembler can get a little confusing. I'm not sure how you have designed your program, but just keep in mind that Assembler is just a Java API, so it's kind of unclear to say something like "response header from Endeca Assembler". That statement seems to imply that Assembler is a webservice, but it isn't. In my experience, people commonly mistakenly refer to the discover-data (Discover service) example app as "Assembler" or "Assembler Service", but it really isn't a general-purpose webservice; it's designed as a reference application to be used specifically with the Discover dataset (But people still use discover-data as a starting point for building production-facing applications). So, bear in mind that I'm not exactly sure what you are referring to.
Anyway, somewhere in your code, you should have a call of something like "contentItem.assemble()", which runs your cartridge handlers on that content item and returns an object of type ContentItem. In the Discover webapp, it then serializes this content item to JSON or XML or renders a JSP page (depending on the request parameters). I assume your application does something similar.
It's a simple matter of adding properties to ContentItem, because ContentItem implements map. So, you can do something like this:
ContentItem responseContentItem = contentItem.assemble();
responseContentItem.put("myKey","myValue");
...continue by serializing responseContentItem or whatever you want to do with it
Do like this:
responseContentItem.put("key", "value");
as the resposne from Endeca Assembler is simply a Map.

How does Groovy work in a dynamic way?

I have built a small compiler, for a statically typed language. After understanding how a static language works, I'm having trouble getting my head into dynamic languages like groovy.
While constructing my compiler, I know that once I generate the machine level-code there is no way of changing it! (i.e its run-time).
But how does Groovy do this magical stuff like inferring type in statements like:
def a = "string"
a.size()
As far as I'm concerned, groovy has to find the type a is of string before running the line a.size(). It seems that it does so in compile time (while constructing AST)! But the language is called dynamic.
I'm confused, kindly help me figure out.
Thanks.
Groovy doesn't simply "call" a method, but dispatches it through the meta-object protocol. The method invocation is sent as a message to the object, which can respond to it or not. When using dynamic typing, it doesn't matter the object type, only if it responds to that message. This is called duck typing.
You can see it (though not easily) when you decompile Groovy code. You can compile using groovyc and decompile using other tool. I recommend jd-gui. You won't see the method being called explicitly, because of Groovy's method caching (it is done this way to achieve Groovy's neat performance).
For a simple script like:
def a = "abcdefg"
println a.substring(2)
This will be the generated code:
CallSite[] arrayOfCallSite = $getCallSiteArray(); Object a = "abcdefg";
return arrayOfCallSite[1].callCurrent(
this, arrayOfCallSite[2].call(a, Integer.valueOf(2))); return null;
And the method call is "dispatched" to the object, not called directly. This is a similar concept to Smalltalks and Ruby method dispatch. It is because of that mechanism that you can intercept methods and property access on Groovy objects.
Since Groovy 2, Groovy code can be statically compiled, thus acting like your compiler.

Has Freemarker something similar to toolbox.xml-file of Velocity?

I have a Struts 1 application which works with Velocity as a template language. I shall replace Velocity with Freemarker, and am looking for something similar to 'toolbox.xml'-File from VelocityViewServlet. (there you can map names to Java Classes and, using these names it is possible to access methods and variables of various Java class in the Velocity template).
Does someone know, what is possible with Freemarker instead? So far I have found only information about the form beans...would be glad if someone can help....
For the utility functions and macros that are View-related (not Model-related), the standard practice is to implement them in FreeMarker and put them into one or more templates and #import (or #include) them. It's also possible to pull in TemplateDirectiveModel-s and TemplateMethodModelEx-es (these are similar to macros and function, but they are implemented in Java) into the template that you will #import/#inlcude as <#assign foo = 'com.example.Foo'?new()>.
As of calling plain static Java methods, you may use the ObjectWrapper's getStaticModels() (assuming it's a BeansWrapper subclass) and then get the required methods as TemplateMethodModelEx-es with staticModels.get("com.example.MyStatics"). Now that you have them, you can put them into the data-model (Velocity context) in the Controller, or pick methods from them in an #import-ed template, etc. Of course, you can also put POJO objects into the data-model so you can call their non-static methods.
The third method, which is not much different from putting things into the data-model is using "shared variables", which are variables (possibly including TemplateMethodModelEx-es and TemplateDirectiveModel-s) defined on the Configuration level.

Implementing Rails 3 template handlers

It appears there's not much documentation on Rails template handlers. There's the included handlers, like RJS, ERB, and Builder, which offer some assistance.
I'm trying to implement my own, and I've succeeded, albeit with a bit of weird code, or possibly there's something I don't quite understand.
class MyHandler < ActionView::Template::Handler
def call(template)
template.source.inspect
end
end
So what's weird is that I have to call inspect, otherwise Rails will try to eval the string as Ruby code.
I was under the impression that that's what include ActionView::...::Compilable did (which I'm not including in my code).
Now, if I make my template "compilable" (by using the include... statement), it still looks for the call method instead of the compile method.
So could anyone explain to me a bit more about how this works?
Thanks!
I've just been going through this problem myself. Basically rails expects the renderer's .call method to return ruby code that will render your template. It then dynamically generates a method which runs this code, and injects it into a module.
The module has all of the url/application helpers included, which means they're in scope for the template.
So, in answer to your question the solution is for .call to return some ruby code that outputs your rendered template as a string, or for it to render ruby code that invokes your template engine.
Check out tilt and temple, I have learnt a lot about template engines reading their code.

Check for "Root Element is missing", when using XDocument.Parse?

I'd like to hear, how you check for "Root element is missing", when using XDocument.Parse(); Currently, I'm using a try-catch, to catch the error, but I'd like to hear, if any of you have a more clever way to do it - personally, I'd like to avoid errors, instead of caching them.
I should clarify, the string, which I'm parsing, is returned from WebClient.DownloadString(...);, and therefor, I'm NOT creating the XML myself.
Best regards.
try/catch exception handling is perfectly normal and usual programming style in the .NET framework. And XML has strict syntax rules which the XML parser checks when parsing markup so you need to be prepared to handle any parsing error, try/catch is the right tool for that.
You haven't said whether you control the creation of the string argument you pass to XDocument.Parse. If you don't control it then you can't avoid errors. If you control then make sure you don't use string concatenation or StringBuilders to construct strings with XML, instead make sure you use XML APIs like XmlWriter, that way you would get well-formed XML markup or you would get any error when constructing your markup, not when parsing it. But any errors thrown by XmlWriter are also best handled with try/catch.