All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nutz.plugins.view.JetTemplateView Maven / Gradle / Ivy

There is a newer version: 1.r.69.v20220215
Show newest version
package org.nutz.plugins.view;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jetbrick.template.JetEngine;
import jetbrick.template.JetTemplate;
import jetbrick.template.web.JetWebContext;
import jetbrick.template.web.JetWebEngine;

import org.nutz.lang.Lang;

/**
 * JetTemplate视图。
 * @author denghuafeng([email protected])
 *
 */
public class JetTemplateView extends AbstractTemplateViewResolver {
	private JetEngine engine;
	
	public JetTemplateView(String dest) {
		super(dest);
	}

	public void init(String appRoot,ServletContext sc) {
		engine = JetWebEngine.create(sc);
	}

	@Override
	public void render(HttpServletRequest req, HttpServletResponse resp,
			String evalPath, Map sharedVars) throws Throwable {
		String charsetEncoding = engine.getConfig().getOutputEncoding().name();
		resp.setCharacterEncoding(charsetEncoding);
		if (resp.getContentType() == null) {
			resp.setContentType("text/html; charset=" + charsetEncoding);
		}
		try {
			JetTemplate template = engine.getTemplate(evalPath);
			Iterator> iter = sharedVars.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = iter.next();
				String key = entry.getKey().toString();
				Object val = entry.getValue();
				req.setAttribute(key, val);
			}
			JetWebContext context = new JetWebContext(req, resp);
			template.render(context, resp.getOutputStream());
		} catch (IOException e) {
			throw Lang.wrapThrow(e);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy