<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    CONAN ZONE

    你越掙扎我就越興奮

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      0 Posts :: 282 Stories :: 0 Comments :: 0 Trackbacks

    struts2默認貌似只是支持VelocityLayoutServlet

    那么如何使用VelocityLayoutServlet呢?

    首先,要自己寫點代碼了,寫個extends org.apache.struts2.dispatcher.VelocityResult 的類,取名曰:VelocityLayoutResult

    實現如下:

    package struts2.velocity;

    import java.io.OutputStreamWriter;
    import java.io.StringWriter;
    import java.io.Writer;

    import javax.servlet.Servlet;
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.jsp.JspFactory;
    import javax.servlet.jsp.PageContext;

    import org.apache.struts2.ServletActionContext;
    import org.apache.struts2.dispatcher.VelocityResult;
    import org.apache.struts2.views.JspSupportServlet;
    import org.apache.struts2.views.velocity.VelocityManager;
    import org.apache.velocity.Template;
    import org.apache.velocity.context.Context;

    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.util.ValueStack;
    import com.opensymphony.xwork2.util.logging.Logger;
    import com.opensymphony.xwork2.util.logging.LoggerFactory;

    public class VelocityLayoutResult extends VelocityResult {

    private static final long serialVersionUID = 6020934292083047099L;

    private static final Logger LOG = LoggerFactory.getLogger(VelocityLayoutResult.class);

    public static String KEY_SCREEN_CONTENT = "screen_content";
    public static String KEY_LAYOUT = "layout";
    public static final String PROPERTY_DEFAULT_LAYOUT = "tools.view.servlet.layout.default.template";
    public static final String PROPERTY_LAYOUT_DIR = "tools.view.servlet.layout.directory";
    public static final String PROPERTY_INPUT_ENCODING = "input.encoding";
    public static final String PROPERTY_OUTPUT_ENCODING = "output.encoding";
    public static final String PROPERTY_CONTENT_TYPE = "default.contentType";

    protected VelocityManager velocityManager;
    protected String defaultLayout;
    protected String layoutDir;
    protected String inputEncoding;
    protected String outputEncoding;
    protected String contentType;

    @Override
    public void setVelocityManager(VelocityManager mgr) {
       
    this.velocityManager = mgr;
       
    super.setVelocityManager(mgr);
    }

    @Override
    public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
       ValueStack stack 
    = ActionContext.getContext().getValueStack();

       HttpServletRequest request 
    = ServletActionContext.getRequest();
       HttpServletResponse response 
    = ServletActionContext.getResponse();
       JspFactory jspFactory 
    = null;
       ServletContext servletContext 
    = ServletActionContext.getServletContext();
       Servlet servlet 
    = JspSupportServlet.jspSupportServlet;

       velocityManager.init(servletContext);

       
    boolean usedJspFactory = false;
       PageContext pageContext 
    = (PageContext) ActionContext.getContext().get(ServletActionContext.PAGE_CONTEXT);

       
    if (pageContext == null && servlet != null) {
        jspFactory 
    = JspFactory.getDefaultFactory();
        pageContext 
    = jspFactory.getPageContext(servlet, request, response, nulltrue8192true);
        ActionContext.getContext().put(ServletActionContext.PAGE_CONTEXT, pageContext);
        usedJspFactory 
    = true;
       }

       
    try {
        String encoding 
    = getEncoding(finalLocation);

        String contentType 
    = getContentType(finalLocation);

        
    if (encoding != null) {
         contentType 
    = contentType + ";charset=" + encoding;
        }

        Template t 
    = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, finalLocation, encoding);

        Context context 
    = createContext(velocityManager, stack, request, response, finalLocation);
        StringWriter stringWriter 
    = new StringWriter();
        t.merge(context, stringWriter);
        context.put(KEY_SCREEN_CONTENT, stringWriter.toString());

        Object obj 
    = context.get(KEY_LAYOUT);
        String layout 
    = (obj == null? null : obj.toString();

        defaultLayout 
    = (String) velocityManager.getVelocityEngine().getProperty(PROPERTY_DEFAULT_LAYOUT);
        layoutDir 
    = (String) velocityManager.getVelocityEngine().getProperty(PROPERTY_LAYOUT_DIR);
        
    if (!layoutDir.endsWith("/")) {
         layoutDir 
    += '/';
        }

        
    if (!layoutDir.startsWith("/")) {
         layoutDir 
    = "/" + layoutDir;
        }

        defaultLayout 
    = layoutDir + defaultLayout;

        
    if (layout == null) {
         layout 
    = defaultLayout;
        } 
    else {
         layout 
    = layoutDir + layout;
        }

        Template layoutVm 
    = null;
        
    try {
         inputEncoding 
    = (String) velocityManager.getVelocityEngine().getProperty(PROPERTY_INPUT_ENCODING);
         layoutVm 
    = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, layout, inputEncoding);
        } 
    catch (Exception e) {
         LOG.error(
    "VelocityLayoutResult: Can't load layout \"" + layout + "\"" + e);

         
    if (!layout.equals(defaultLayout)) {
          layoutVm 
    = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, defaultLayout,
            inputEncoding);
         }
        }

        outputEncoding 
    = (String) velocityManager.getVelocityEngine().getProperty(PROPERTY_OUTPUT_ENCODING);
        Writer writer 
    = new OutputStreamWriter(response.getOutputStream(), outputEncoding);
        response.setContentType(contentType);

        layoutVm.merge(context, writer);
        writer.flush();
       } 
    catch (Exception e) {
        LOG.error(
    "Unable to render Velocity Template, '" + finalLocation + "'", e);
        
    throw e;
       } 
    finally {
        
    if (usedJspFactory) {
         jspFactory.releasePageContext(pageContext);
        }
       }

       
    return;
    }

    }

    然后在struts.xml中就可以配置了,我的配置如下:

    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd"
    >
    <struts>

    <constant name="struts.velocity.toolboxlocation" value="WEB-INF/toolbox.xml" />

    <constant name="struts.velocity.configfile" value="WEB-INF/velocity.properties" />
    <include file="struts-default.xml" />
    <package name="default" extends="struts-default">
    <result-types>
        
    <result-type name="velocity" class="struts2.velocity.VelocityLayoutResult"/>
       
    </result-types>
       
    <action name="hello" class="test.Hello" method="execute">
        
    <result name="success" type="velocity">result.vm</result>
       
    </action>
    </package>
    </struts>


    接下來就可以開始velocity layout之旅了






    posted on 2010-11-16 16:34 CONAN 閱讀(1670) 評論(0)  編輯  收藏 所屬分類: 模板
    主站蜘蛛池模板: 美女黄频a美女大全免费皮| 成人最新午夜免费视频| 麻豆69堂免费视频| 亚洲中文字幕无码一去台湾| 亚洲AV无码专区国产乱码电影| 免费少妇a级毛片| 亚洲人成电影网站免费| 中国人xxxxx69免费视频| 你懂得的在线观看免费视频| 草久免费在线观看网站| 无码一区二区三区亚洲人妻| 亚洲乱码中文字幕小综合| 亚洲欧洲在线观看| 亚洲av中文无码乱人伦在线r▽| 久久亚洲国产精品123区| 亚洲AV网站在线观看| 国产成人综合久久精品免费| 成全影视免费观看大全二| 青娱乐免费视频在线观看| 18禁成人网站免费观看| 久久久久久AV无码免费网站下载| 中文字字幕在线高清免费电影| 思思久久99热免费精品6| 色www免费视频| 免费无码AV一区二区| 免费看一级高潮毛片| 香港一级毛片免费看| 国产精品久久亚洲一区二区| 激情无码亚洲一区二区三区| 亚洲a∨无码精品色午夜| 亚洲精品成a人在线观看夫| 亚洲AV综合色区无码一二三区| 亚洲国产成人久久精品软件| 久久亚洲AV成人无码国产最大| 亚洲av无码专区青青草原| 99亚洲乱人伦aⅴ精品| 老司机精品视频免费| 一级人做人a爰免费视频| 精品久久久久久无码免费| a级毛片免费完整视频| 免费看男人j放进女人j免费看|