0

Getting error while using @ModelAttribute, tried many of the solutions but could not find any answers

My controller is

@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();
}

My view page is:

<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>

User 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;
    }



}

I tried using @ModelAttribute in my controller but somehow value for email is null in when it comes to controller side.

Below is code for same after changing my code to fit @ModelAttribute.

Controller side.

@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();
    }

View page

<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>

Below is the error I am getting

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)

3 Answers 3

2

You have problem with the bellow code :

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

You need to use the exact java object field name at your name attribute of input field to map it from view. So use emailAddress instead of email_address. I do not understand why you've used two value attributes! Remove value="${role.value}".

Your code should look like this :

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

As you are using spring you can also use spring tag library here. If you use <form:input path="emailAddress"> it will automatically bind with user object field emailAddress and show the existing field value at form view.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to use @RequestBody

@RequestMapping(value = "/user", params = "add", method = RequestMethod.POST)
    public String postAddUser@RequestBody User user) {

6 Comments

Are you saying like this? @RequestMapping(value = "/user", params = "add", method = RequestMethod.POST) public String postAddUser @RequestBody(@ModelAttribute("user")User user) { it says "The annotation @RequestMapping is disallowed for this location"
Do not use @ModelAttribute. Only public String postAddUser@RequestBody User user) {
I am using like this @RequestMapping(value = "/addBuyer", method = RequestMethod.POST) public Buyer addProductUser(@RequestBody Buyer buyer) throws NullParamException {
not working .. can you please share same part of the code i.e. controller and jsp part
@RequestMapping(value = "/user", params = "add", method = RequestMethod.POST) public String postAddUser(@RequestBody 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(); }
|
0

If you dont want the email adress to be null use the rignte name liek this

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

I suggest you to use the

<form:*> 

tag lib to generate your html

About the @ModelAttribute :

You should also create a method @ModelAttribute("user") otherwise using it only on a parameter of method is useless.

2 Comments

can you please share the example here..I am new in spring

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.