- 论坛徽章:
- 0
|
本帖最后由 aef25u 于 2011-03-28 15:43 编辑
因最近有人反映代码有问题,检查后发现是由于Dancer版本更新引起的:
-------------------------------------------
Dancer最新版本-1.3011请下载此附件(2011-03-28更新):
MyDancer.rar
(42.7 KB, 下载次数: 350)
-------------------------------------------
这是本人用perl的Dancer+DBI+SQLite+TT实现的网站源码
此附件为旧版Dancer:
dancer.rar
(35.99 KB, 下载次数: 232)
。
运行环境:Windows XP、strawberry perl、IE
实现了:
1、Template::Toolkit、SQLite、Authen::Simple: BI、Dancer::Session::Storable在dancer框架中的简单运用。
2、javascript+css实现二级导航栏
3、网站Perl代码着色显示
4、post方法的注册表单、登陆表单和留言本。
内附(位于dancer目录下):
start.bat 方便本机运行WEB服务器 (然后在IE中输入http://localhost:3000/)
SQLite.pl 生成test.db数据库
1、dancer.pl- #!/usr/bin/perl
- use DBI;
- use Dancer;
- use MyDancer;
- use Dancer::Plugin::Database;
- my @pages = qw(TtExample SqlExample registerExample loginExample GuesBookExample configExample layoutTT);
- #与use MyDancer等同;
- #get '/', sub {
- # layout 'main.tt';
- # template 'home';
- #};
- get '/:page' => sub {
- layout 'main.tt';
- my ($page) = params->{page};
- pass and return false unless grep /$page/, @pages;
- template $page;
- };
- get '/tt/scalar', sub {
- layout 'main.tt';
- template 'scalar' =>
- { content => '欢迎您的到来!希望你也会和我一样喜欢上Perl与Dancer!', };
- };
- get '/tt/table', sub {
- layout 'main.tt';
- my $r = [ { a => 1, b => 2, c => 3, },
- { a => 2, b => 4, c => 6, } ];
- template 'table' => { r => $r, };
- };
- get '/tt/filter', sub {
- layout 'main.tt';
- template 'filter';
- };
- get '/Sql/dbiSqlTest', sub {
- layout 'main.tt';
- my $dbh =
- DBI->connect( "dbi:SQLite:dbname=test.db",,,
- { RaiseError => 1, AutoCommit => 1 } );
- my $sth = $dbh->prepare( 'select * from test where id = 1' );
- $sth->execute;
- my @row_ary = $sth->fetchrow_array;
- template 'dbiSqlTest' => { head => 'use DBI', content => $row_ary[1], };
- };
- get '/Sql/PluginSqlTest', sub {
- layout 'main.tt';
- my $sth = database->prepare( 'select * from test where id = 1' );
- $sth->execute;
- my @row_ary = $sth->fetchrow_array;
- template 'PluginSqlTest',
- {
- head => 'use Dancer::Plugin::Database',
- content => $row_ary[1],
- };
- };
- #表单注册
- get '/book/register' => sub {
- layout 'login_main';
- template
- 'register' => { path => 'GuestBook' },
- };
- post '/book/register' => sub {
- my $sth =
- database->prepare('SELECT * FROM authentication WHERE username=?');
- $sth->execute( params->{username} );
- if ( $sth->fetch ) {
- return "User had been registered, sorry";
- }
- $sth = database->prepare('INSERT INTO authentication VALUES(NULL,?,?,?)');
- $sth->execute(
- params->{username},
- params->{password},
- params->{fmemail},
- );
- session user => params->{username};
- redirect params->{path};
- };
- #GuestBook
- get '/book/GuestBook' => sub {
- layout 'login_main';
- my @myarray;
- my $sth = database->prepare( 'select * from comments order by id asc' );
- $sth->execute;
- while(my @row_ary = $sth->fetchrow_array){
- push @myarray, { user => $row_ary[1], info => $row_ary[2] };
- }
- template 'GuestBook' => {message=>\@myarray};
- };
- post '/book/GuestBook' => sub {
- my $username=session('user');
- if (length params->{fmmsg} > 4096) {
- return "Your comment is too long, sorry";
- }
- my $sth =
- database->prepare('INSERT INTO comments VALUES(NULL,?,?)');
- $sth->execute(
- $username,
- params->{fmmsg},
- );
- session user => $username;
- redirect 'GuestBook';
- };
- #表单登陆
- load_app 'Pamlogin';
- dance;
复制代码 2、Pamlogin.pm- package Pamlogin;
- use Authen::Simple::DBI;
- use Dancer ':syntax';
- # will match '/book/*'
- prefix '/book';
- # will match '/book/login'
- get '/login' => sub {
- layout 'login_main';
- template
- 'login' => { path => 'GuestBook' },
- };
- post '/login' => sub {
- my $dbi = Authen::Simple::DBI->new(
- dsn => 'dbi:SQLite:dbname=test.db',
- statement => 'SELECT password FROM authentication WHERE username = ?'
- );
- if ( $dbi->authenticate( params->{username}, params->{password} ) ) {
- session user => params->{username};
- redirect params->{path};
- }
- else {
- redirect 'login';
- }
- };
- get '/logout' => sub {
- session->destroy;
- redirect 'GuestBook';
- };
- true;
复制代码 |
|