📄 ch05s03.html
字号:
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>3. Counter之Rife版</title><link rel="stylesheet" href="html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.69.1"><link rel="start" href="index.html" title="Java网络程序员看Continuation"><link rel="up" href="ch05.html" title="Chapter 5. RIFE简介"><link rel="prev" href="ch05s02.html" title="2. 安装和启动Rife"><link rel="next" href="ch05s04.html" title="4. 猜数字之Rife版"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3. Counter之Rife版</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch05s02.html">Prev</a> </td><th width="60%" align="center">Chapter 5. RIFE简介</th><td width="20%" align="right"> <a accesskey="n" href="ch05s04.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e675"></a>3. Counter之Rife版</h2></div></div></div><p>在做任何开发之前,我们首先介绍几个Rife里的重要目录和文件:</p><div class="itemizedlist"><ul type="disc"><li><p>src/sites/pub.xml:Rife中每个项目是由若干个site组成的,rife-jumpstart中定义了一个pub site,和一个用来做admin管理的admin site。我们的例子中只会用到pub.xml。这两个site都是在src/sites/main.xml中定义的。</p></li><li><p>如果我们打开pub.xml,我们就会看到其中element的定义。Rife中要用到的每一个部件(Element类)都要有一个element定义。我们可以给该element一个URL,否则用户不能直接访问该element。大部分element都会由一个类支持,这些类通常放在src/implementation文件夹中。</p></li><li><p>src/java:商业数据及逻辑类在这个目录下。</p></li><li><p>src/template:template目录。我们在程序中调用“counter.display”时,Rife会将之翻译为src/template/counter/display.html。</p></li></ul></div><p>既然我们是要写counter,那么我们先写一个counter的element定义。修改src/sites/pub.xml如下:</p><pre class="programlisting"><?xml version="1.0" encoding="ISO-8859-1"?><site> <arrival destid="Counter"/> <element id="Counter" implementation="counter.CounterController" url="/counter"/></site></pre><p>这里我们用arrival声明了Counter element为首页。接下来我们在src/implementation中创建counter.CounterController类以实现该element。</p><pre class="programlisting">package counter;import com.uwyn.rife.engine.Element;import com.uwyn.rife.template.Template;public class CounterController extends Element { private int count = 0; private Template template; public void initialize() { template = getHtmlTemplate("counter.display"); } public void processElement() { print(template); }}</pre><p>上面介绍过,count.display被翻译成src/template/counter/display.html,所以我们在src/template下创建一个新的文件夹counter,并在里面新建一个文件display.html。其内容如下:</p><pre class="programlisting"><!--I 'common.blueprint'/--><!--I 'common.error_messages'/--><!--BV 'window_title'-->Counter Application<!--/BV--><!--BV 'content'--> <div class="content"> <h1>Counter Application</h1> <p>Welcome to the counter application!</p> </div><!--/BV--></pre><p>Rife模版的语法看起来相当奇怪,这里我稍微解释一下。Rife模版里有两个很重要的tag,一个是Block,用B表示;另一个是Value,用V表示。简单的说,实现element的类可以读模版里的Block值,可以写模版里的Value值。B和V的起始中止与XML类似,不过它改用所在文件格式的注释格式取代“<”和“>”。所以在HTML模版文件中,<!--B ‘content’-->something here<!--/B-->定义了一个Block,<!--B ‘content’/-->则定义了一个空的Block。</p><p>上面模版中的content值将会出现在HTML BODY中,window_title值则将成为HTML标题。之所以会使用这两个值的具体定义在src/template/common/blueprint模版中,该模版也包含了标准的HTML开头和结尾,有兴趣的读者可以看看其内容。</p><p>好了,保存所有文件,不需要重新启动服务器,刷新浏览器我们就能看见模版正确显示:</p><div class="screenshot"><div class="mediaobject"><img src="resources/rife-counter-welcome.png"></div></div><p>确认了这些步骤都做对了之后,我们就可以来实现counter程序了。首先我们要给pub.xml里的counter element增加两个submission,这时在Rife中定义element“入口”的办法。修改后的pub.xml如下所示:</p><pre class="programlisting"><?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE site SYSTEM "/dtd/site.dtd"><site> <arrival destid="Counter"/> <element id="Counter" implementation="counter.CounterController" url="/counter">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -