我列出了带有复选框的用户表中的用户列表。按下“保存用户”按钮会将所有选择的用户名附加到名为“vals”的字符串中。我为此使用了javascript。我想将此附加的val用户名发送到我的 Controller 操作addUser(),并将其保存到另一个名为group_user的表中。我为此使用remoteFunction()。但是我得到的是“类型不匹配错误”。
GSP页面。
<table>
<g:each in="${Users.list()}" status="i" var="usersInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td>${fieldValue(bean: usersInstance, field: "dname")}</td>
<td>${fieldValue(bean: usersInstance, field: "owner")}</td>
<td>${fieldValue(bean: usersInstance, field: "serailNumber")}</td>
<td>${fieldValue(bean: usersInstance, field: "supportingAndroidVersion")}</td>
<td><g:checkBox name="myCheckbox[${i}]" value="${fieldValue(bean: usersInstance, field: "dname")}" checked="false"/></td>
</tr>
</g:each>
</tbody>
</table>
<button class="btn btn-xs btn-info pull-right" type="button" onclick="getvalue()"> Save User </button>
javascript:-
function getvalue(){
var p = document.getElementById('mytable').rows.length;
var elements = document.querySelectorAll('[id^=myCheckbox]');
<!-- var checkboxes = document.getElementsByName('myCheckbox[]');-->
var vals = "";
for (var i=0, n=p-1;i<n;i++) {
if (elements[i].checked)
{
vals += ","+elements[i].value;
}
}
if (vals) vals = vals.substring(1);
alert(vals)
${remoteFunction (controller:'groupuser', action: 'addUser', params: '\'myString=\' + vals', update: 'addUser')}
}
</script>
Action addUser() in controler groupuser is:-
def addUser()
{
String groupIns = (String)Groupuser.get(params.myString)
// println Groupuser.get(params.myString)
}
***************************************************************************************
The error is as follows:-
The error is
Error 2014-04-07 14:38:25,461 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver - TypeMismatchException occurred when processing request: [POST] /RITE/groupuser/addUser - parameters:
myString: user2,user3,user4
Provided id of the wrong type for class com.rite.Groupuser. Expected: class java.lang.Long, got class java.lang.String. Stacktrace follows:
Message: Provided id of the wrong type for class com.rite.Groupuser. Expected: class java.lang.Long, got class java.lang.String
Line | Method
->> 24 | addUser in com.rite.GroupuserController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 918 | run . . . in ''
^ 662 | run in java.lang.Thread
最佳答案
你的问题:
String groupIns = (String)Groupuser.get(params.myString)
get方法需要Groupuser的ID(http://grails.org/doc/latest/ref/Domain%20Classes/get.html)
如果要使用“dname”获取Groupuser,则必须使用:
Groupuser.findByDname(params.myString)