net.ymate.platform.mvc.web.view.impl.JspView.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.platform.mvc.web.view.impl.JspView.java

Source

/*
 * Copyright 2007-2107 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.platform.mvc.web.view.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Map.Entry;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import net.ymate.platform.mvc.web.WebMVC;
import net.ymate.platform.mvc.web.context.WebContext;
import net.ymate.platform.mvc.web.view.AbstractWebView;

import org.apache.commons.lang.StringUtils;

/**
 * <p>
 * JspView
 * </p>
 * <p>
 * JSP
 * </p>
 * 
 * @author (suninformation@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">?</th><th width="100px"></th><th
 *          width="100px"></th><th width="100px"></th>
 *          </tr>
 *          <!--  Table ?? -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td></td>
 *          <td></td>
 *          <td>2011-7-24?06:49:28</td>
 *          </tr>
 *          </table>
 */
public class JspView extends AbstractWebView {

    /**
     * JSP
     */
    protected String path;

    /**
     * 
     */
    public JspView() {
    }

    /**
     * 
     * 
     * @param path
     */
    public JspView(String path) {
        this.path = path;
    }

    /**
     * 
     * 
     * @param path
     * @param key
     * @param value
     */
    public JspView(String path, String key, Object value) {
        this.path = path;
        this.addAttribute(key, value);
    }

    protected void processPath() {
        if (StringUtils.isNotBlank(getContentType())) {
            WebContext.getResponse().setContentType(getContentType());
        }
        for (Entry<String, Object> entry : getAttributes().entrySet()) {
            WebContext.getRequest().setAttribute(entry.getKey(), entry.getValue());
        }
        //
        String _viewBasePath = this.getBaseViewPath();
        if (StringUtils.isBlank(this.path)) {
            String _mapping = WebContext.getWebRequestContext().getRequestMapping();
            if (_mapping.charAt(0) == '/') {
                _mapping = _mapping.substring(1);
            }
            if (_mapping.endsWith("/")) {
                _mapping.substring(0, _mapping.length() - 1);
            }
            this.path = _viewBasePath + _mapping + ".jsp";
        } else {
            if (!this.path.startsWith("/")) {
                this.path = _viewBasePath + this.path;
            }
            if (this.path.indexOf('?') == -1 && !this.path.endsWith(".jsp")) {
                this.path += ".jsp";
            }
        }
    }

    /* (non-Javadoc)
     * @see net.ymate.platform.mvc.web.view.AbstractWebView#renderView()
     */
    protected void renderView() throws Exception {
        this.processPath();
        WebContext.getRequest().getRequestDispatcher(this.path).forward(WebContext.getRequest(),
                WebContext.getResponse());
    }

    /* (non-Javadoc)
     * @see net.ymate.platform.mvc.web.view.AbstractWebView#render(java.io.OutputStream)
     */
    public void render(final OutputStream output) throws Exception {
        // JSP?????UTF-8
        this.processPath();
        final ServletOutputStream _oStream = new ServletOutputStream() {
            @Override
            public void write(int b) throws IOException {
                output.write(b);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                output.write(b, off, len);
            }
        };
        final PrintWriter _printWriter = new PrintWriter(new OutputStreamWriter(output));
        HttpServletResponse _newResponse = new HttpServletResponseWrapper(WebContext.getResponse()) {
            @Override
            public ServletOutputStream getOutputStream() {
                return _oStream;
            }

            @Override
            public PrintWriter getWriter() {
                return _printWriter;
            }
        };
        _newResponse.setCharacterEncoding(WebMVC.getConfig().getCharsetEncoding());
        WebContext.getRequest().getRequestDispatcher(this.path).include(WebContext.getRequest(), _newResponse);
        _printWriter.flush();
    }

}