我正在使用grails,我對它很陌生。我需要在grails中使用sql查詢登錄。請幫幫我。這是我的視圖頁面,控制器和域類。使用sql查詢的grails登錄
login.gsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="layout"content="main"/>
<g:set var="entityName" value="ProjectTracker Login" />
<title><g:message code="projectTracker login" args="[entityName]" /></title>
</head>
<body>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri:"/")}"></a></li>
<li><g:link class="list" action="logout">Logout</g:link></li>
</ul>
</div>
<div id="create-endUser" class="content scaffold-create" role="main">
<h1>Login</h1>
<g:form action="authenticate" >
<fieldset class="form">
<div class="fieldcontain ${hasErrors(bean: endUserInstance, field: 'userName', 'error')} ">
<label for="userName">
<g:message code="endUser.userName.label" default="User Name" />
</label>
<g:textField name="userName" value="${endUserInstance?.userName}"/>
</div>
<div class="fieldcontain ${hasErrors(bean: endUserInstance, field: 'password', 'error')} ">
<label for="password">
<g:message code="endUser.password.label" default="Password" />
</label>
<g:field type="password" name="password" value="${endUserInstance?.password}"/>
</div>
</fieldset>
<fieldset class="buttons">
<g:submitButton name="login" class="save" value="Login" />
</fieldset>
</g:form>
</div>
</body>
</html>
我的領域類
Testuser.groovy
class Testuser {
String userName
String password
String fullName
String toString(){
"${fullName}"
}
static constraints = {
fullName();
userName(unique:true);
password(password:true);
}
}
我控制器
TestuserController.groovy
import groovy.sql.*
def index()
{
redirect(action:"login")
}
def login={ }
def authenticate={
def username_log = parmas.userName
def password_log = parmas.password
def results
Sql sql = new Sql(dataSource)
def joining = sql.rows("SELECT user_name,password from user where user_name=username_log and password=password_log")
{
results << it.toRowResult()
}
results
if (joining != null) {
results = sql.eachRow()
redirect(action:"login")
}
else {
redirect(action:"login")
}
這是使用SQL查詢我的簡單登錄代碼和我收到錯誤,如
URI
/login/testuser/authenticate
Class
groovy.lang.MissingPropertyException
Message
No such property: parmas for class: login.test.TestuserController Possible solutions: params
這是'params'不'parmas'。錯誤消息從字面上告訴你這一點。 – codelark
我知道這不是問題,但請閱讀有關Bob Martin乾淨代碼等乾淨代碼的書。這段代碼只有很多問題。爲什麼命名變量'username_log'和'password_log'?爲什麼'username'和'params.userName'之間的不一致?爲什麼在'def login = {}'之間沒有空格,而在'def authenticate = {'後面有兩行?爲什麼將'sql.eachRow()'賦值給'results'並從不使用它?爲什麼在同一行上將大括號放在一個新的行上,並用大括號括起來?在你的代碼中引以自豪。 – grantmcconnaughey