How to configure Maven Freemarker to recognize `#include_page` tag? - freemarker

I'm trying to use the #include_page macro from FreemarkerServlet - using Maven Freemarker framework with Freemarker version 2.3.31 and I can see the notes about this tag being now available in the source of it, but it still is not resolved/recognized in the actual template output.
For context:
Everything is fully configured and working as expected with pure ftl
We have a dependency on using JSP-only templates for a part of the output (while the main code base is using only .ftl templates)
The #include_page allows to include jsp pages into ftl
How I'm trying:
In WEB-INF/views directory, let's say there is a home.ftl
Add hello.jsp with <h2>Hello!</h1>
In home.ftl
<#assign....>
.
.
.
<div>
<#include_page path="hello.jsp"/>
</div>
.
.
.
This results in an error that statement on the line with include_page was not found on null followed by instructional on using defaults or ifs
Does anyone know if there is some additional configuration needed to make use of it?

The issue was that we needed to add the
model.put( FreemarkerServlet.KEY_INCLUDE, new IncludePage( request,
response ) );
to the FreemarkerView model.
This is the post that actually solves the problem.

To use JSP support (and include_page is part of it) you have to invoke the template with forwarding to an URL that's mapped to the FreemarkerServlet, instead of directly calling FreeMarker with its Java API. It's just how JSP works too (or have worked, originally). See https://freemarker.apache.org/docs/pgui_misc_servlet.html for details.

Related

How to add prefix in URI while loading XQuery file using ml-gradle

I am using gradle 6.8 and MarkLogic version is 10.0-5.2,
My XQuery code is in directory \ml-gradle\src\main\common. When I run the command mlLoadModules to load XQuery into the modules database it loads with default URI /common/test.xqy.
I want to add some prefix to the URIs e.g. /rsc/common/test.xqy. How can I achieve that?
Note: I don't want to create an extra folder in my source for prefix "rsc".
It's not supported, though you could write a custom Gradle task to change the URI to whatever you like.
Why do you not want to create an "rsc" folder above "common"? I think other developers would find it much more intuitive that "rsc/common/test.xqy" becomes "/rsc/common/test.xqy" when loaded, rather than "common/test.xqy" becomes "rsc/common/test.xqy", which begs the question - where is "rsc" coming from? And then that developer would need to understand what property / custom code is adding that "rsc".

cannot create a spring template project using STS

While creating a new spring template project, I keep getting the following error -
Input for field toplevelpackage does not match the regex \w+\.\w+(\.\w+)+.
I tried all combinations for top level package names. But none seem to work
com.springtest
com.springtest.*
com.springtest*
We cant leave this field empty. :-(
I am using STS version of 2.9.1
You need to define a top package name with 3 or more parts.
for example
com.mycompany.myapp
BTW: That is exact what the regex and the example in the dialog say

Getting request attributes in freemarker

How do I check a value from the request attribute in freemarker?
I tried <#if *${RequestParameters['servicesettings']} ??> but getting errors ->
Encountered "*" at line
Can anyone help?
It depends on the Web application framework, because FreeMarker itself doesn't expose the request parameters. (Well, except if the framework uses freemareker.ext.servlet.FreemarkerServlet which is kind of an extension to FreeMarker.) Also, usually you shouldn't access request parameters directly from an MVC template, or anything that is HTTP/Servlet specific.
As of the error message, what you have written has a few syntax errors... probably you meant <#if RequestParameters.servicesettings??> (it's not JSP - don't use ${...}-s inside FreeMarker tags). This will require that you have RequestParameters in the data-model, that I can't know for sure...
We should write like this:
${Request.requestattribute}
You can use
${requestParameters.servicesettings}.
According to the JavaDoc of the FreemarkerServlet:
It makes all request, request parameters, session, and servlet context attributes available to templates through Request, RequestParameters, Session, and Application variables.
The scope variables are also available via automatic scope discovery. That is, writing Application.attrName, Session.attrName, Request.attrName is not mandatory; it's enough to write attrName, and if no such variable was created in the template, it will search the variable in Request, and then in Session, and finally in Application.
You can simply write:
${attrName}
to get the value of a request attribute (that you might have set in a servlet request filter using request.setAttribute('attrName', 'value')
Worked for me with Freemarker 2.3.27-incubating

Placeholder substitution in a Maven APT page

I am working on Maven site documentation for some related projects, and I want to create a hyperlink from one site's documentation to another sites documentation. The wrinkle is that the URL for the link target depends on a Maven property.
I tried this:
{{{http://example.com/site/project-${some-prop}/some.html}the documentation}}
but the ${some-prop} placeholder doesn't get replaced, and the APT processor then gets
confused by the first '}'. Escaping the { and } characters (e.g. $\{some-prop\} ) doesn't help, and the following doesn't work either:
{{http://example.com/site/project-${some-prop}/some.html}}
Is there some other way that I can accomplish this task? For example, is there a way to define a Doxia macro that could be used to substitute a URL that had the placeholder expanded?
Are you using version 2.0-beta-6 or later of the site plugin? Also, does your apt filename ends with a .vm extension (as described in the filtering section of Creating Content?

how to debug a gsp page? (no grails, just gsp)

I've tried with netbeans and eclipse, with no luck... (coudn't try IntelliJ idea)
I gave a quick look ant the code
http://kickjava.com/src/groovy/servlet/TemplateServlet.java.htm
and it gives me the impression that .gsp pages are translated to .groovy servlets (groovlets) in memory (I might be wrong)...
so perhaps it's not so easy to debug gsp as I though...
so, can anybody tell me how to do it?
pd: By debugging I mean things like browsing the code step-by-step, inspecting variables, adding watches, and all those sort of things, obviously. Not the (not so) good old printf approach...
Most of the logic in your GSPs should be encapsulated in TagLibs, and you can debug them (with IntelliJ at least), just as easily as any other Groovy code.
If you do have a lot of scriptlet code in your GSPs (which you shouldn't), and you want to debug into it, you can't do much more than println. One other possibility is to view the source of the Groovy code generated for your GSP. This can be done by appending a showSource parameter to the URL, as described here.
I use a hack: add a method to any controller, like static def debugme(def param) { def a = param }, and call it from gsp code: <% ThisController.debugme(this) %>, or <% ThisController.debugme(params) %>
(you do know you don't have to restart application after editing a controller or view, right?)
I also don't think that all the logic should be in taglibs: page-specific logic should be clearly visible in a controller or a view. We have most of the logic in controllers or domain classes.
Add a setting to Config.groovy and the generated gsp-files will be written to a directory: grails.views.gsp.keepgenerateddir='/some/existing/directory'
(the target directory has to exists and be writable)
More information:
http://jira.codehaus.org/browse/GRAILS-4422
It should be possible to debug the generated groovy code with a standard Java debugger.
It was a long time ago when I did that (when I created the patch to grails) and I think I used jswat (http://code.google.com/p/jswat/) to debug gsps. I couldn't get eclipse to find the source files, but that's probably working in Spring Tool Suite Eclipse nowadays.
You have to debug groovy code step-by-step/step-into and use filters, otherwise you might lose the step point (because of closures?). That's already another story...

Resources