Grails(9)Guide Book Chapter 7 The Web Layer Web Flow

Grails(9)Guide Book Chapter 7 The Web Layer Web Flow
7.5 Web Flow
Grails supports the creation of web flows built on the Spring Web Flow project.

To install a plugin
>grails install-plugin webflow

7.6 Filters
Although Grails controllers support fine gained interceptors. There are only really useful when applied to a few controllers and become difficult to manage with larger applications. Filters on the other hand can be applied across a whole group of controllers, a URI space or to a specific action.

7.7 Ajax
<g:javascript library="jquery" />

7.7.1.1 Remoting Linking
<g:remoteLink action="delete" id="1">Delete Book</g:remoteLink>

<g:remoteLink update="[success: 'message', failure: 'error']"
action="delete" id="1">
Delete Book
</g:remoteLink>

7.7.1.3 Remote Form Submission
<g:formRemote url="[controller: 'book', action: 'delete']"
update="[success: 'message', failure: 'error']">
<input type="hidden" name="id" value="1" />
<input type="submit" value="Delete Book" />
</g:formRemote>

Data Centric Ajax with JSON
import grails.converters.JSON

def showBook(){
def b = Book.get(params.id)
render b as JSON
}

Data Centric Ajax with XML
import grails.converters.XML

def showBook(){
def b = Book.get(params.id)
render b as XML
}

Responding to both Ajax and non-Ajax requests
It's straightforward to have the same Grails controller action handle both Ajax and non-Ajax requests. Grails adds the isXhr() method to HttpServletRequest which can be used to identify Ajax requests.

7.8 Content Negotiation

8. Validation
8.1 Declaring Constraints
class User {
String login
String password
String email
Integer age

static constraints = {
login size: 5..15, blank: false, unique: true
password size: 5..1, blank: false
email email: true, blank: false
age min: 18
}
}

8.2 Validating Constraints
def user = new User(params)

if(user.validate()){
…snip...
}else{
user.errors.allErrors.each {
println it
}
}

Validation Phases
def user = new User(params)

if(user.hasErrors()){
if(user.errors.hasFieldErrors("login")) {
println user.errors.getFieldError("login").rejectedValue
}
}

By default the save method calls validate before executing, allowing us to write code like this:
if(user.save()){
return user
}else{
user.errors.allErrors.each {
println it
}
}

Importing Constraints
class User {
String firstName
String lastName
String passwordHash

static constraints = {
firstName blank: false, nullable: false
lastName blank: false, nullable: false
passwordHash blank: false, nullable: false
}
}

class UserCommand {
String firstName
String lastName
String password
String confirmPassword

static constraints = {
importFrom User

password blank: false, nullable: false
confirmPassword blank: false, nullable: false
}
}

8.4 Validation on the Client
Displaying Errors
<g:renderErrors bean="${user}" /> // this can render the errors list.

<g:hasErrors bean="${user}">
<ul>
<g:eachError var="err" bean="${user}">
<li>${err}</li>
</g:eachError>
</ul>
</g:hasErrors>

Highlighting Errors
<div class=‘value ${hasErrors(bean:user, field: 'login', 'errors')}’>
<input type="text" name="login" value="${fieldValue(bean:user, field:'login'}" />
</div>

If there is error, there should be errors CSS in the div.

Also, we restore the value input by the user using the fieldValue tag

8.5 Validation and Internationalization

8.6 Applying Validation to Other Classes


References:
http://grails.org/doc/latest/guide/index.html
http://grails.org/doc/latest/guide/theWebLayer.html#webflow
http://grails.org/doc/latest/guide/validation.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值