尝试从下拉列表添加到数据库

时间:2019-04-18 07:22:17

标签: java hibernate spring-boot

我正在尝试为用户添加一个新事件,但是它不起作用,认为问题是html形式的,关系很多。使用Spring Boot并进入休眠状态

谢谢。 html表单

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
<!--Include _menu.html -->
<th:block th:include="/menu"></th:block>
<h1>Person added</h1>

<table border="1">
    <tr>
        <th>ID</th>
        <th>First Name</th>
        <th>Last name</th>
        <th>Third name</th>
        <th>phoneNumber</th>
        <th>date of Birth</th>
        <th>comment</th>
    </tr>
    <!--<td th:utext="${personName}">..</td>-->

    <tr >
        <td th:utext="${person.id_person}">..</td>
        <td th:utext="${person.name}">..</td>
        <td th:utext="${person.surname}">..</td>
        <td th:utext="${person.thirdName}">..</td>
        <td th:utext="${person.phone_number_person}">..</td>
        <td th:utext="${person.date_of_birth}">..</td>
        <td th:utext="${person.comment}">..</td>
    </tr>

    <tr th:each="event : ${person.getEvents()}">
    <td th:utext="${event.getname_event()}">..</td>
</tr>
    <tr th:each="org : ${person.getOrganizations()}">
        <td th:utext="${org.getName_organization()}">..</td>
    </tr>
    <tr th:each="project : ${person.getProjects()}">
        <td th:utext="${project.getName_project()}">..</td>

    </tr>
</table>
<form name='f' th:object="${list}" method='POST'>
    <select >
        <option value="">Select event</option>
        <option th:each="test : ${list}"
                th:value="${test.getname_event()}"
                th:text="${test.getname_event()}">"${test.getname_event()}"</option>
    </select>
    <p><input type="submit" value="Добавить cобытие"></p>
</form>

控制器


@GetMapping("person/{id}")
    public String onePerson(@PathVariable("id") int id, Model model, Model model1){
        Person person = personService.findPerson(id);
        model.addAttribute("person", person);
        List<Event> event = eventService.eventList();
        model.addAttribute("list", event);

        return "onePerson";
    }

    @RequestMapping(value = "/person/{id}", method = RequestMethod.POST)
    public String addEventToPerson(@PathVariable("id") @ModelAttribute int id,Event event, Model model) {
        model.addAttribute("list", event);
        personService.findPerson(id).addEvent(event);
        return "onePerson";
    }

人员模型


@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "person_event",

            joinColumns = @JoinColumn(name = "id_person"),

            inverseJoinColumns = @JoinColumn(name = "id_event"))
    public Set<Event> events = new HashSet<>();

    public Set<Event> getEvents() {
        return events;
    }


    public void setEvents(Set<Event> events) {
        this.events = events;
    }
    public void addEvent(Event event){
        events.add(event);
    }

出现错误(第27行未涉及此问题,我确定问题出在这种形式或在控制器中)

2019-04-18 13:20:28.765 ERROR 80318 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "person.id_person" (template: "onePerson" - line 27, col 13)] with root cause

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id_person' cannot be found on null
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:209) ~[spring-expression-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:100) ~[spring-expression-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:50) ~[spring-expression-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:368) ~[spring-expression-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:89) ~[spring-expression-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:111) ~[spring-expression-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:334) ~[spring-expression-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:165) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.standard.processor.StandardUtextTagProcessor.doProcess(StandardUtextTagProcessor.java:87) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:592) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:354) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:187) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1325) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1069) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1008) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
    at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]

2 个答案:

答案 0 :(得分:0)

错误消息表明您正在尝试访问属性id_person,但是person对象在模板中具有空引用。您可以确定在控制器的onePerson方法中,personService.findPerson(id);返回什么吗?此服务返回的内容由model.addAttribute("person", person);放到模板中,因此那里似乎没有null。

更新

我假设您的personService下有一些存储库(数据库?)。检查它是否包含您要通过URL person/{id}传递的ID下的人。

此外,值得检查:将参数从@PathVariable("id") int id更改为@PathVariable("id") String id并执行Integer.parseInt(id),然后再将其传递给findById。我不确定Spring是否会自动执行该操作。

答案 1 :(得分:0)

我已经创建了一个支持类(Personconnect)来选择id,然后将该id添加到我的方法addEvent中。

控制器

@RequestMapping(value = "/person/{id}", method = RequestMethod.POST)
public String onePersonEvent(@PathVariable("id") int id, @ModelAttribute Person person,PersonConnect personcon, Model model) {
    model.addAttribute("person", person );
    model.addAttribute("personcon", personcon );
    Person person1 = personService.findPerson(id);
    person1.addEvent(eventService.findEvent(personcon.getEvent_Id()));
    person1.addOrganization(organizationService.findOrganization(personcon.getOrganization_Id()));
    person1.addProject(projectService.findProject(personcon.getProject_Id()));
    personService.updatePerson(person1);

    return "redirect:/person/" + id;
}

@GetMapping("/person/{id}/edit")
public String showUpdateForm(@PathVariable("id") int id, Model model) {
    Person person = personService.findPerson(id);
    List eventList = eventService.eventList(); //лист эвентов для выпадащего списка
    model.addAttribute("person", person);
    model.addAttribute("list", eventList);

    return "updatePerson";

HTML:

<form name='f' th:object="${personcon}" method='POST'>
            <table>
                <tr>
                    <td>Select event:</td>
                    <td>
                        <select th:field="*{event_Id}">
                            <option th:each="event : ${list}"
                                    th:value="${event.id_event}"
                                    th:text="${event.id_event}+' : '+${event.name_event}"></option>
                        </select>

                    </td>
                </tr>
                <tr>
                    <td>Select project:</td>
                    <td>
                        <select th:field="*{project_Id}">
                            <option th:each="project : ${projectlist}"
                                    th:value="${project.id_project}"
                                    th:text="${project.id_project}+' : '+${project.name_project}"></option>
                        </select>

                    </td>
                </tr>
                <tr>
                    <td>Select organization:</td>
                    <td>
                        <select th:field="*{organization_Id}">
                            <option th:each="org : ${orglist}"
                                    th:value="${org.id_organization}"
                                    th:text="${org.id_organization}+' : '+${org.name_organization}"></option>
                        </select>

                    </td>
                </tr>