
net.dreamlu.ext.render.JadeRender Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JFinal-jade4j Show documentation
Show all versions of JFinal-jade4j Show documentation
JFinal-jade4j is a plugin for Java Web Framework JFinal.
The newest version!
package net.dreamlu.ext.render;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import com.jfinal.core.JFinal;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import de.neuland.jade4j.JadeConfiguration;
import de.neuland.jade4j.template.JadeTemplate;
/**
* JFinal-jade-extension
* @author L.cm
*/
public class JadeRender extends Render {
private transient static final String encoding = getEncoding();
private transient static final String contentType = "text/html; charset=" + encoding;
JadeConfiguration config = null;
public JadeRender(JadeConfiguration config, String view) {
this.config = config;
this.view = view;
}
@SuppressWarnings("unchecked")
public void render() {
response.setContentType(contentType);
Map model = new HashMap();
// request paras
Enumeration paras = request.getParameterNames();
while (paras.hasMoreElements()) {
String parasName = paras.nextElement();
model.put(parasName, request.getParameter(parasName));
}
// request attrs
Enumeration attrs = request.getAttributeNames();
while (attrs.hasMoreElements()) {
String attrName = attrs.nextElement();
model.put(attrName, request.getAttribute(attrName));
}
// session attrs
HttpSession httpSession = request.getSession(false);
if (null != httpSession) {
Map session = new JadeSession(httpSession);
for (Enumeration names = httpSession.getAttributeNames(); names.hasMoreElements();) {
String name = names.nextElement();
session.put(name, httpSession.getAttribute(name));
}
model.put("session", session);
}
model.put("ctxPath", JFinal.me().getContextPath());
// request and response
model.put("request", request);
model.put("response", response);
// render model
PrintWriter writer = null;
try {
JadeTemplate template = config.getTemplate(view);
writer = response.getWriter();
config.renderTemplate(template, model, writer);
} catch (Exception e) {
throw new RenderException(e);
}
finally {
if (writer != null) {
writer.close();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy