2016-03-10 136 views
0

得到錯誤,而使用@ModelAttribute,嘗試了許多解決辦法,但找不到任何答案春季使用@ModelAttribute冬眠

我控制器

@RequestMapping(value = "/user", params = "add", method = RequestMethod.POST) 
public String postAddUser(
     @RequestParam("name") String name, 
     @RequestParam("role") Role role, 
     @RequestParam("email_address") String emailAddress 
     ) { 
    System.out.println("Inside postAddUser"); 
    User user = new User(name, role,emailAddress); 
    user = userRepository.save(user); 
    return "redirect:user?id=" + user.getId(); 
} 

我的看法頁:

<title>Add User</title> 
</head> 
<body> 

    <div class="container"> 
     <div class="row"> 
      <div class="col-md-12"> 
       <div class="col-md-4"> 
        <h2>Add User</h2> 
        <form action="user" method="post" role="form"> 


         <div class="form-group "> 
          <label for="user">User Name:</label> 
          <input type="text" 
           class="form-control" name="name" id="name" 
           placeholder="Enter User Name" required> 
         </div> 


         <div class="form-group"> 
          <label for="email">Email Address:</label> <input type="email" 
           name="email_address" class="form-control" id="email" 
           placeholder="Enter email Address" required> 
         </div> 

         <div class="form-group"> 
          <label for="role">Role:</label> 
          <select name="role" 
           class="form-control" id="role" placeholder="Enter Role" required> 
           <option disabled selected></option> 
           <c:forEach var="role" items="${roles}"> 
            <option value="${role.value}">${role.name}</option> 
           </c:forEach> 
          </select> 
         </div> 

         <button type="submit" value="Submit" name="add" 
          class="btn btn-info active">Submit</button> 
         <a href="adminHome" class="btn btn-info" role="button">Back</a> 

        </form> 


       </div> 
       <div class="col-md-4"> 
        <div class="col-md-4"></div> 
       </div> 
      </div> 
     </div> 
    </div> 
</body> 

用戶Bean

@Entity 
@Table(name="USER") 
public class User extends BaseEntity{ 

    @Column(name="NAME") 
    private String name; 

    @Column(name="ROLE") 
    private Role role; 

    @Column(name = "EMAIL_ADDRESS", nullable = false) 
    private String emailAddress; 

    public User(){} 



    public User(String name, Role role, String emailAddress) { 
     super(); 
     this.name = name; 
     this.role = role; 
     this.emailAddress = emailAddress; 
    } 



    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public Role getRole() { 
     return role; 
    } 
    public void setRole(Role role) { 
     this.role = role; 
    } 

    public String getEmailAddress() { 
     return emailAddress; 
    } 

    public void setEmailAddress(String emailAddress) { 
     this.emailAddress = emailAddress; 
    } 



} 

我試過在我的控制器中使用@ModelAttribute,但不知何故,當它涉及到控制器端時,電子郵件的值爲空。

將代碼更改爲適合@ModelAttribute後,下面的代碼相同。

控制器端。

@RequestMapping(value = "/user", params = "add", method = RequestMethod.POST) 
    public String postAddUser(@ModelAttribute("user")User user) { 
     System.out.println("Inside postAddUser"); 
     System.out.println(user.getId()); 
     System.out.println(user.getEmailAddress()); 
     System.out.println(user.getName()); 
     System.out.println(user.getRole()); 
     user = userRepository.save(user); 
     return "redirect:user?id=" + user.getId(); 
    } 

查看頁面

<title>Add User</title> 
</head> 
<body> 

    <div class="container"> 
     <div class="row"> 
      <div class="col-md-12"> 
       <div class="col-md-4"> 
        <h2>Add User</h2> 
        <form action="user" method="post" role="form"> 


         <div class="form-group "> 
          <label for="user">User Name:</label> 
          <input type="text" 
           class="form-control" name="name" id="name" 
           placeholder="Enter User Name" value="${user.name}" required> 
         </div> 


         <div class="form-group"> 
          <label for="email">Email Address:</label> <input type="email" 
           name="email_address" class="form-control" id="email" 
           placeholder="Enter email Address" value="${user.emailAddress}" value="${role.value}" required> 
         </div> 

         <div class="form-group"> 
          <label for="role">Role:</label> 
          <select name="role" 
           class="form-control" id="role" placeholder="Enter Role" required> 
           <option disabled selected></option> 
           <c:forEach var="role" items="${roles}"> 
            <option value="${role.value}">${role.name}</option> 
           </c:forEach> 
          </select> 
         </div> 

         <button type="submit" value="Submit" name="add" 
          class="btn btn-info active">Submit</button> 
         <a href="adminHome" class="btn btn-info" role="button">Back</a> 

        </form> 


       </div> 
       <div class="col-md-4"> 
        <div class="col-md-4"></div> 
       </div> 
      </div> 
     </div> 
    </div> 
</body> 

下面是我得到

Inside postAddUser 
0 
null 
Harding Berg 
HEAD_OF_DEPARTMENT 
Mar 10, 2016 2:49:33 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet spring threw exception 
org.hsqldb.HsqlException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10097 table: USER column: 
EMAIL_ADDRESS 
    at org.hsqldb.error.Error.error(Unknown Source) 
    at org.hsqldb.Table.enforceRowConstraints(Unknown Source) 
    at org.hsqldb.Table.insertSingleRow(Unknown Source) 
    at org.hsqldb.StatementDML.insertRowSet(Unknown Source) 
    at org.hsqldb.StatementInsert.getResult(Unknown Source) 
    at org.hsqldb.StatementDMQL.execute(Unknown Source) 
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source) 
    at org.hsqldb.Session.execute(Unknown Source) 
    at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source) 
    at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source) 
    at com.jolbox.bonecp.PreparedStatementHandle.executeUpdate(PreparedStatementHandle.java:203) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHa 
ndler.java:122) 
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) 
    at com.sun.proxy.$Proxy58.executeUpdate(Unknown Source) 
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96) 
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58) 
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2870) 
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3381) 
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81) 
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362) 
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203) 
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183) 
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167) 
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320) 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287) 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193) 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126) 
    at org.hibernate.ejb.event.EJB3MergeEventListener.saveWithGeneratedId(EJB3MergeEventListener.java:71) 
    at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:236) 
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:216) 
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154) 
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76) 
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:900) 
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:884) 
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:888) 
    at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:879) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEnt 
ityManagerCreator.java:365) 
    at com.sun.proxy.$Proxy43.merge(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityMan 
agerCreator.java:240) 
    at com.sun.proxy.$Proxy43.merge(Unknown Source) 
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:329) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMe 
thodOn(RepositoryFactorySupport.java:368) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(Re 
positoryFactorySupport.java:349) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationI 
nterceptor.java:155) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
    at com.sun.proxy.$Proxy51.save(Unknown Source) 
    at in.ac.jmi.controllers.AdminController.postAddUser(AdminController.java:87) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocab 
leHandlerMethod.java:96) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMapp 
ingHandlerAdapter.java:617) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHa 
ndlerAdapter.java:578) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:8 
0) 
    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) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.j 
ava:113) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    at java.lang.Thread.run(Thread.java:745) 

回答

2

您有波紋管代碼問題:

<input type="email" name="email_address" class="form-control" id="email" placeholder="Enter email Address" value="${user.emailAddress} value="${role.value}" required> 

您需要使用精確的Java對象的字段名稱在輸入字段的你的名字屬性,它從查看地圖。所以使用emailAddress而不是email_address。 我不明白你爲什麼使用了兩個值屬性!刪除值=「$ {role.value}」。

您的代碼應該是這樣的:

<input type="email" name="emailAddress" class="form-control" id="email" placeholder="Enter email Address" value="${user.emailAddress} required> 

當你使用Spring,你也可以在這裏使用Spring標籤庫。如果您使用<form:input path="emailAddress">它將自動綁定到用戶對象字段emailAddress並在窗體視圖中顯示現有的字段值。

0

嘗試使用@RequestBody

@RequestMapping(value = "/user", params = "add", method = RequestMethod.POST) 
    public String [email protected] User user) { 
+0

你是這樣說的嗎? @RequestMapping(value =「/ user」,params =「add」,method = RequestMethod。POST) \t public String postAddUser @RequestBody(@ModelAttribute(「user」)User user){它表示「註釋@RequestMapping不允許用於此位置」 –

+0

請勿使用@ModelAttribute。 只有 '公共字符串postAddUser @ RequestBody用戶用戶){' –

+0

我使用這樣 '@RequestMapping(值= 「/ addBuyer」,方法= RequestMethod.POST) 公共買方addProductUser(@RequestBody買家買方)拋出NullParamException {' –

0

如果你不希望的電子郵件地址是錯誤null使用rignte名字liek thi小號

<input type="email" name="emailAddress" class="form-control" id="email" placeholder="Enter email Address" value="${user.emailAddress}" value="${role.value}" required> 

我建議你使用

<form:*> 

標記庫生成的HTML

關於@ModelAttribute:

您還應該創建一個方法@ModelAttribute(」用戶「),否則僅在方法的參數上使用它是無用的。

+0

請問您可以在這裏分享這個例子..我是新的春天 –

+0

看這裏: http://docs.spring.io/spring/docs/current/spring-framework-reference/ HTML/mvc.html#MVC-ANN-modelattrib的方法 – jpprade