net.groupbuy.template.directive.FlashMessageDirective.java Source code

Java tutorial

Introduction

Here is the source code for net.groupbuy.template.directive.FlashMessageDirective.java

Source

/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package net.groupbuy.template.directive;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import net.groupbuy.Message;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * ? - ?
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Component("flashMessageDirective")
public class FlashMessageDirective extends BaseDirective {

    /** "?"?? */
    public static final String FLASH_MESSAGE_ATTRIBUTE_NAME = FlashMessageDirective.class.getName()
            + ".FLASH_MESSAGE";

    /** ???? */
    private static final String VARIABLE_NAME = "flashMessage";

    @SuppressWarnings("rawtypes")
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            Message message = (Message) requestAttributes.getAttribute(FLASH_MESSAGE_ATTRIBUTE_NAME,
                    RequestAttributes.SCOPE_REQUEST);
            if (body != null) {
                setLocalVariable(VARIABLE_NAME, message, env, body);
            } else {
                if (message != null) {
                    Writer out = env.getOut();
                    out.write("$.message(\"" + message.getType() + "\", \"" + message.getContent() + "\");");
                }
            }
        }
    }

}