spring 多对多的保存

wenwen1 发布于 2012/11/02 16:16
阅读 1K+
收藏 0

我的jsp如下:

<%@page language="java" isELIgnored="false" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<jsp:directive.include file="/WEB-INF/sitemesh-decorators/include.jsp"/>
<fmt:setBundle basename="bundles.userinfo-resources"/>
<html>
<head>
<title>Edit <fmt:message key="team.title"/></title>
</head>
<body>
<div id="contentarea">      
    <div id="lb"><div id="rb"><div id="bb"><div id="blc">
    <div id="brc"><div id="tb"><div id="tlc"><div id="trc">
    <div id="content">
        <form:form action="${pageContext.request.contextPath}/saveUserAuthority" method="POST" modelAttribute="user">
            <table cellpadding="0" cellspacing="0" id="viewTable">
                <tbody>
                <tr>
                         <td class="label" valign="top">
                            <fmt:message key="user.id.title"/>:
                        </td>
                        <td colspan="2">
                            <c:choose>
                                <c:when test='${newFlag}' >
                            <form:input id="user_id" path="id" cssStyle="width:300px;"/>
                            <script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : "user_id",widgetType : "dijit.form.NumberTextBox",widgetAttrs : {promptMessage: "<fmt:message key="user.id.help"/>", constraints : {places:0}}})); </script>
                                </c:when>
                                <c:otherwise>
                            ${user.id}
                        &nbsp;
                        <form:hidden path="id"/>
                                </c:otherwise>
                            </c:choose>
                        </td>
                    </tr>
                    <tr>
                    <td class="label" valign="top">
                    
                    </td>
                    
                    </tr>
                    <tr>
                     <td class="label" valign="top">
                   <fmt:message key="user.authority.title"/>:
                  </td>
                <c:forEach items="${authorities}" var="current" varStatus="i">
                    <td >
                      <form:checkbox path="authorities" value="${current.id}" />
                        ${current.CName}
                    </td>
                </c:forEach>
                </tbody>
            </table>
            <span class="inputbutton"><input class="savebutton" id="save" type="submit" value="<fmt:message key="navigation.save"/>"/></span>
            <script type="text/javascript">Spring.addDecoration(new Spring.ValidateAllDecoration({elementId:'save', event:'onclick'}));</script>
        </form:form>
        <div class="clear">&nbsp;</div>
    </div>
    </div></div></div></div>
    </div></div></div></div>
</div>
</body>
</html>

control层如下:/*
     * save a User Authority
     */
    @RequestMapping("/saveUserAuthority")
    public String saveUserAuthority(@ModelAttribute User user)
    {
        systemManageService.saveUserAuthority(user);
        return "forward:/sys/authorityList";
    }

Service层如下:

public void saveUserAuthority(User user)
{
    int userId=user.getId();
    Set<Authority> authority=user.getAuthorities();
    user.setAuthorities(authority);
    user.setId(userId);
  userDAO.store(user);
  userDAO.flush();
}

保存时报如下错误:

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'user' on field 'authorities': rejected value [2]; codes [typeMismatch.user.authorities,typeMismatch.authorities,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.authorities,authorities]; arguments []; default message [authorities]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Set' for property 'authorities'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [net.gvsun.preparation.domain.Authority] for property 'authorities[0]': no matching editors or conversion strategy found]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:111)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

怎么解决啊?

加载中
0
震秦
震秦

Cannot convert value of type [java.lang.String] to required type [net.gvsun.preparation.domain.Authority] for property 'authorities[0]'.看到没?你把数据类型搞错啦!

很可能是这里:Set<Authority> authority=user.getAuthorities();

wenwen1
wenwen1
哦。
返回顶部
顶部