복붙노트

[SPRING] 어떻게 thymeleaf를 사용하여 두 가지 객체를 하나의 양식으로 전달할 수 있습니까?

SPRING

어떻게 thymeleaf를 사용하여 두 가지 객체를 하나의 양식으로 전달할 수 있습니까?

내 문제는 다음과 같습니다.

저는 하나의 형식으로 채울 두 가지 객체를 가졌습니다.

1 개체를 사용하면 newFoo.html에서 간단히 수행 할 수 있습니다.

<form th:object="${foo} th:action="@{/foo}" method="post">
    <input type="text" th:field="*{name}"/>
    <button type="submit">Go</button>
</form>

그리고 FooController에서 :

@RequestMapping(value = "/foo/new", method = RequestMethod.GET) 
public String newFoo(final Foo foo, Model model) { 
    return "newFoo"; 
} 

@RequestMapping(value = "/foo/new", method = RequestMethod.POST) 
public String saveFoo(final Foo foo, final BindingResult bindingResult, Model model) { 
    fooService.save(foo); 
    return "redirect:/foo/new"; 
} 

변수에 "상태"변수가있는 다른 객체 막대가 있다고 가정 해 봅시다. 동일한 양식 내에서 입력을 제출할 수 있도록 해당 오브젝트를 전달하려면 어떻게해야합니까?

처럼:

<form th:object="${foo} && ${bar}" th:action="@{/foo}" method="post">
    <input type="text" th:field="*{name}"/>
    <input type="text" th:field="*{status}"/>
    <button type="submit">Go</button>
</form>

지금까지 나는 필드 세트를 가지고 작업하려고했는데, 그 안에는 작동하지 않는 객체가 있고, 두 번째를 넣으려고했다.

내가 찾은 유일한 방법은 두 객체를 포함하는 다른 객체를 만들고 전달하는 것입니다. 그것은 잘 작동하지만, 나는 그런 종류의 대상을 만들 수 없다. (작동하더라도) 난센스 다.

물론, 객체는 Foo와 Bar만큼 간단하지 않습니다. 그렇지 않으면이 두 객체를 병합합니다. 하지만 그건 내가 할 수있는 것이 아닙니다.

형태로 사용하는 두 개의 객체를 전달할 수 있습니까?

이미 고마워.

해결법

  1. ==============================

    1.두 가지를 사용해야 할 필요는 없다고 생각합니다. th : ​​value를 사용하십시오.

    두 가지를 사용해야 할 필요는 없다고 생각합니다. th : ​​value를 사용하십시오.

    <form th:action="@{/foo}" method="post">
          <input type="text" th:value="${foo.name}" name="name"/>
          <input type="text" th:value="${bar.status}" name="status"/>
          <button type="submit">Go</button>
    </form>
    

    컨트롤러 측에서는 Spring이 매핑 기술을 사용하여 필드를 적절한 명령 객체, foo 또는 bar에 매핑하는 데 충분히 똑똑하다고 생각할 것입니다.

  2. ==============================

    2.div 태그를 사용하여 두 번째 객체에 대한 양식 입력을 둘러싸고 th : object ..... 컨트롤러가 그것을 처리하고 데이터베이스에 추가했습니다.

    div 태그를 사용하여 두 번째 객체에 대한 양식 입력을 둘러싸고 th : object ..... 컨트롤러가 그것을 처리하고 데이터베이스에 추가했습니다.

    <form method=post th:object="${object1}" >
       <div th:object="${object2}" >
    
          code......
    
       </div> 
       <input type="submit" />
    </form>
    
  3. from https://stackoverflow.com/questions/16122257/how-to-pass-two-objects-to-use-in-a-form-using-thymeleaf by cc-by-sa and MIT license