我的目标是创建一个用户可以更改其数据的编辑页面。
这是我的_form:
<div class="fieldcontain ${hasErrors(bean: user, field: 'firstName', 'error')} required">
<label for="firstName">
<g:message code="userSec.firstName.label" default="First Name" />
<span class="required-indicator">*</span>
</label>
<g:textField name="firstName" required="" value="${user?.firstName}"/>
</div>
<div class="fieldcontain ${hasErrors(bean: user, field: 'lastName', 'error')} required">
<label for="lastName">
<g:message code="userSec.lastName.label" default="Last Name" />
<span class="required-indicator">*</span>
</label>
<g:textField name="lastName" required="" value="${user?.lastName}"/>
</div>
<div class="fieldcontain ${hasErrors(bean: user, field: 'username', 'error')} required">
<label for="username">
<g:message code="userSec.username.label" default="Username" />
<span class="required-indicator">*</span>
</label>
<g:textField name="username" required="" value="${user?.username}"/>
</div>
<div class="fieldcontain ${hasErrors(bean: user, field: 'email', 'error')} required">
<label for="email">
<g:message code="userSec.email.label" default="Email" />
<span class="required-indicator">*</span>
</label>
<g:field type="email" name="email" required="" value="${user?.email}"/>
</div>
<div class="fieldcontain ${hasErrors(bean: command, field: 'password', 'error')} required">
<label for="password">
<g:message code="user.password.label" default="Password" />
<span class="required-indicator">*</span>
</label>
<g:field type="password" name="password" class="passwd" required="" value="${command.password}" />
<span id="result"></span>
</div>
<div class="fieldcontain ${hasErrors(bean: command, field: 'password2', 'error')} required">
<label for="password2">
<g:message code="user.password2.label" default="Password (again)" />
<span class="required-indicator">*</span>
</label>
<g:field type="password" name="password2" required="" value="${command.password2}" />
</div>
这是我的编辑页面:
<%@ page import="p2s.UserSec" %>
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main">
<g:set var="entityName" value="${message(code: 'userSec.label', default: 'UserSec')}" />
<title><g:message code="default.edit.label" args="[entityName]" /></title>
</head>
<body>
<a href="#edit-userSec" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content…"/></a>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
</ul>
</div>
<div id="edit-userSec" class="content scaffold-edit" role="main">
<h1><g:message code="default.edit.label" args="[entityName]" /></h1>
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>
<g:hasErrors bean="${user}">
<ul class="errors" role="alert">
<g:eachError bean="${user}" var="error">
<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
</g:eachError>
</ul>
</g:hasErrors>
<g:form url="[resource:user, action:'update']" method="PUT" >
<g:hiddenField name="version" value="${user?.version}" />
<fieldset class="form">
<g:render template="form"/>
</fieldset>
<fieldset class="buttons">
<g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" />
</fieldset>
</g:form>
</div>
</body>
</html>
如何让用户修改他的数据?我在密码方面遇到了很多麻烦,因为我无法对其进行解码,并将其与用户想要更改密码时的密码相匹配。
我想用Spring方法来做这件事。
感谢所有人,对不起,如果这是一个简单的问题,但我是Grails的新手。
答案 0 :(得分:1)
不要显示旧密码,即使是*字符,因为它已经散列并且通常没用。给他们一个空密码字段,如果你想让他们输入两次,请添加一秒,如果他们不匹配则添加验证错误。
如果密码已更改,您不必担心。如果他们重新输入与以前相同的密码,它将具有相同的散列(或在bcrypt中等效的散列),并且该字段不会被视为已修改。只要他们已经过身份验证,您就不应该关心他们是否进行了更改。