探讨问题的同胞们可以加QQ群的标签吗?
优采云 发布时间: 2021-04-05 02:04探讨问题的同胞们可以加QQ群的标签吗?
正在讨论问题的同胞可以加入QQ组:315309006
许多人对cms感到非常好奇,并思考为什么cms系统中存在各种标签,例如#if,其中有些是#getArticle(“ page = 5”),甚至更棒还有“ [loop] [输出标题/] [/ loop]”,这些是如何具体实现的?本文通过示例分析cms的核心内容,以帮助开发人员了解cms的工作原理以及如何设计自己的特殊标签。
通常来说,一个完整的cms系统至少应收录以下四个部分:文档组织,文档发布,模板和自定义脚本以及文件管理。模板和自定义脚本是核心部分,因为它必须面对cms的用户-这对于进行二次开发并以网站为生的用户来说非常重要。
代码示例:(通过JAVA速度实现)[或使用freemark]
// 自定义声明
@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.FIELD })
public @interface InjectProperty {
public String name();
public String defaultValue() default "";
}
}
// 描述注入对象
public class InjectBean {
@InjectProperty(name = "articleId")
private String articleId = "";
@InjectProperty(name = "title")
private String title = "";
@InjectProperty(name = "author")
private String author = "";
……
@InjectProperty(name = "cms")
private final INCCMSArticle cms = new INCCMSArticle(this);
……
}
// 将模板发布过程抽象成接口
public interface TemplateEngine {
// 发布模板
public String run(File f) throws Exception;
// 注入页面属性
public void injectProperty(Object param) ;
}
// 定义抽象类 处理执行过程
public abstract class AbstractTemplateEngine implements TemplateEngine {
private final Map properties = new HashMap();
public String run(File f) throws Exception {
return new VelocityTemplate().publishVelocityContent(properties, f);
}
public void injectProperty(Object param) throws InjectException {
for (Field f : param.getClass().getDeclaredFields()) {
InjectProperty injectProperty = f.getAnnotation(InjectProperty.class);
if (injectProperty == null) continue;
name = f.getName();
try {
Object value = PropertyUtils.getProperty(param, name);
properties.put(injectProperty.name(), value);
} catch (Exception e) {
throw new InjectException("加载[" + name + "]错误:" + e);
}
}
}
}
// 定义Velocity发布机
public class VelocityTemplate {
public String publishVelocityContent(Map properties, String forder,
String fileName) throws ParseErrorException,
MethodInvocationException, ResourceNotFoundException, IOException {
// filepath是模板位置
InputStream in = new FileInputStream(new File(#fiepath#));
Reader read = new InputStreamReader(in, "utf8");
VelocityEngine velocity = new VelocityEngine();
VelocityContext context = new VelocityContext(properties);
StringWriter sw = new StringWriter();
// 调用Velocity接口方法
velocity.evaluate(context, sw, null, in);
return sw.toString();
}
}
(上面的图片是我实现的标签)
正在讨论问题的同胞可以加入QQ组:315309006