注册账号 登录
小春网 返回首页

崖上のオッサン的个人空间 https://www.incnjp.com/?192660 [收藏] [复制] [分享] [RSS]

日志

【暇つぶし】grovvyでblog作ってみたw

已有 493 次阅读2013-7-14 22:14 |系统分类:闲谈| blog

最近流行のgrovvyでお試しにBlogを作ってみた編~^^var vglnk = {api_url: '//api.viglink.com/api', key: '0dff9ade2d1125af6c910069b6d6e155'};
意外と簡単! groovyってすごいな(ってか基本Javaだしw)

domain~~
Entry.groovy
package jp.co.test
class Entry {
Date dateCreated
String title
String text
byte[] imageData

static hasMany=[comments:Comment]
static mapping={
comments(sort:"id",order:"asc")
imageData(type:'materialized_blob')
}
static constraints = {
title(blank:false)
text(blank:false,maxSize:1024)
dateCreated()
imageData(nullable:true)
}
}

Comment.groovy
package jp.co.test
class Comment {
String text

static belongsTo=[entry:Entry]
static constraints = {}
}

controller~~~
EntryController.groovy
package jp.co.test
import org.springframework.dao.DataIntegrityViolationException
class EntryController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

def index() {
redirect(action: "list", params: params)
}
def list() {
params.max = Math.min(params.max ? params.int('max'): 10, 100)
if(params.query){
def entryList = Entry.findAllByTitleIlike("%${params.query}%")
def entryCount = entryList ? entryList.size : 0
return [entryInstanceList: entryList, entryInstanceTotal:entryCount]
}

[entryInstanceList: Entry.list(params), entryInstanceTotal: Entry.count()]
}
def create() {
[entryInstance: new Entry(params)]
}
def save() {
def entryInstance = new Entry(params)
if (!entryInstance.save(flush: true)) {
render(view: "create", model: [entryInstance: entryInstance])
return
}
flash.message = message(code: 'default.created.message', args: [
message(code: 'entry.label', default: 'Entry'),
entryInstance.id
])
redirect(action: "show", id: entryInstance.id)
}
def show(Long id) {
def entryInstance = Entry.get(id)
if (!entryInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'entry.label', default: 'Entry'),
id
])
redirect(action: "list")
return
}
[entryInstance: entryInstance]
}
def edit(Long id) {
def entryInstance = Entry.get(id)
if (!entryInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'entry.label', default: 'Entry'),
id
])
redirect(action: "list")
return
}
[entryInstance: entryInstance]
}
def renderImage() {
def entry = Entry.findById(params.id)
if (entry.imageData) {
response.contentLength = entry.imageData.size()
response.outputStream.write(entry.imageData)
}
else {
response.sendError(404)
}
}
def update(Long id, Long version) {
def entryInstance = Entry.get(id)
if (!entryInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'entry.label', default: 'Entry'),
id
])
redirect(action: "list")
return
}
if (version != null) {
if (entryInstance.version > version) {
entryInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[
message(code: 'entry.label', default: 'Entry')] as Object[],
"Another user has updated this Entry while you were editing")
render(view: "edit", model: [entryInstance: entryInstance])
return
}
}
entryInstance.properties = params
if (!entryInstance.save(flush: true)) {
render(view: "edit", model: [entryInstance: entryInstance])
return
}
flash.message = message(code: 'default.updated.message', args: [
message(code: 'entry.label', default: 'Entry'),
entryInstance.id
])
redirect(action: "show", id: entryInstance.id)
}
def delete(Long id) {
def entryInstance = Entry.get(id)
if (!entryInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'entry.label', default: 'Entry'),
id
])
redirect(action: "list")
return
}
try {
entryInstance.delete(flush: true)
flash.message = message(code: 'default.deleted.message', args: [
message(code: 'entry.label', default: 'Entry'),
id
])
redirect(action: "list")
}
catch (DataIntegrityViolationException e) {
flash.message = message(code: 'default.not.deleted.message', args: [
message(code: 'entry.label', default: 'Entry'),
id
])
redirect(action: "show", id: id)
}
}
}

CommentController.groovy
package jp.co.test
import org.springframework.dao.DataIntegrityViolationException
class CommentController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

def index() {
redirect(action: "list", params: params)
}
def list(Integer max) {
params.max = Math.min(max ?: 10, 100)
[commentInstanceList: Comment.list(params), commentInstanceTotal: Comment.count()]
}
def create() {
[commentInstance: new Comment(params)]
}
def save() {
def commentInstance = new Comment(params)
if (!commentInstance.save(flush: true)) {
render(view: "create", model: [commentInstance: commentInstance])
return
}
flash.message = message(code: 'default.created.message', args: [
message(code: 'comment.label', default: 'Comment'),
commentInstance.id
])
redirect(controller: "entry", action: "list")
}
def show(Long id) {
def commentInstance = Comment.get(id)
if (!commentInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'comment.label', default: 'Comment'),
id
])
redirect(action: "list")
return
}
[commentInstance: commentInstance]
}
def edit(Long id) {
def commentInstance = Comment.get(id)
if (!commentInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'comment.label', default: 'Comment'),
id
])
redirect(action: "list")
return
}
[commentInstance: commentInstance]
}
def update(Long id, Long version) {
def commentInstance = Comment.get(id)
if (!commentInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'comment.label', default: 'Comment'),
id
])
redirect(action: "list")
return
}
if (version != null) {
if (commentInstance.version > version) {
commentInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[
message(code: 'comment.label', default: 'Comment')] as Object[],
"Another user has updated this Comment while you were editing")
render(view: "edit", model: [commentInstance: commentInstance])
return
}
}
commentInstance.properties = params

if (!commentInstance.save(flush: true)) {
render(view: "edit", model: [commentInstance: commentInstance])
return
}
flash.message = message(code: 'default.updated.message', args: [
message(code: 'comment.label', default: 'Comment'),
commentInstance.id
])
redirect(action: "show", id: commentInstance.id)
}
def delete(Long id) {
def commentInstance = Comment.get(id)
if (!commentInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'comment.label', default: 'Comment'),
id
])
redirect(action: "list")
return
}
try {
commentInstance.delete(flush: true)
flash.message = message(code: 'default.deleted.message', args: [
message(code: 'comment.label', default: 'Comment'),
id
])
redirect(action: "list")
}
catch (DataIntegrityViolationException e) {
flash.message = message(code: 'default.not.deleted.message', args: [
message(code: 'comment.label', default: 'Comment'),
id
])
redirect(action: "show", id: id)
}
}
}

Viewは長いんで割愛~~
次はPJでも使えそうなTodoリスト作ってみよう~



悲剧

无聊

震惊

支持

不解

超赞

愤怒

高兴

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册账号

小春网
常务客服微信
微信订阅号
手机客户端
扫一扫,查看更方便! 返回顶部