程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java列舉的應用示例

java列舉的應用示例

編輯:關於JAVA

java列舉的應用示例。本站提示廣大學習愛好者:(java列舉的應用示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java列舉的應用示例正文


1、web.py簡介
web.py是一款輕量級的Python web開辟框架,簡略、高效、進修本錢低,特殊合適作為python web開辟的入門框架。官方站點:http://webpy.org/

2、web.py裝置
1、下載:http://webpy.org/static/web.py-0.33.tar.gz
2、解壓並進入web.py-0.33目次,裝置:python setup.py install

3、創立簡略單純博客
1、目次解釋:主目次blog/,模板目次blog/templates
2、在數據庫“test”中創立表“entries”

CREATE TABLE entries ( 
  id INT AUTO_INCREMENT, 
  title TEXT, 
  content TEXT, 
  posted_on DATETIME, 
  primary key (id) 
); 

3、在主目次創立blog.py,blog/blog.py

#載入框架
import web
#載入數據庫操作model(稍後創立)
import model
#URL映照
urls = (
    '/', 'Index',
    '/view/(/d+)', 'View',
    '/new', 'New',
    '/delete/(/d+)', 'Delete',
    '/edit/(/d+)', 'Edit',
    '/login', 'Login',
    '/logout', 'Logout',
    )
app = web.application(urls, globals())
#模板公共變量
t_globals = {
  'datestr': web.datestr,
  'cookie': web.cookies,
}
#指定模板目次,並設定公共模板
render = web.template.render('templates', base='base', globals=t_globals)
#創立登錄表單
login = web.form.Form(
           web.form.Textbox('username'),
           web.form.Password('password'),
           web.form.Button('login')
           )
#首頁類
class Index:
  def GET(self):
    login_form = login()
    posts = model.get_posts()
    return render.index(posts, login_form)
  def POST(self):
    login_form = login()
    if login_form.validates():
      if login_form.d.username == 'admin' /
        and login_form.d.password == 'admin':
        web.setcookie('username', login_form.d.username)
    raise web.seeother('/')
#檢查文章類
class View:
  def GET(self, id):
    post = model.get_post(int(id))
    return render.view(post)
#新建文章類
class New:
  form = web.form.Form(
             web.form.Textbox('title',
             web.form.notnull,
             size=30,
             description='Post title: '),
             web.form.Textarea('content',
             web.form.notnull,
             rows=30,
             cols=80,
             description='Post content: '),
             web.form.Button('Post entry'),
             )
  def GET(self):
    form = self.form()
    return render.new(form)
  def POST(self):
    form = self.form()
    if not form.validates():
      return render.new(form)
    model.new_post(form.d.title, form.d.content)
    raise web.seeother('/')
#刪除文章類
class Delete:
  def POST(self, id):
    model.del_post(int(id))
    raise web.seeother('/')
#編纂文章類
class Edit:
  def GET(self, id):
    post = model.get_post(int(id))
    form = New.form()
    form.fill(post)
    return render.edit(post, form)
  def POST(self, id):
    form = New.form()
    post = model.get_post(int(id))
    if not form.validates():
      return render.edit(post, form)
    model.update_post(int(id), form.d.title, form.d.content)
    raise web.seeother('/')
#加入登錄
class Logout:
  def GET(self):
    web.setcookie('username', '', expires=-1)
    raise web.seeother('/')
#界說404毛病顯示內容
def notfound():
  return web.notfound("Sorry, the page you were looking for was not found.")
  
app.notfound = notfound
#運轉
if __name__ == '__main__':
  app.run()

4、在主目次創立model.py,blog/model.py

import web
import datetime
#數據庫銜接
db = web.database(dbn = '<a href="http://lib.csdn.net/base/14" class='replace_word' title="MySQL常識庫" target='_blank' style='color:#df3434; font-weight:bold;'>MySQL</a>', db = 'test', user = 'root', pw = '123456')
#獲得一切文章
def get_posts():
  return db.select('entries', order = 'id DESC')
  
#獲得文章內容
def get_post(id):
  try:
    return db.select('entries', where = 'id=$id', vars = locals())[0]
  except IndexError:
    return None
#新建文章
def new_post(title, text):
  db.insert('entries',
    title = title,
    content = text,
    posted_on = datetime.datetime.utcnow())
#刪除文章
def del_post(id):
  db.delete('entries', where = 'id = $id', vars = locals())
  
#修正文章
def update_post(id, title, text):
  db.update('entries',
    where = 'id = $id',
    vars = locals(),
    title = title,
    content = text)

5、在模板目次順次創立:base.html、edit.html、index.html、new.html、view.html

<!-- base.html -->
$def with (page)
<html>
  <head>
    <title>My Blog</title>
    <mce:style><!--
      #menu {
        width: 200px;
        float: right;
      }
    
--></mce:style><style mce_bogus="1">      #menu {
        width: 200px;
        float: right;
      }
    </style>
  </head>
  
  <body>
    <ul id="menu">
      <li><a href="/" mce_href="">Home</a></li>
      $if cookie().get('username'):
        <li><a href="/new" mce_href="new">New Post</a></li>
    </ul>
    
    $:page
  </body>
</html>

<!-- edit.html -->
$def with (post, form)
<h1>Edit $form.d.title</h1>
<form action="" method="post">
  $:form.render()
</form>
<h2>Delete post</h2>
<form action="/delete/$post.id" method="post">
  <input type="submit" value="Delete post" />
</form>

<!-- index.html -->
$def with (posts, login_form)
<h1>Blog posts</h1>
$if not cookie().get('username'):
  <form action="" method="post">
  $:login_form.render()
  </form>
$else:
  Welcome $cookie().get('username')!<a href="/logout" mce_href="logout">Logout</a>
<ul>
  $for post in posts:
    <li>
      <a href="/view/$post.id" mce_href="view/$post.id">$post.title</a>
      on $post.posted_on
      $if cookie().get('username'):
        <a href="/edit/$post.id" mce_href="edit/$post.id">Edit</a>
        <a href="/delete/$post.id" mce_href="delete/$post.id">Del</a>
    </li>
</ul>

<!-- new.html -->
$def with (form)
<h1>New Blog Post</h1>
<form action="" method="post">
$:form.render()
</form>

<!-- view.html -->
$def with (post)
<h1>$post.title</h1>
$post.posted_on<br />
$post.content

6、進入主目次在敕令行下運轉:python blog.py,將啟動web辦事,在閱讀器輸出:http://localhost:8080/,簡略單純博客即已完成。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved