Re: Include JSP page in freemarker page
Attila Szegedi <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
On Oct 17, 2008, at 4:35 PM, doahh wrote:
>
> Thanks for the replies.
>
> Am I correct in reading your response that it is currently not
> supported?
Correct, however...
> I am using Struts2 with the Freemarker plugin and as such I don't
> have to do
> anything in order to get Freemarker templates to work (other than
> making
> sure that freemarker.jar is available).
... excuse my ignorance, but is Struts2 actually using our
FreemarkerServlet, or does it have its own servlet implementation? If
it has its own and doesn't use FreemarkerServlet, then our
improvements to FreemarkerServlet won't help you in the least, I'm
afraid.
(Regardless, this is something that would make sense to implement.)
> This is something that I could
> really do with getting working as the option to use JSP is not really
> available to me (my JSP/FTL files are stored in a Java Plugin
> Framework
> plugin.jar file and struts doesn't seem to be able to reference them
> without
> have the result type set to 'freemarker').
>
> I had a quick look into the FreemarkerServlet you mentioned but I am
> unsure
> how to use it in the way you suggest. I will carry on looking at it
> but
> could you suggest and possible ways to make it achieve what I am
> looking
> for?
Here's a diff against current 2.3 branch that would allow you to use
<@servlet_include path="... path to JSP ..."/>. Note it doesn't use a
*file path*, but rather does a HTTP servlet request include, using
javax
.servlet.http.HttpServletRequest.getRequestDispatcher().include(),
which uses a *URL path within your servlet container*. I'll eventually
commit this...
Index: /Users/aszegedi/Documents/projects/freemarker/branches/2.3/
freemarker/src/freemarker/ext/servlet/FreemarkerServlet.java
===================================================================
--- /Users/aszegedi/Documents/projects/freemarker/branches/2.3/
freemarker/src/freemarker/ext/servlet/FreemarkerServlet.java (revision
859)
+++ /Users/aszegedi/Documents/projects/freemarker/branches/2.3/
freemarker/src/freemarker/ext/servlet/FreemarkerServlet.java (working
copy)
@@ -59,6 +59,7 @@
import java.util.Calendar;
import java.util.Enumeration;
import java.util.GregorianCalendar;
+import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
@@ -72,6 +73,7 @@
import freemarker.cache.TemplateLoader;
import freemarker.cache.WebappTemplateLoader;
import freemarker.core.Configurable;
+import freemarker.core.Environment;
import freemarker.ext.jsp.TaglibFactory;
import freemarker.log.Logger;
import freemarker.template.Configuration;
@@ -77,6 +79,8 @@
import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
+import freemarker.template.TemplateDirectiveBody;
+import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateModel;
@@ -81,6 +85,7 @@
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
+import freemarker.template.TemplateScalarModel;
import freemarker.template.utility.StringUtil;
import java.util.Locale;
@@ -189,6 +194,7 @@
private static final String DEPR_INITPARAM_DEBUG = "debug";
public static final String KEY_REQUEST = "Request";
+ public static final String KEY_INCLUDE = "servlet_include";
public static final String KEY_REQUEST_PRIVATE =
"__FreeMarkerServlet.Request__";
public static final String KEY_REQUEST_PARAMETERS =
"RequestParameters";
public static final String KEY_SESSION = "Session";
@@ -483,8 +489,8 @@
protected TemplateModel createModel(ObjectWrapper wrapper,
ServletContext servletContext,
- HttpServletRequest request,
- HttpServletResponse response)
throws TemplateModelException {
+ final HttpServletRequest
request,
+ final HttpServletResponse
response) throws TemplateModelException {
try {
AllHttpScopesHashModel params = new
AllHttpScopesHashModel(wrapper, servletContext, request);
@@ -537,6 +543,35 @@
createRequestParametersHashModel(request));
}
params.putUnlistedModel(KEY_REQUEST, requestModel);
+ params.putUnlistedModel(KEY_INCLUDE, new
TemplateDirectiveModel()
+ {
+ public void execute(Environment env, Map params,
+ TemplateModel[] loopVars,
TemplateDirectiveBody body)
+ throws TemplateException, IOException
+ {
+ Object path = params.get("path");
+ if(path == null) {
+ throw new TemplateException(
+ "Missing required parameter 'path'",
env);
+ }
+ if(path instanceof TemplateScalarModel) {
+ try {
+ String strPath =
+
((TemplateScalarModel)path).getAsString();
+
request.getRequestDispatcher(strPath).include(
+ request, response);
+ }
+ catch(ServletException e) {
+ throw new TemplateException(e, env);
+ }
+ }
+ else {
+ throw new TemplateException(
+ "Expected a scalar model. 'path' is
instead " +
+ path.getClass().getName(), env);
+ }
+ }
+ });
params.putUnlistedModel(KEY_REQUEST_PRIVATE,
requestModel);
// Create hash model wrapper for request parameters
Attila.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user