视频连接:http://www.ilovexuexi.com/Grails/k45(视频链接来自ilove学习网)
第一步:根据_form.gsp改造登录界面
_loginfrom.gsp代码如下:
<%@ page import="myclass.User" %>
<div class="fieldcontain ${hasErrors(bean: userInstance, field: 'name', 'error')} ">
<label for="name">
名字:
</label>
<g:textField name="name" />
</div>
<div class="fieldcontain ${hasErrors(bean: userInstance, field: 'password', 'error')} ">
<label for="password">
密码:
</label>
<input type="password" name="password"/>
</div>
第二步:根据create.gsp修改login.gsp代码如下:
!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main">
<g:set var="entityName" value="${message(code: 'user.label', default: 'User')}" />
<title>用户登录</title>
</head>
<body>
<a href="#create-user" 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>
<li><g:link class="list" action="index"><g:message code="default.list.label" args="[entityName]" /></g:link></li>
</ul>
</div>
<div id="create-user" class="content scaffold-create" role="main">
<h1>用户登录</h1>
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>
<g:hasErrors bean="${userInstance}">
<ul class="errors" role="alert">
<g:eachError bean="${userInstance}" 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:userInstance, action:'login1']">
<fieldset class="form">
<g:render template="loginform"/>
</fieldset>
<fieldset class="buttons">
<g:submitButton name="login" class="save" value="登录" />
</fieldset>
</g:form>
</div>
</body>
</html>
第三步:在UserController.groovy中添加一个空的login方法或闭包再添加一个login1方法或闭包
代码如下:
def login(){
}
def login1(){
if(!User.findByNameAndPassword(params.name,params.password)){//关键代码
flash.message="用户名或密码错误,请重新输入!"
redirect(action:"login")
}
else{
render "登录成功"
}
}