beetl与sitemesh组合的时候sitemesh的拦截对beetl无作用

rubysoft 发布于 2013/11/13 17:00
阅读 841
收藏 1

@闲.大赋 你好,想跟你请教个问题: 使用 sitemesh进行布局的时候,web.xml中使用你文档中的servlet,Freemarker是用com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet,

将如下 BeeServlet 当做 *.html的解析


package org.web.apps.fxz.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bee.tl.core.BeeException;
import org.bee.tl.core.Template;
import org.bee.tl.ext.ServletGroupTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BeeServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3424135216699759688L;
	Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//super.doGet(req, resp);
		process(req,resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//super.doPost(req, resp);
		process(req,resp);
	}
	
	
	public void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String path = req.getRequestURI();
		logger.info("template path=" + path);
		Template template = ServletGroupTemplate.instance().getTemplate(path, req, resp);
		Enumeration<String> keys = req.getAttributeNames();
		String key = "";
		Object val = null;
		while (keys.hasMoreElements()) {
			key = keys.nextElement();
			val = req.getAttribute(key);
			template.set(key, val);
		}

		try {
			// response.setCharacterEncoding("UTF-8");
			template.getText(resp.getOutputStream());
		} catch (BeeException e) {
			// 既然没有设置错误处理器,GroupTemplate将使用默认处理,此异常不会被抛出
			e.printStackTrace();
		}
	}
}
web.xml如下:



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>test</display-name>
	<filter>
		<filter-name>sitemesh</filter-name>
		<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
	</filter>
	<filter>
		<filter-name>dispatcher</filter-name>
		<filter-class>org.express.webwind.DispatcherFilter</filter-class>
		<init-param>
			<param-name>PackageToScan</param-name>
			<param-value>org.bossware.web.apps.fxz</param-value>
		</init-param>
		<init-param>
			<param-name>TemplatePath</param-name>
			<param-value>/views</param-value>
		</init-param>
		<init-param>
		    <param-name>DefaultViewType</param-name>
		    <param-value>ftl</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>sitemesh</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>dispatcher</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<display-name>init</display-name>
		<listener-class>org.bossware.web.apps.fxz.servlet.InitListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>bee</servlet-name>
		<servlet-class>org.web.apps.fxz.servlet.BeeServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet>
		<servlet-name>freemarker</servlet-name>
		<servlet-class>com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class>
		<init-param>
			<param-name>TemplatePath</param-name>
			<param-value>/</param-value>
		</init-param>
		<init-param>
			<param-name>NoCache</param-name>
			<param-value>true</param-value>
		</init-param>

		<init-param>
			<param-name>template_update_delay</param-name>
			<param-value>0</param-value>
		</init-param>
		<init-param>
			<param-name>default_encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
		<init-param>
			<param-name>number_format</param-name>
			<param-value>0.##########</param-value>
		</init-param>
		<init-param>
			<param-name>auto_import</param-name>
			<param-value>/views/lib/ui.ftl as ui</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>freemarker</servlet-name>
		<url-pattern>*.ftl</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>bee</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

会发现 对于 Freemarker的*.ftl,sitemesh返回正确的装饰;而*.html的Bee模板Servlet则无法返回正确的装饰。这也是我无法将Freemarker 替换成Bee 的最主要的一个原因。应该如何解决呢?

是Bee不支持还是我的servlet写的不对?

加载中
0
闲大赋
闲大赋

BeeServlet  执行了process方法后,没有任何输出么?

看web.xml似乎少了对Beetl的一些初始化配置,比如模板根目录等。文档提供了一个可方案 在http://ibeetl.com/guide/Linstener.html

rubysoft
rubysoft
BeeServlet输出模板内容是正常的。只是没有sitemesh的装配内容(就是没有把sitemesh的decorator的内容加到 bee模板中整合后才输出到浏览器)。这个listener我已经加上去了的。能够正确的获取每个请求的模板。
0
闲大赋
闲大赋
我看sitemesh都有freemaker,velocity的集成,以freemarker为例,需要设置body,title等数据( https://github.com/sitemesh/sitemesh2/blob/master/src/java/com/opensymphony/module/sitemesh/freemarker/FreemarkerDecoratorServlet.java

所以我认为可能没有那么简单,还需要根据sitemesh来做一些进一步集成以适应sitemesh。

另外,beetl本生就具有较强的布局功能,通过layout和include标签完成简单布局,以及模板变量完成复杂布局。也许你用不着sitemesh。beetl的一位使用者就是用beetl布局代替了sitemesh布局

0
linapex
linapex
@ rubysoft @ 闲.大赋  sitemeth 与 beetl 结合的问题解决了吗?
0
linapex
linapex

引用来自“linapex”的评论

@ rubysoft @ 闲.大赋  sitemeth 与 beetl 结合的问题解决了吗?
看完 sitemesh 源码了,完全可以结合
0
linapex
linapex
已经完美实现
闲大赋
闲大赋
有文档嘛?我看其他人也有需求
0
dimdim
dimdim

求教,sitemesh不装饰的问题。

session失效之后,shiro验证重新登录,返回之前访问的页面,就不装饰了。

返回顶部
顶部