简体   繁体   中英

Spring <form:select> tag causing error in Spring mvc + Hibernate app

First of all I'd like to say that I've searched for the solution all over the Internet and SO, but found no solution for this particular situation. I am just learning about java web programming. I am making simple mvc app for adding teachers to MySQL database. DB has only 3 tables: 'teacher', 'title' and join table 'teacher_title'. 'title' is a lookup table, with 'title_id' / 'title_description' values. In my jsp page I have drop-down list for selecting the teacher title (eg PhD, MSc, etc.), 2 input fields for entering the first name and the last name of the teacher and a submit/save button.

This is my controller:

    /**
     * Handles and retrieves the add teacher page
     */
    @RequestMapping(value="/add", method = RequestMethod.GET)
    public String getAddTeacher(Model model) {

        logger.debug("Received request to show add teacher page");

        // Add to model
        model.addAttribute("teacherAttribute", new Teacher());
        model.addAttribute("titleList", titleService.getAll());

        return "addTeacher";
    }

    /**
     * Adds a new teacher by delegating the processing to TeacherService.
     * Displays a confirmation JSP page
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public String postAddTeacher(@RequestParam(value = "id") Integer titleId, 
                                @ModelAttribute("teacherAttribute") Teacher teacher) {

        logger.debug("Received request to add new teacher");

        // Call TeacherService to do the actual adding
        teacherService.add(titleId, teacher);

        return "addTeacherSuccess";
    }

This is the important part of addTeacher.jsp page:

<c:url var="saveUrl" value="/essays/main/teacher/add?id=titleId" />
<form:form modelAttribute="teacherAttribute" method="POST" action="${saveUrl}">
        <form:label path="title">Title:</form:label>
        <form:select path="title" id="titleSelect">
            <form:option value="0" label="Select" />
            <form:options items="${titleList}" itemValue="titleId" itemLabel="titleDescription" />              
        </form:select>
...

This is Teacher entity:

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "TEACHER_ID")
    private Integer techerId;

    @Column(name = "FIRST_NAME", length = 50)
    private String firstName;

    @Column(name = "LAST_NAME", length = 50)
    private String lastName;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
        @JoinTable(name="teacher_title",
        joinColumns = @JoinColumn(name="TEACHER_ID"),
        inverseJoinColumns = @JoinColumn(name="TITLE_ID")
                )
    private Title title;
// getters and setters
}

This is Title entity:

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "TITLE_ID")
    private Integer titleId;

    @Column(name = "TITLE_DESCRIPTION", length = 10)
    private String titleDescription;
// getters and setters
}

And just in case, these are Teacher and Title services, respectively:

    /**
     * Adds new teacher
     */
    public void add(Integer titleId, Teacher teacher) {

        logger.debug("Adding new teacher");

        Session session = sessionFactory.getCurrentSession();

        // Retrieve existing title via id
        Title existingTitle = (Title) session.get(Title.class, titleId);

        // Add title to teacher
        teacher.setTitle(existingTitle);

        // Persists to db
    session.save(teacher);
    }

.

    /**
     * Retrieves a list of titles
     */
    public List<Title> getAll() {

        logger.debug("Retrieving all titles");

        Session session = sessionFactory.getCurrentSession();

        Query query = session.createQuery("FROM Title");

        List<Title> titleList = castList(Title.class, query.list());

        return titleList;
    }

    public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> c) {
        List<T> r = new ArrayList<T>(c.size());
        for(Object o: c)
          r.add(clazz.cast(o));
        return r;
    }

    /**
     * Retrieves a single title
     */
    public Title get(Integer titleId) {

        logger.debug("Retrieving single title");

        Session session = sessionFactory.getCurrentSession();

        // Retrieve existing title
        Title title = (Title) session.get(Title.class, titleId);

        return title;
    }

What I've managed to understand so far is that the problem is with the select tag in my jsp. When I comment out select tag and eneter the parameter manually (eg URL?id=1), saving to the DB works just fine. Otherwise I get the following error: "HTTP Status 400 - The request sent by the client was syntactically incorrect.". I have searched the Internet for this error too, but found no applicable solutions.

It is just beyond my humble knowledge to determine where I'm making a mistake. Thank you in advance.

Update : This is new controller method for adding new teacher, where @RequestParam 's value attribute was changed from id to title :

@RequestMapping(value="/add", method = RequestMethod.POST)
public String postAddTeacher(@RequestParam(value = "title") Integer titleId, 
@ModelAttribute("teacherAttribute") Teacher teacher) {

logger.debug("Received request to add new teacher");

// Call TeacherService to do the actual adding
teacherService.add(titleId, teacher);

// This will resolve to /WEB-INF/jsp/addTeacherSuccess.jsp
return "addTeacherSuccess";

}

And this is new addTeacher.jsp, where ?id=titleId was deleted from URL:

<c:url var="saveUrl" value="/essays/main/teacher/add" />
<form:form modelAttribute="teacherAttribute" method="POST" action="${saveUrl}">
        <form:label path="title">Title:</form:label>
        <form:select path="title" id="titleSelect">
            <form:option value="0" label="Select" />
            <form:options items="${titleList}" itemValue="titleId" itemLabel="titleDescription" />              
        </form:select>
...

Still getting the same error as before.

Here is the error I'm getting (and I don't understand):

[TRACE] [http-bio-8080-exec-7 11:10:38] (InvocableHandlerMethod.java:getMethodArgumentValues:166) Error resolving argument [1] [type=com.jovana.domain.Teacher]
HandlerMethod details: 
Controller [com.jovana.controller.MainController]
Method [public java.lang.String com.jovana.controller.MainController.postAddTeacher(java.lang.Integer,com.jovana.domain.Teacher)]

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'teacherAttribute' on field 'title': rejected value [4]; codes [typeMismatch.teacherAttribute.title,typeMismatch.title,typeMismatch.com.jovana.domain.Title,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [teacherAttribute.title,title]; arguments []; default message [title]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.jovana.domain.Title' for property 'title'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.jovana.domain.Title] for property 'title': no matching editors or conversion strategy found]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:110)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
[TRACE] [http-bio-8080-exec-7 11:10:38] (InvocableHandlerMethod.java:getMethodArgumentValues:166) Error resolving argument [1] [type=com.jovana.domain.Teacher]
HandlerMethod details: 
Controller [com.jovana.controller.MainController]
Method [public java.lang.String com.jovana.controller.MainController.postAddTeacher(java.lang.Integer,com.jovana.domain.Teacher)]

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'teacherAttribute' on field 'title': rejected value [4]; codes [typeMismatch.teacherAttribute.title,typeMismatch.title,typeMismatch.com.jovana.domain.Title,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [teacherAttribute.title,title]; arguments []; default message [title]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.jovana.domain.Title' for property 'title'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.jovana.domain.Title] for property 'title': no matching editors or conversion strategy found]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:110)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
[DEBUG] [http-bio-8080-exec-7 11:10:38] (AbstractHandlerExceptionResolver.java:resolveException:132) Resolving exception from handler [public java.lang.String com.jovana.controller.MainController.postAddTeacher(java.lang.Integer,com.jovana.domain.Teacher)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'teacherAttribute' on field 'title': rejected value [4]; codes [typeMismatch.teacherAttribute.title,typeMismatch.title,typeMismatch.com.jovana.domain.Title,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [teacherAttribute.title,title]; arguments []; default message [title]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.jovana.domain.Title' for property 'title'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.jovana.domain.Title] for property 'title': no matching editors or conversion strategy found]
[DEBUG] [http-bio-8080-exec-7 11:10:38] (AbstractHandlerExceptionResolver.java:resolveException:132) Resolving exception from handler [public java.lang.String com.jovana.controller.MainController.postAddTeacher(java.lang.Integer,com.jovana.domain.Teacher)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'teacherAttribute' on field 'title': rejected value [4]; codes [typeMismatch.teacherAttribute.title,typeMismatch.title,typeMismatch.com.jovana.domain.Title,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [teacherAttribute.title,title]; arguments []; default message [title]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.jovana.domain.Title' for property 'title'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.jovana.domain.Title] for property 'title': no matching editors or conversion strategy found]
[DEBUG] [http-bio-8080-exec-7 11:10:38] (AbstractHandlerExceptionResolver.java:resolveException:132) Resolving exception from handler [public java.lang.String com.jovana.controller.MainController.postAddTeacher(java.lang.Integer,com.jovana.domain.Teacher)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'teacherAttribute' on field 'title': rejected value [4]; codes [typeMismatch.teacherAttribute.title,typeMismatch.title,typeMismatch.com.jovana.domain.Title,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [teacherAttribute.title,title]; arguments []; default message [title]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.jovana.domain.Title' for property 'title'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.jovana.domain.Title] for property 'title': no matching editors or conversion strategy found]
[DEBUG] [http-bio-8080-exec-7 11:10:38] (AbstractHandlerExceptionResolver.java:resolveException:132) Resolving exception from handler [public java.lang.String com.jovana.controller.MainController.postAddTeacher(java.lang.Integer,com.jovana.domain.Teacher)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'teacherAttribute' on field 'title': rejected value [4]; codes [typeMismatch.teacherAttribute.title,typeMismatch.title,typeMismatch.com.jovana.domain.Title,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [teacherAttribute.title,title]; arguments []; default message [title]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.jovana.domain.Title' for property 'title'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.jovana.domain.Title] for property 'title': no matching editors or conversion strategy found]
[DEBUG] [http-bio-8080-exec-7 11:10:38] (AbstractHandlerExceptionResolver.java:resolveException:132) Resolving exception from handler [public java.lang.String com.jovana.controller.MainController.postAddTeacher(java.lang.Integer,com.jovana.domain.Teacher)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'teacherAttribute' on field 'title': rejected value [4]; codes [typeMismatch.teacherAttribute.title,typeMismatch.title,typeMismatch.com.jovana.domain.Title,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [teacherAttribute.title,title]; arguments []; default message [title]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.jovana.domain.Title' for property 'title'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.jovana.domain.Title] for property 'title': no matching editors or conversion strategy found]
[DEBUG] [http-bio-8080-exec-7 11:10:38] (AbstractHandlerExceptionResolver.java:resolveException:132) Resolving exception from handler [public java.lang.String com.jovana.controller.MainController.postAddTeacher(java.lang.Integer,com.jovana.domain.Teacher)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'teacherAttribute' on field 'title': rejected value [4]; codes [typeMismatch.teacherAttribute.title,typeMismatch.title,typeMismatch.com.jovana.domain.Title,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [teacherAttribute.title,title]; arguments []; default message [title]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.jovana.domain.Title' for property 'title'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.jovana.domain.Title] for property 'title': no matching editors or conversion strategy found]
[DEBUG] [http-bio-8080-exec-7 11:10:38] (DispatcherServlet.java:processDispatchResult:999) Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
[DEBUG] [http-bio-8080-exec-7 11:10:38] (DispatcherServlet.java:processDispatchResult:999) Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
[TRACE] [http-bio-8080-exec-7 11:10:38] (FrameworkServlet.java:resetContextHolders:1028) Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@5f57b190
[TRACE] [http-bio-8080-exec-7 11:10:38] (FrameworkServlet.java:resetContextHolders:1028) Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@5f57b190
[DEBUG] [http-bio-8080-exec-7 11:10:38] (FrameworkServlet.java:processRequest:966) Successfully completed request
[DEBUG] [http-bio-8080-exec-7 11:10:38] (FrameworkServlet.java:processRequest:966) Successfully completed request
[TRACE] [http-bio-8080-exec-7 11:10:38] (AbstractApplicationContext.java:publishEvent:332) Publishing event in WebApplicationContext for namespace 'spring-servlet': ServletRequestHandledEvent: url=[/testLookupTable/essays/main/teacher/add]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[spring]; session=[72089007DB0E4468BA90CDA44FCA8CC0]; user=[null]; time=[144ms]; status=[OK]
[TRACE] [http-bio-8080-exec-7 11:10:38] (AbstractApplicationContext.java:publishEvent:332) Publishing event in WebApplicationContext for namespace 'spring-servlet': ServletRequestHandledEvent: url=[/testLookupTable/essays/main/teacher/add]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[spring]; session=[72089007DB0E4468BA90CDA44FCA8CC0]; user=[null]; time=[144ms]; status=[OK]
[TRACE] [http-bio-8080-exec-7 11:10:38] (AbstractApplicationContext.java:publishEvent:332) Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/testLookupTable/essays/main/teacher/add]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[spring]; session=[72089007DB0E4468BA90CDA44FCA8CC0]; user=[null]; time=[144ms]; status=[OK]
[TRACE] [http-bio-8080-exec-7 11:10:38] (AbstractApplicationContext.java:publishEvent:332) Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/testLookupTable/essays/main/teacher/add]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[spring]; session=[72089007DB0E4468BA90CDA44FCA8CC0]; user=[null]; time=[144ms]; status=[OK]

Solution (thank you storm_buster) is adding this to the controller:

@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {

        binder.registerCustomEditor(Title.class, "title", new PropertyEditorSupport() {
         @Override
         public void setAsText(String text) {
            setValue((text.equals(""))?null:titleService.getTitle(Integer.parseInt((String)text)));
         }
     });
}

The end!

you are getting this error because you are using

@ModelAttribute("teacherAttribute") Teacher teacher)

Spring is unable to convert your requestParam "title" ( which is string ) to a Title class which belongs to a Teacher Entity.

You have two solution : 1- add this to your controller

@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {

        binder.registerCustomEditor(Title.class, "title", new PropertyEditorSupport() {
         @Override
         public void setAsText(String text) {
            setValue((text.equals(""))?null:titleService.getTitle(Integer.parseInt((String)text)));
         }
     });
}   

2- Modify your requestmapping as:

@RequestMapping(value="/add", method = RequestMethod.POST)
public String postAddTeacher(@RequestParam(value = "title") Integer titleId, 
@RequestParam("teacherAttribute") Integer teacherId) {

And add a new Interface:

teacherService.add(titleId, teacherId);

<c:url var="saveUrl" value="/essays/main/teacher/add?id=titleId" />

You don't need to write params as ?id=titleId manually, it will be added automatically. In this case in request body (because it's POST request).

Also path attribute in form tag and value of @RequestParam in controller attributes must be the same. So write, for example @RequestParam(value = "title") Integer titleId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM