RRCatalog 示例应用程序将演示如何使用 JRuby 和 Rails 对存储目录中数据集进行分页。
下载 RRCatalog
示例源代码
示例应用程序中的技术和框架概述
Rails 是一种基于模式-视图-控制器的框架,用于在 Ruby 中开发数据库支持的 web 应用程序。
JRuby 是 Ruby 编程语言的一种 100% 纯 Java 实现。使用 JRuby 和 Rails,m我们能够在 Classfish 或 Tomcat
之类的 servlet 容器中运行 web 应用程序。
示例应用程序
此示例应用程序展示了某宠物商店所销售宠物的在线目录。下图为 Catalog Listing 页面,用户能够使用此页面翻阅商店的物品清单。
模型 此模型为应用程序的持久业务域对象。对于此模型,Rails 实现了
活动记录设计模式。<
/a>一个活动记录对象代表一个数据库表中的一行。下图所示的 item.rb 和 address.rb 类是 Rails 为 item 和 addresses 表生成的。要学习使用 Netbeans
6 生成 Rails 代码,请阅读
10 分钟内创建一个 Ruby Weblog。生
成模型代码后需要添加关系。
Item
类与
Address
及 Contactinfo 类是一对多的关系。在 Rails中,
belongs_to
是一个多对一关系的多端,而
has_many
是一端。Rails 约定具有外键的对象属于其他对象。
示例代码位于:app/models/
item.rb
|
class Item < ActiveRecord::Base
belongs_to :address
belongs_to :contactinfo
end
|
|
|
示例代码位于: app/models/
address.rb
|
class Address < ActiveRecord::Base
has_many :item
end
|
|
Item 类是 Rails
ActiveRecord Base 类的子类。在运行时,c
Rails 框架动态地将列名和属性(有设置器和取得器)添加到相应项目表各列的 Item 类中。Rails 使用默认映射规则:item 类映射到 item 表,address 类默映射到
address 表,主键默认映射到 id,外键默认映射到 talename_id...
item 表的 SQL 示例
|
CREATE TABLE items ( id VARCHAR(10) NOT NULL,
name VARCHAR(30) NOT NULL,
description VARCHAR(500) NOT NULL,
imageurl VARCHAR(55),
imagethumburl VARCHAR(55),
price DECIMAL(14,2) NOT NULL,
address_id VARCHAR(10) NOT NULL,
contactinfo_id VARCHAR(10) NOT NULL,
primary key (id),
foreign key (address_id) references addresses(id),
foreign key (contactinfo_id) references contactinfos(id)
);
|
控制器
控制器将处理传入的 http 请求,与模型交互获取数据并处理请求,调用合适的视图,并将域数据在视图中显示出来。在 Rails 中,ActionController 类处理
http 请求,ActionController 类由一个或多个 action 方法组成,这些 action 方法将根据请求执行,然后呈现模板或重定向到另一个 action。Rails
将请求发送到与此请求映射的 URL 相关的控制器 action。在 Rails 中,从 URL 到 action 的默认映射符合以下约定:http://host/controller/action/id 。例如,URL http://host/item/list 调用以下 item controller 类中的 list action 方法。
注意此代码是用 NetBeans 6 IDE 支持的 Ruby
Rails 平台生成的。
Rails
Scaffolding 提供了一系列标准 action 用于登记,显示,创建,更新以及销毁类的对象。这些标准 action 具有控制器逻辑以及默认视图模板(我修改了视图模板)。ItemController
list
action 提供一个视图,此视图含有一个 item 对象的分页目录。
|
示例代码位于:app/controllers/
item_controller.rb
|
class ItemController <
ApplicationController
def index
list
render :action => 'list'
end
def list
@item_pages, @items = paginate :items, :per_page => 10
end
|
ItemController
是 ApplicationController
的一个子类,而 ApplicationController 是 Rails
ActionController Base class
的一个子类。如果一个 URL 有控制器,但是没有 action(例如 http://host/controller/),Rails 默认将其设置为 index action。在ItemController
代码中,index
action 方法重定向到 list
action。list
action 方法调用 ActionController
paginate
方法,paginate 方法将查询 Item
Active Record 来实现分页。此分页方法创建
@items 实例变量,它是当前页(最多为 10 页)模型对象的规则集合,分
页方法还将创建一个 @item_pages
分页器实例,该类表示 Active Record 集合的分类器。@item_pages
和框架自动将 @items
变量设置为对目录视图可用。
执行这些代码后,action 通常会在与控制器和 action 名字相关的视图目录中提供一个模板,比如说 list action 将提供一个 app/views/item/list.rthml
模板。
视图
视图层将使用控制器提供的域对象数据生成 web 页面。在 Rails 中,将使用
RHTML 、RXML 或 RJS 呈现视图。RHTML
是嵌入了 Ruby 代码的 HTML。
示例代码位于:app/views/item/
list.rhtml
|
<h2>Listing items</h2>
<%= link_to 'Previous page', { :page
=> @item_pages.current.previous }
if
@item_pages.current.previous %>
<%= link_to 'Next page', { :page =>
@item_pages.current.next }
if @item_pages.current.next
%>
<table border="1" cellpadding="4" cellspacing="0">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">photo</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<% for item in @items %>
<tr>
<td>
<%= link_to %Q{#{item.name}}, :action
=> 'show', :id => item %>
</td>
<td>
<%= image_tag item.imagethumburl
%>
</td>
<td align="right">
<%=h item.price %>
</td>
</tr>
<% end %>
</tbody>
</table>
|
视图将使用控制器设置的实例变量访问生成 rhtml 所需的数据。在 list.rhtml 中:
<% for item in @items %>
循环遍历
@items 实例变量中的对象(@items 实例变量是
Item
模型对象的一个规则集合),并将各个
Item
模型对象指派给
item
变量。
<%= link_to %Q{#{item.name}}, :action
=> 'show', :id => item %>
调用 Rails 的助手方法
link_to
,该方法将创建一个到 item/show/id action 的链接,用于显示相应项目的详细信息。Rails 助手方法能够帮助视图模板生成 HTML。举例来说,这
一行将会为一个项目生成如下的 HTML:
<a href="/item/show/1">Friendly Cat</a>
<%= image_tag item.imagethumburl
%>
调用 Rails helper 方法
image_tag
,它将为
item's imagethumburl
属性生成一个 HTML 图形标记。
<%=h item.price %>
显示
item 's price
属性的值。Rails
h
方法创将建转义 HTML 上下文。
<%= link_to 'Previous page',{:page =>
@item_pages.current.previous} if @item_pages.current.previous
%>
使用 @item_pages
分页器实例创建一个到前一页项目的 html 链接(如果有前一页)。
Show Action 方法
在 Rails 中,URL http://host/item/show/1 ( http://host/controller/action/id ) 到
action 方法的映射将发送到 ItemController
中的 show
action 方法,将 数值 1 传递给此方法作为 params
hash 的 id
成员。如下所示为 ItemController
类的 show
action 方法。ItemController
show
action 提供一个视图来显示与此 id 参数相关的 item 对象的详细信息。
|
示例代码位于:app/controllers/
item_controller.rb
|
def show
@item = Item.find(params[:id])
end
|
show
action 方法将调用 Item
ActiveRecord Base 类的 find
方法。find 方法将查询 item 表创建 @item
实例变量,它与 id(主键)等于 id
参数 item 相关。这与下列的 sql 等价:select * from items where id='1'。框架自动将 @item
变量设置为对 Show 视图可用。
Show 视图模板
执行完此 action 中的代码后,show
action 将呈现 app/views/item/show.rthml 模板。以下是 item show 视图的 RHTML:
示例代码位于:app/views/item/
show.rhtml
|
<h2> Detail of item</h2>
<table>
<tbody>
<tr>
<td>Name:</td>
<td> <%=h @item.name%> </td>
</tr>
<tr>
<td>Description:</td>
<td> <%=h @item.description%>
</td>
</tr>
<tr>
<td>Photo:</td>
<td> <%= image_tag @item.imageurl
%> </td>
</tr>
<tr>
<td>Price:</td>
<td> <%=h @item.price%> </td>
</tr>
<tr>
<td>Seller's Location:</td>
<td>
<%=h @item.address.city%> ,
<%=h @item.address.state%>
</td>
</tr>
<tr>
<td>Seller's email:</td>
<td> <%=h @item.contactinfo.email%>
</td>
</tr>
</tbody>
</table>
|
<%=h @item.description%>
在转义 HTML 上下文中显示
item 's
description属性的值。
<%= image_tag @item.imageurl
%>
为
item's imageurl
属性生成一个 HTML 图形标记
<%=h @item.address.city%>
在转义 HTML 文本中显示
item's
address city
属性的值。
下图所示为 url http://host/item/show/id 结果页面,它显示了 item 的详细信息:
Layout 模板使用 Rails 设计模板,我们能够将普通的 html 放到多视图(例如,页头,页脚,侧栏)中。默认情况下,layout 模板位于 views layouts
目录中,其文件名与控制器相关。为了在 Pet Catalog 页面添加标题和 parrot 图像,我将此表放到 app\views\layouts\item.rhtml 模板中:
|
示例代码位于:app/views/layouts/
item.rhtml
|
<table>
<tr>
<td>Pet Catalog</td>
<td><img src="/images/banner_logo.gif"></td>
</tr>
</table>
|
结束语
本文通过示例应用程序演示了如何使用 JRuby 和 Rails 实现翻页功能:使用 Item Controller action 方法检索Item Model 对象中,并使用 Item rhtml View 模板呈现该对象的内容。
运行示例应用程序:
设置
-
下载 并安装 NetBeans 6.0 Beta
1。请下载完整版本,其中包括 Java IDE、Ruby 和 GlassFish。
- 配置 JRuby 使用 Derby 数据库 (默认情况下,Ruby 使用 MySQL。但是我个人比较喜欢使用
Derby。如果您更喜欢 MySQL,则可以忽略这一步。修改 RRCatalog\config\database.yml 文件并将下表添加到您的 MySQL 数据库中)。
- 打开 Tools Options 对话框,查找 JRuby 解析器的所在目录。
- 将以下的 jar 包拷贝到您的 JRuby lib 目录中: derbyclient.jar (Tools > Java DB Database > Settings 可以找到
derbyclient.jar 的所在目录)
打开并运行示例代码:
- 下载
示例代码
并解压其内容。新解压目录应为
<sample_install_dir>/RRCatalog,其中
<sample_install_dir> 是解压示例包的目录。例如,如果在 Windows 系统中将内容解压到
C:\,新创建的目录就应该在
C:\RRCatalog。
- 启动 NetBeans IDE,单击 File 菜单中的 Open Project 并选择刚才解压的
RRCatalog 目录。
- 按照以下方法启动 Java DB 数据库:
- 在 Tools 菜单中选择 Java DB Database。
- 选择 Start Java DB Server。
- 按照以下方法将一个连接添加到 Java DB 数据库:
- 在左侧选择 Services Tab。
- 选择 Databases,右击鼠标并选择 New Connection。
- 在 New DB Connection 窗口中:
选择 Java DB (Network) 作为名称
输入:jdbc:derby://localhost:1527/pet-catalog 作为 URL
用户名输入:app,密码输入:app
- 按照以下方法在 pet-catalog 数据库中创建表:
- 在 Databases 下,选择我们在刚才创建的连接关系 pet-catalog。右击鼠标并选择 Connect。
- 输入用户名 app 和密码 app。
- 鼠标右击 pet-catalog 并选择 Excecute Command。
- 从
<sample_install_dir>/RRCatalog/catalog.sql 文件拷贝粘贴所有的 sql 文本到 SQL 命令窗口。
- 在窗口顶端单击 Run SQL 图标。这样将创建此应用程序所需的所有表和数据。
- 按照以下方法运行项目:
- 鼠标右击项目窗口的
RRCatalog 节点。
- 选择 Run。在 WEBrick 服务器中运行这个应用程序。
运行项目时,浏览器会显示示例应用程序的 List Items 页面(在 http://localhost:3000/)
在 Glassfish 中运行示例代码:
- 使用
<sample_install_dir>/RRCatalog/RRCatalog.war 中的 WAR 文件或者创建一个 WAR 文件:
- 在 NetBeans IDE 中,单击鼠标右键并选择 project,选择
Run Rake Target,war,standalone,create
- 将 WAR 文件拷贝到 (
RRCatalog.war) Glassfish 的安装目录 "
domains/domain/autodeploy"。
- 在浏览器中输入 URL http://localhost:8080/RRCatalog/,浏览器中会显示示例应用程序的 List Items 页面。
参考资料
|