???????????????????h1????з?????????????“user”??Freemarker????????“user”????????????HashMap?????
??????????Controller???????web.xml??????????????example.Test?????????????????£????????“example”????????????????????Servlet??Servlet???????趨?Test?????????????????????????????е???????????????????μ??
packageexample;
importjava.io.*;
importjava.util.*;
importjavax.servlet.ServletException;
importjavax.servlet.http.*;
importfreemarker.template.*;
publicclassTestextendsHttpServlet{
privateConfiguration_config;
publicvoidinit(){
//?????Freemarker????
_config=newConfiguration(Configuration.VERSION_2_3_0);
//????Freemarker????????λ??
_config.setServletContextForTemplateLoading(this.getServletContext()??"templates");
}
publicvoiddoGet(HttpServletRequestrequest??HttpServletResponseresponse)throwsServletException??IOException{
//????
Maproot=newHashMap();
root.put("user"??"Jack");
//??????
Templatet=_config.getTemplate("Test.ftl");
//??????????????????????????charset
response.setContentType("text/html;charset="+t.getEncoding());
PrintWriterout=response.getWriter();
try{
//??????м????????
t.process(root??out);
}
catch(freemarker.template.TemplateExceptione){
thrownewServletException("????Template????г??????"??e);
}
}
}
????????_config.setServletContextForTemplateLoading(this.getServletContext()??"templates");?????????????????????????????Controller?????????????????????????????????????WebRoot?′?????templates?????????????Templatet=_config.getTemplate("Test.ftl");??????????????????????????Maproot=newHashMap();root.put("user"??"Jack");?????????????Model?????key??????????????趨??${user}???????????t.process(root??out);?У???????????????С???????????MVC?????????????????
????????index.jsp???????????????<ahref="Hello.do">Freemarkerfirstpage</a>?????????????????????????Ч???ɡ?
????Freemarker??????????????????????檔????????н?????????????滻?????????????????????á?