星期一 2007年09月03日
ajax上传文件
使用ajax上传文件,最好是有一个进度条显示,这样就有了更好的用户体验。
原理:
1.监视上传文件流,并且不断将信息暂存。
2.定时向服务器请求上传信息。还可以考虑用comet来代替。
3.显示在进度条上。
要监视上传文件流则需要设一个listener到Stream去,不断地将信息放到公共暂存区里。
定时在网页发起请求,将公共暂存区里的信息读出来,唯一标志就使用session id,也可以考虑根据页面访问生成guid。
示例代码:
基于buffalo, struts
http://www.gaimcn.com/bbs/attachment.php?aid=19
Posted at 10:08下午 九月 03, 2007 by magicgod in 原创 |
星期五 2007年08月31日
rjs的使用
参见:ajax on rails第5章。下载:http://www.gaimcn.com/bbs/attachment.php?aid=20
事先创建好view, controller
1.在view里创建一个远程链接,link_to_remote "go", :url=>{:action=>"go_with_rjs"} , 如果是form_remote_tag也可以。
2.在controller里创建一个action
def go_with_rjs
@msg="let's go"
end
3.在目录 app/views/<controller名字> 下建一个 go_with_rjs.rjs ,名字与action相同。
内容:
page.alert("#{msg}")
这样点击那个链接就出一个alert信息。
具体page的用法参见Module
ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods
也可以看ajax on rails,里面更详细,另外有多种效果可以使用。
rjs给了一种选择,让你在ajax的时代也可以重回ruby,如果你不喜欢js的话。
Posted at 08:48下午 八月 31, 2007 by magicgod in 原创 |
rails的缺省三层测试
rails缺省的测试建议就是三层:
1. Unit Test 单元测试,用于测试各种域对象,也可以测试一些工具类。
2. Functional Test 功能测试,其实是对于controller测试,有点象web层面的单元测试。
3. Integration Test 集成测试,这个最贴近业务需求,往往是将几个页面串起来,以一个业务动作为基本单位。
这些测试都是写代码来测试,似乎并没有录制工具来生成,不过因为ruby的代码实在是非常简洁,写起来应该不算费力的。最难的web测试已经分解为一些预置的mock类,很容易写代码来测试。
测试是需要环境支持的,rails 缺省使用Test数据库环境,大多数应用所指的环境就是数据库环境,如果有特殊的环境要求还可以在setup里配置。
测试数据是放在 Test Fixture里的,都是一些简单的yaml文件,由于支持erb,使得测试数据可以成为动态数据,比如当前时间的前一个小时、sha1加密的密码之类。这些动态数据的灵活性要超过SQL和dump文件。
当你开始测试应用的时候,需要的是:
1. 准备好Test数据库环境(基本上只要建一个库,另外用rake从开发数据库来生成一个schema.rb)。
2. 准备好测试数据 Test Fixture,填好内容,并且理顺他们之间的关系。
3. 写好三层测试代码。
最后,开始 rake test,一切就开始了。虽然没有红绿条,但是感觉比JUnit要踏实。
Posted at 08:47下午 八月 31, 2007 by magicgod in 原创 |
星期五 2007年08月10日
那本关于gaim的书可以更加精简一点
前100页还没讲到hello world,当我绝望并欣喜的时候,在 113页找到了。
普及了大量cvs, make, gtk+, OO C的概念和实用知识后,让我对默默无闻的linux编程世界肃然起劲。
从200页开始讲了一个插件的例子。马上又变成了高级GTK+。
实际上这本书换个名字叫GTK从入门到精通也算是可以的。
Posted at 12:14上午 八月 10, 2007 by magicgod in 原创 |
星期一 2007年07月09日
jboss 配置基于容器的安全管理
1. web.xml
登录配置,基于form:
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/loginError.jsp</form-error-page>
</form-login-config>
</login-config>
<security-role>
<description>A
client</description>
<role-name>client</role-name>
</security-role>
<security-role>
<description>Sales rep role
</description>
<role-name>salesrep</role-name>
</security-role>
<security-constraint>
<web-resource-collection>
<web-resource-name>TMTix</web-resource-name>
<description>Require users to
authenticate</description>
<url-pattern>*.pdf</url-pattern>
<http-method>POST</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<web-resource-collection>
<web-resource-name>TMTix</web-resource-name>
<description>Require users to
authenticate</description>
<url-pattern>*.html</url-pattern>
<http-method>POST</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<description>Define who can access this url-pattern</description>
<role-name>admin</role-name>
<role-name>client</role-name>
<role-name>salesrep</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<resource-ref>2.jboss-web.xml :
<description>DB
Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
数据库连接池引用,必须在这里有定义,否则无法使用,jndi名称定义在mysql-ds.xml:
<security-domain>java:/jaas/webSecurity</security-domain>
<resource-ref>3. mysql-ds.xml
<description>DB Connection</description>
<!-- jdbc/trs defined in mysql-ds.xml -->
<jndi-name>jdbc/mysql</jndi-name>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<local-tx-datasource>4.login-config.xml
<jndi-name>jdbc/mysql</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:mysql://localhost/database?autoReconnect=true&useUnicode=true&characterEncoding=utf-8
</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>test</user-name>
<password>test</password>
<min-pool-size>2</min-pool-size>
<max-pool-size>20</max-pool-size>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
<application-policy name = "webSecurity">这样的配置就是访问*.html的时候,自动使用login.jsp来进行登录用户。
<authentication>
<login-module code =
"org.jboss.security.auth.spi.DatabaseServerLoginModule"
flag =
"required">
<module-option name = "dsJndiName">jdbc/mysql</module-option>
<module-option name = "principalsQuery">
select password from user where
email=?
</module-option>
<module-option name = "rolesQuery">
select role_name, 'Roles' from
user_role where email=?
</module-option>
</login-module>
</authentication>
</application-policy>
Posted at 07:45下午 七月 09, 2007 by magicgod in 原创 |
星期六 2007年05月26日
rails.vim命令记
:Rake 这个好记
:Redit 任意文件,相对路径
:Rlog 日志文件
:Rpreview 打开浏览器,http://localhost:3000
:Rtags 生成一个tag,要求ctags
:Rrefresh 刷新
:Rfind 查文件,可以不写.rb后缀
gf 这个命令超强,根据当前光标处内容跳转到文件,以下是详细例子(*代表光标):
Pos*t.find(:first)
=> app/models/post.rb
has_many :c*omments
=> app/models/comment.rb
link_to "Home", :controller => :bl*og
=> app/controllers/blog_controller.rb ~
<%= render :partial => 'sh*ared/sidebar' %>
=> app/views/shared/_sidebar.rhtml
<%= stylesheet_link_tag :scaf*fold %>
=> public/stylesheets/scaffold.css
class BlogController < Applica*tionController
=> app/controllers/application.rb
class ApplicationController < ActionCont*roller::Base
=> .../action_controller/base.rb
fixtures :pos*ts
=> test/fixtures/posts.yml
layout :pri*nt
=> app/views/layouts/print.rhtml
(In the Blog controller)
def li*st
=> app/views/blog/list.rhtml
:A :R 也是跳文件的
Current file Alternate file Related file ~
model unit test related migration
controller (in method) functional test template (view)
template (view) helper controller (jump to method)
migration previous migration next migration
config/routes.rb config/database.yml config/environment.rb
直接打开
:Rmodel
:Rmigration
:Robserver
:Rfixtures
:Runittest
:Rcontroller
:Rhelper
:Rview
:Rlayout
:Rapi
:Rfunctionaltest
:Rstylesheet
:Rjavascript
:Rplugin
:Rlib
:Rtask
:Rintegrationtest
一个超强的:Rcommand,不介绍了,看help
:Rscript
:Rconsole
:Rgenerate
:Rserver
:Rserver! 强制运行,杀掉前面那个,相当于重启
重构:
:Rextract 简单说就是把rhtml中的一段提出来,变成子页面
项目结构
:Rproject 看上去有个左边项目结构树
surroud: 这个不是命令,而是一个插件,装好后就可以在rhtml中自动关闭<%%>
Posted at 11:34下午 五月 26, 2007 by magicgod in 原创 |
星期日 2007年05月20日
rails 中连接数据库使用utf-8连接
mysql实际上可以分级控制编码:
client, server, connection, database, table
在rails里要配置的是:
1.文件格式都是utf-8。
2.连接配置成为utf-8。
编辑database.yml,加入一个配置:
encoding: utf8
Posted at 01:53下午 五月 20, 2007 by magicgod in 原创 |
星期六 2007年05月19日
正在找一个轻量级的html编辑器
没想到VIM这么好用,连代码补全都有,虽然不是可视的,但是因为轻量级嘛,用起来特别顺手。
速度快、纯文本、支持多编码、代码补全、高亮识别、无数快捷键......
正在寻找支持java的插件,最好带ant和project功能的...
Posted at 08:48下午 五月 19, 2007 by magicgod in 原创 |
最近没写博客,实在是奇怪
主要原因竟然是想写的时候总是发不了,奇怪的很,提交没反应,之后整个博客就看不了,是不是人品问题。
还是因为我总是在后半夜发啊。
Posted at 08:09下午 五月 19, 2007 by magicgod in 原创 |
vim中的tab设定
如果要纯用tab,并且设为4格宽度,就要进行以下设置
:set ts=4
:set noet
:set softtabstop=0
这样输入一个tab只占4格宽度,也不会变成空格。
不要使用tab混合空格的模式,这样最容易导致在其他编辑器里混乱的情况。
vim还有一个evim的已配置简易环境,非常好用,类似windows的操作风格。
Posted at 08:08下午 五月 19, 2007 by magicgod in 原创 |
星期六 2007年05月05日
把gaim 2.0 beta6 升级到了pidgin 2.0
gaim改了名字,叫一只小鸟了pidgin 。
版本也升到了 2.0正式版,只是图标确实有点难看,没办法,凑和看吧。
qq跟以前一样,一个能上,一个不能上。
也把服务器上更新了一下。
Posted at 08:24下午 五月 05, 2007 by magicgod in 原创 |
星期二 2007年04月24日
vim命令仅记
ctrl+v:可视状态,可以定块,也可以直接用鼠标
"+y:copy 块内容到剪贴板,在定块后,这个剪贴板是通用于其他程序。
y: 类似上,但是仅用于vim
"+p: paste内容来自剪贴板。
p: 类似上,但是仅限于vim的y拷出来的内容
i: 插入
a:添加在后面
dd: 删除当前行
q: 退出。
qa: 全退出。
q!: 强制退出,!基本上都代表强制
w: 写入,可以与很多命令一起用,包括q
/<字符串> :搜索
n: 搜索下一个匹配的地方
G: 到文件最后面
s/<匹配>/<替换>/ : 替换字符串,支持正则表达式,最后加一个g就是全换。加一个c就是确认。
ctrl+x: 开始自动完成功能,可以进行全方面的匹配,但是界面不好。要配合下一个命令,比如ctrl+o, ctrl+n 等。
tabnew 打开一个tab,空内容。
tabclose 关闭当前tab。
tabnext 下一个tab。
tabp 上一个tab。
rails.vim:
这个插件有很多有用的命令
Rproject :变成project状态,要求project.vim插件
Rmodel <类名>: 直接跳到某个model类
Rcontroller <类名>:跳到一个controller类
Rview <名称>:打开一个view 文件
Rlayout <名称>: 打开一个layout文件
Posted at 03:33下午 四月 24, 2007 by magicgod in 原创 |
星期五 2007年04月13日
rails里的中文处理
实际上没什么很悬的,记住一点,处处都用utf8就可以了。
文件要用utf8,mysql要用utf8(包括连接,建库表),系统环境最好是设为utf8(LANG变量,这点ubuntu挺好的),html要设为utf8,header也要设utf8,浏览器要缺省utf8。
这样utf8什么编码都可以处理,如果有遗留系统也记得要先转utf8再处理。跟外部接口一律用utf8。
utf8的世界清静了....
Posted at 12:15下午 四月 13, 2007 by magicgod in 原创 |
星期一 2007年04月09日
如何在eclipse下调试grails
安装 Grails Eclipse,去grails网站下载。
先将生成后的grails application导入eclipse。
创建一个Run, Java Application, 运行类grails.util.GrailsMain,
参数是-Dbase.dir=${project_loc} -Dserver.port=8080 -Dgrails.env=development
Environment下创建GRAILS_HOME=安装目录
就可以开始运行了,如果要单步调试就用Debug来运行,将断点设在groovy文件里,运行的时候就会停下来,可以单步执行,其他操作与普通的java调试一样。
另外说一下如何在eclipse里执行grails自动生成脚本。就是创建Extrnal Tools,运行命令就是grails或grails.bat,设置变量 GRAILS_HOME,工作目录是${project_loc},参数为${string_prompt},这样在运行时会提示输入命令。
如果输入create-domain-class,就是创建域对象,会在console窗口里提示输入域对象名称。
Posted at 09:24下午 四月 09, 2007 by magicgod in 原创 |
grails 的三种环境配置
Test, Development, Production.这是三种数据库环境
在生成的代码中,grails-app/conf目录下有三个数据源的配置,其实是三个类。
TestDataSource.groovy
DevelopmentDataSource.groovy
ProductionDataSource.groovy
打开看一下,其实非常简单,有几个属性:
boolean pooling = true
String dbCreate = "update" // one of 'create', 'create-drop','update'
String url = "jdbc:mysql://localhost/anyblog?autoReconnect=true&useUnicode=true&characterEncoding=utf-8"
String driverClassName = "com.mysql.jdbc.Driver"
String username = "blog"
String password = "123456"
pooling就是缓冲池的设置,一般为true
dbCreate是创建数据库的方式,create-drop是完全重建,根据域对象。create就是创建,似乎跟create-drop一样。update则是更新库结构的方式。
显示sql在日志里:
def logSql = true使用自己的数据库方言:
def dialect = MySQLDialect.class
Posted at 09:11下午 四月 09, 2007 by magicgod in 原创 |