问题
I want to create a custom template engine like velocity or freemarker which will be used in struts 2 based application. Why I don't want to use any of the available template engines is because, I want to keep the HMTL fixed and editable by dreamweaver means no struts tags or JSTL. The values will be injected with Xpath or simple string replacement of values of existing HTML tags. I require:
plain HTML + some configuration (properties/xml) + data =>
HTML populated with data + some dynamically generated javascripts
回答1:
1) Define a package with the name of your result type and the class that will be called when an action returns that result type.
<package name="default" namespace="/" extends="struts-default">
<result-types>
<result-type name="myResultType" class="com.awesome.MyResult"/>
</result-types>
.... actions and other things...
</package>
2) Implement the Struts 2 result type class:
package com.awesome;
public class MyResult extends StrutsResultSupport{
//1) read the the target file
//2) process/transform the target file
//3) write out the transformed file as the result
}
There is a good description of this in "Apache Struts 2 web application Development" by Dave Newton. I know the above class isn't implemented but I bet you can find what you need from here.
来源:https://stackoverflow.com/questions/4375653/custom-template-engine-for-struts2