0

Assume I have the following entities in my application:

public class Payment {
    private Long id;
    private Service service;
    private User user;
    private BigDecimal amount;
}

public cass Service {
    private Long id;
    private String name;
    private BigDecimal minAmount;
    private BigDecimal maxAmount;
}

public class User {
    private Long id;
    private String login;
    private String password;
    private BigDecimal balance;
}

I need to create html form that will allow users to process payments (instances of Payment class). So I need to create Payment instance in my controller method. I know that I can add to controller method, for example, Service service argument, and it will be filled by values from form with the same names. But how can I get the filled Payment object? With filled Service and User objects? I need to somehow save the whole Service object in my server page? How? I use Thymeleaf, if it matters.

1
  • Can you state your question in a more clear way? Commented May 20, 2015 at 13:45

1 Answer 1

2

On they thymeleaf file, as long as you respect the field structure of your class, Spring should be smart enough to fill the different fields.

<form th:object="${payment}" th:action="@{/sendPayment}" method="post">
      <input type="text" th:field="*{id}"/>
      <input type="text" th:field="*{service.name}"/>
      <input type="text" th:field="*{user.id}"/>
      <button type="submit">Submit</button>
</form>

Then on your Controller you just pass the Payment object:

@RequestMapping(value = "/sendPayment", method = RequestMethod.POST)
public String processPayment(final Payment payment){
    doSomethingWithPayment(payment);
}
Sign up to request clarification or add additional context in comments.

Comments

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.