要想动态切换模板,首先把default文件夹复制一份,改成新的名称放到同目录下,然后修改你的新模板,无论样式和结构都可以修改,但是文件名称和里面的变量不要动,然后就是修改你的各个模板的header.htm文件。在里面加上切换按钮,每个按钮赋javascript的事件,代码如下:
cookieString="templateName=mosou;path=/";
document.cookie=cookieString;
document.location.reload();
从上面代码可以看出是把模板名称保存在cookie里,templateName为模板名称,每个模板的header.htm的切换按钮都要加上这个javascript代码。接下来就是修改两个java文件,首先修改net.jforum.ControllerUtils文件,把prepareTemplateContext方法里的context.put("templateName", SystemGlobals.getValue(ConfigKeys.TEMPLATE_DIR));这段代码换为下面的代码:
Cookie cookie=this.getCookieTemplate("templateName");
if(cookie != null ){
System.out.println(cookie.getValue());
context.put("templateName", cookie.getValue());
}else{
context.put("templateName", SystemGlobals.getValue(ConfigKeys.TEMPLATE_DIR));
}
最后再修改net.jforum.net.jforum这个文件,把process这个方法里的try块换成下面的代码:
Cookie cookie=this.getCookie("templateName");
String dir="";
if(cookie != null ){
dir=cookie.getValue();
}else{
dir=SystemGlobals.getValue(ConfigKeys.TEMPLATE_DIR);
}
return JForumExecutionContext.templateConfig().getTemplate(
new StringBuffer(dir).
append('/').append(this.templateName).toString());
再在这个类里加入以下方法:
public static Cookie getCookie(String name)
{
Cookie[] cookies = JForumExecutionContext.getRequest().getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
Cookie c = cookies[i];
if (c.getName().equals(name)) {
return c;
}
}
}
return null;
}
到此你的jforum论坛就可以手动更换模板和风格了。