freemarker 使用jsp标签

freemarker 可以使用jsp标签,但只有你用了他的 freemarkerservlet,这个功能才能有效。如果你想生成静态文件那么这样就不行了

使用jsp标签的方法  使用心得

<#globalhm=JspTaglibs["/WEB-INF/struts-html.tld"]>

<@hm.formaction="logon.do"method="post">

<inputname="username"type="text"/>

<inputname="password"type="password"/>

<inputtype="submit"name="submit"></button>

</@hm.form>

<@include_pagepath="/error.jsp"/>

可以这样来支持jsp标签

也可以支持jsp:include这个标签

其本质是在model中添加了对jsptaglibs和tabinclude的标记

这是freemarkerservlet中对templateModel的初始化从以下红色代码可以看出,想要自己处理jsptaglibs而不去调用它的servlet只需要在自己的model中添加上这个值对就可以了,应用环境servlet

需要注意的一个问题使用include这个标签是由于传入的是request和response这两个参数,include来的内容会写入到response中,而不是自己指定的out中,所以这部分可能需要进行一些修改,如果只是其用来做表示层那么很容易(本来就是这么做的)

protectedTemplateModelcreateModel(ObjectWrapperwrapper,

ServletContextservletContext,

finalHttpServletRequestrequest,

finalHttpServletResponseresponse)throwsTemplateModelException{

try{

AllHttpScopesHashModelparams=newAllHttpScopesHashModel(wrapper,servletContext,request);

//Createhashmodelwrapperforservletcontext(theapplication)

ServletContextHashModelservletContextModel=

(ServletContextHashModel)servletContext.getAttribute(

ATTR_APPLICATION_MODEL);

if(servletContextModel==null)

{

servletContextModel=newServletContextHashModel(this,wrapper);

servletContext.setAttribute(

ATTR_APPLICATION_MODEL,

servletContextModel);

TaglibFactorytaglibs=newTaglibFactory(servletContext);

servletContext.setAttribute(

ATTR_JSP_TAGLIBS_MODEL,

taglibs);

initializeServletContext(request,response);

}

params.putUnlistedModel(KEY_APPLICATION,servletContextModel);

params.putUnlistedModel(KEY_APPLICATION_PRIVATE,servletContextModel);

params.putUnlistedModel(KEY_JSP_TAGLIBS,(TemplateModel)servletContext.getAttribute(ATTR_JSP_TAGLIBS_MODEL));

//Createhashmodelwrapperforsession

HttpSessionHashModelsessionModel;

HttpSessionsession=request.getSession(false);

if(session!=null){

sessionModel=(HttpSessionHashModel)session.getAttribute(ATTR_SESSION_MODEL);

if(sessionModel==null||sessionModel.isZombie()){

sessionModel=newHttpSessionHashModel(session,wrapper);

session.setAttribute(ATTR_SESSION_MODEL,sessionModel);

if(!sessionModel.isZombie()){

initializeSession(request,response);

}

}

}

else{

sessionModel=newHttpSessionHashModel(this,request,response,wrapper);

}

params.putUnlistedModel(KEY_SESSION,sessionModel);

//Createhashmodelwrapperforrequest

HttpRequestHashModelrequestModel=

(HttpRequestHashModel)request.getAttribute(ATTR_REQUEST_MODEL);

if(requestModel==null||requestModel.getRequest()!=request)

{

requestModel=newHttpRequestHashModel(request,response,wrapper);

request.setAttribute(ATTR_REQUEST_MODEL,requestModel);

request.setAttribute(

ATTR_REQUEST_PARAMETERS_MODEL,

createRequestParametersHashModel(request));

}

params.putUnlistedModel(KEY_REQUEST,requestModel);

params.putUnlistedModel(KEY_INCLUDE,newIncludePage(request,response));

params.putUnlistedModel(KEY_REQUEST_PRIVATE,requestModel);

//Createhashmodelwrapperforrequestparameters

HttpRequestParametersHashModelrequestParametersModel=

(HttpRequestParametersHashModel)request.getAttribute(

ATTR_REQUEST_PARAMETERS_MODEL);

params.putUnlistedModel(KEY_REQUEST_PARAMETERS,requestParametersModel);

returnparams;

}catch(ServletExceptione){

thrownewTemplateModelException(e);

}catch(IOExceptione){

thrownewTemplateModelException(e);

}

}

相关推荐