Grails命令对象重定向与模型具有相同的URL /页面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Grails命令对象重定向与模型具有相同的URL /页面相关的知识,希望对你有一定的参考价值。
我创建了一个用户名,密码和电子邮件的用户域,我想使用Command对象检查重复的密码和重复的电子邮件是否相同。我创建了一个UserController:
class UserController {
static scaffold = User
def register() { }
def registration(UserRegistrationCommand urc) {
if (urc.hasErrors()) {
render(view: 'register', model: [user: urc])
} else {
def user = new User(urc.properties)
user.profile = new Profile()
if (user.save()) {
flash.message = "Congratulations ${user.username}, you've been correctly registered. An email as been sent to ${user.email} in order to activate your account."
redirect(uri: '/')
} else {
return [user: user]
}
}
}
}
class UserRegistrationCommand {
String username
String password
String passwordRepeat
String email
String emailRepeat
static constraints = {
importFrom User
importFrom Profile
password(size: 6..8, blank: false, validator: {pwd, urc -> return pwd != urc.username})
passwordRepeat(nullable: false, validator: {pwd, urc -> return pwd == urc.password})
emailRepeat(nullable: false, email: true, validator: {addr, urc ->
return addr == urc.email})
}
}
并且只有“register.gsp”视图:
<html>
<head>
<meta name="layout" content="main">
</head>
<body>
<h1>Register New User</h1>
<g:hasErrors>
<div class="errors">
<g:renderErrors bean="${user}" as="list" />
</div>
</g:hasErrors>
<g:if test="${flash.message}">
<div class="flash">${flash.message}</div>
</g:if>
<g:form controller="user" action="registration" method="POST" >
<fieldset class="form">
<div class="fieldcontain required">
<label for="username">Username</label>
<g:textField name="username" required="" value="${user?.username}" />
</div>
<div class="fieldcontain required">
<label for="password">Password</label>
<g:passwordField name="password" required="" />
</div>
<div class="fieldcontain required">
<label for="passwordRepeat">Repeat Password</label>
<g:passwordField name="passwordRepeat" required="" />
</div>
<div class="fieldcontain required">
<label for="email">Email</label>
<g:textField name="email" required="" type="email" value="${user?.email}" />
</div>
<div class="fieldcontain required">
<label for="emailRepeat">Repeat Email</label>
<g:textField name="emailRepeat" required="" />
</div>
</fieldset>
<fieldset class="buttons">
<g:submitButton name="register" value="Register" />
</fieldset>
</g:form>
</body>
</html>
当我编译带有错误的表单时会调用“urc.errors”,它会将我重定向到/ user / registration并在那里出现错误而不是/ user / register我想要的。如果数据不正确,有没有办法只使用“用户/注册”页面来编译表单并提交数据或编辑表单?
答案
你需要一个register
和registration
动作吗?您可以只使用一个register
操作,并且仅在用户提交表单时才会响应,例如
控制器:
...
def register(UserRegistrationCommand urc) {
if (params.register) {
if (urc?.hasErrors()) {
[user: urc]
} else {
def user = new User(urc.properties)
user.profile = new Profile()
if (user.save()) {
flash.message = "Congratulations ${user.username}, you've been correctly registered. An email as been sent to ${user.email} in order to activate your account."
redirect(uri: '/')
} else {
return [user: user]
}
}
}
}
...
GSP:
...
<g:form controller="user" action="register" method="POST" >
...
以上是关于Grails命令对象重定向与模型具有相同的URL /页面的主要内容,如果未能解决你的问题,请参考以下文章