File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Exception occurs when using s2 tags in *.ftl Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Exception occurs when using s2 tags in *.ftl" Watch "Exception occurs when using s2 tags in *.ftl" New topic
Author

Exception occurs when using s2 tags in *.ftl

aran tam
Greenhorn

Joined: Jun 24, 2009
Posts: 12
HI, guys, i need your help.
i am using freemark generating html, and here is what i do:
1. copy the tag-lib "struts-tags.tld" from struts-core-2.1.6.jar and put it in "/WEB-INF/". then wrap the <tag-lib></tag-lib> with <jsp-config></jsp-config>.
2. add the following config in "web.xml":

3. put this code in the head of a .ftl file:

4. code in the action method is:


but when running the app, exception occurs:


anyone also comes across this problem ? i just can't get it through...

thanks in advance...
sachin burange
Ranch Hand

Joined: Jun 24, 2008
Posts: 41
configuration problem. try to put necessary things in classpath.


Regards
Sachin
aran tam
Greenhorn

Joined: Jun 24, 2009
Posts: 12
sachin burange wrote:configuration problem. try to put necessary things in classpath.

thanks!
but what should i put?
i use struts2, and necessary jars have been set in classpath:

commons-fileupload.jar
commons-logging-1.0.4.jar
freemarker-2.3.13.jar
ognl-2.6.11.jar
struts2-convention-plugin-2.1.6.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar


waiting for more details.
tons of thnx!!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not sure what, exactly, you're trying to do.

FreeMarker's tag support requires the template mechanism to be run through the FreemarkerServlet (or equivalent, like an S2 "freemarker" result).

See how FreeMarker supports custom tags in the Using FreeMarker with Servlets portion of the FreeMarker manual.
aran tam
Greenhorn

Joined: Jun 24, 2009
Posts: 12
David Newton wrote:I'm not sure what, exactly, you're trying to do.

FreeMarker's tag support requires the template mechanism to be run through the FreemarkerServlet (or equivalent, like an S2 "freemarker" result).

See how FreeMarker supports custom tags in the Using FreeMarker with Servlets portion of the FreeMarker manual.


hi, David. thanks a lot for your help and info.
and i've solved this pro now...
as it is, ftl can be rendered as view when we set "result type=freemarker" in s2 cfg, and the ftls support s2's tags.
but i jsut take use of freemarker to generate html files, and this is the right reason why the lousy exception occured. -- the result type! I returned no result and attempted to make it handled in the action method...
different from jsp, there must be an engine to render the code when using freemarker. but s2 has a special mechanism to process freemarker when setting "result type="freemarker". so i extract the source code of class "FreemarkerResult" and refact the method to write a local file rather than response to the client.
all i need to do is:
1. Create a java file ViewAndGenerateResult.java
2. copy the code from FreemarkerResult.java and modify it to generate file rather than response to the client.
3. register this result type.

well, so this is how i get it through... :)
thanks again. David.

btw, excuse my poor english.. :D

Aran Tam

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm curious why you're doing this, though!
aran tam
Greenhorn

Joined: Jun 24, 2009
Posts: 12
David Newton wrote:I'm curious why you're doing this, though!

hi, David.
i use jsp as view in my app and i want to change the pages that are not often updated into html pages, in order for higher loading speed and search engines. rather than response to client directly

i've try several ways of generating those html pages, such as IO, URL, JspWriter... and i found this is an effecient one for my laziness. :D
cuz i just need to get the templates ready and set "result=xxxFreemarker"(custom string), then the freemarker engine will do that job automaticly.

btw, do you have any good ideas how to handle this pro, please?
thank you. David.

aran tam
Greenhorn

Joined: Jun 24, 2009
Posts: 12
David Newton wrote:I'm curious why you're doing this, though!

hi, David!
now i've found a best way to handle this problem!! :D
i looked into the source code of FreemarkerResult.class again and found a method named "postTemplateProcess". it saids "Give subclasses a chance to hook into preprocessing".. yep!! that's it! so i extends "FreemarkerResult.class" and override the method "postTemplateProcess".
aha, so interesting a framework s2 is!

thanks again.
aran
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No problem... yes, there are lots of little hooks.

If you're just trying to cache pages, though, I'm not sure I'd approach it from the Struts 2 side but rather use a higher-level mechanism like a caching filter (or a file-writing filter, in your case, perhaps). Just an idea!
Damon Lv
Greenhorn

Joined: Nov 22, 2011
Posts: 1
aran tam wrote:
David Newton wrote:I'm not sure what, exactly, you're trying to do.

FreeMarker's tag support requires the template mechanism to be run through the FreemarkerServlet (or equivalent, like an S2 "freemarker" result).

See how FreeMarker supports custom tags in the Using FreeMarker with Servlets portion of the FreeMarker manual.


hi, David. thanks a lot for your help and info.
and i've solved this pro now...
as it is, ftl can be rendered as view when we set "result type=freemarker" in s2 cfg, and the ftls support s2's tags.
but i jsut take use of freemarker to generate html files, and this is the right reason why the lousy exception occured. -- the result type! I returned no result and attempted to make it handled in the action method...
different from jsp, there must be an engine to render the code when using freemarker. but s2 has a special mechanism to process freemarker when setting "result type="freemarker". so i extract the source code of class "FreemarkerResult" and refact the method to write a local file rather than response to the client.
all i need to do is:
1. Create a java file ViewAndGenerateResult.java
2. copy the code from FreemarkerResult.java and modify it to generate file rather than response to the client.
3. register this result type.

well, so this is how i get it through... :)
thanks again. David.

btw, excuse my poor english.. :D

Aran Tam



hey,can you tell me how to modify the FreemarkerResult.java?
I'm just starting to learn Freemarker.
I'd searched many solutions,but those're useless to me before I saw this.
Now, i can understand what you said in your document,but i don't know how to modify the file.
If you would tell me how to do it,you can send me an email.
My email account is:wm.0613@163.com.
Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception occurs when using s2 tags in *.ftl