Thanks, Jerome!
I stepped out this problem after a hour's work.
1.I put template file in template directory which under
classpath:templates\people.vm,by the way,I use Eclipse to develope
these.
2.put velocity.properties under classpath:
resource.loader = file
file.resource.loader.description = Velocity File Resource Loader
file.resource.loader.class =
org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path = ./templates
file.resource.loader.cache = false
file.resource.loader.modificationCheckInterval = 2
3.load this properties file when restlet init:
public class TemplateRestlet extends Restlet {
Properties pros=new Properties();
public TemplateRestlet(){
super();
try {
pros.load(ClassLoader.getSystemResourceAsStream("velocity.properties"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void handle(Request request, Response response) {
TemplateRepresentation templateRepr= new TemplateRepresentation(
"people.vm",
MediaType.TEXT_HTML);
Map<String,Object> dataModel=new HashMap<String,Object>();
People people=new People(
(String)request.getAttributes().get("user"),
1,
""
);
...
dataModel.put(
"people",
people
);
templateRepr.setDataModel(dataModel);
try {
templateRepr.getEngine().init(pros);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setEntity(templateRepr);
}
}
On Fri, Feb 29, 2008 at 8:14 PM, Jerome Louvel <[EMAIL PROTECTED]> wrote:
>
> Hello,
>
>
> > Thanks Vincent,that's pretty way for restlet-1.1snapshot.I
> > used this way in restlet or resource:
> > TemplateRepresentation templateRepr= new TemplateRepresentation(
> > "/people.vm",
> > MediaType.TEXT_HTML);
> > ...
> > templateRepr.setDataModel(dataModel);
> > templateRepr.getEngine().setProperty(
> > VelocityEngine.FILE_RESOURCE_LOADER_PATH,
> > "E:/eclipse3.1RC3/workspace/RestletPractice/templates"
> > );
> > response.setEntity(templateRepr);
> >
> > I have a lot of problem about it:
> > 1.How to use the relative path to set "file resource loader
> > path" property of velocity engine?It must to fullfill all of path?
>
> You might be interested in this RFEs:
>
> "Accept Velocity templates as representations"
> http://restlet.tigris.org/issues/show_bug.cgi?id=268
>
> "Support template inclusion for Freemarker and Velocity"
> http://restlet.tigris.org/issues/show_bug.cgi?id=269
>
>
> > 2.How to set once when a Velocitytemplate is
> > initialized,without set again when you use this template?
> http://restlet.tigris.org/issues/show_bug.cgi?id=268
>
> Maybe you could configure the Velocity engine in your Application
> constructor instead of each time the TemplateRepresentation class is used?
>
> Best regards,
> Jerome
>
>
--
cleverpig
Location: Beijing
Address: Room 4018,No.A2 South Avenue Fuxingmen Beijing,P.R.China
Zipcode: 100031
Phone: 010-66415588-1113
MSN: [EMAIL PROTECTED]
QQ: 149291732
Skype: cleverpigatmatrix
My Facebook ID:cleverpig
My Blog: www.morpheus.org.cn
My Tags: del.icio.us/cleverpig
My Twitter: twitter.com/cleverpig
My Organization: www.beijing-open-party.org
一些值得关注的唧歪:
http://jiwai.de/t/jmatrix/
http://jiwai.de/t/db4o/
http://jiwai.de/t/matrix-community/