保存前将对象添加到页面的数据表中。春天和Thymeleaf

时间:2017-09-02 13:58:21

标签: java spring thymeleaf

我将离开JSF环境,在使用Spring mvc和Thymeleaf时我有些疑惑。如下例所示,我有一个与资费清单相关的帐户对象,我想要完成的是在我的对象帐户中的关税清单中添加正在注册的帐户的关税,因此,在保存对象时该帐户,费用通过瀑布记录。用spring和thymeleaf实现这个简单函数的最佳方法是什么。

帐户类

public class Account {

    private Integer accountNumber;
    private BigDecimal balance;

    @ManyToOne(targetEntity=Rate.class, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    @JoinColumn(name="RateCode", foreignKey= @ForeignKey(name = "fk_account_rate"))
    private List<Rate> rates;

    // get and sets
}

public class Rate {

    private Long rateCode;
    private String rateName;
    private BigDecimal value;


    // get and sets

}

控制器帐户

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired AccountRepository accountRepository;

    @GetMapping
    public ModelAndView new(Account account) {

        ModelAndView model = new ModelAndView("page/account/cadAccount");
        model.addObject("rate", new Rate());
        model.addObject(account);

        return model;
    }
}

页面帐户

<form th:object="${account}" method="POST" th:action="@{/account/save}">
    <gff:message/>

    <div class="box-body">
        <input type="hidden" th:field="*{accountCode}"/>
        <div class="row">

            <div class="col-md-12">
              <div class="nav-tabs-custom">
                <ul class="nav nav-tabs">
                  <li class="active"><a href="#tab_conta" data-toggle="tab">Account</a></li>
                  <li><a href="#tab_tarifas" data-toggle="tab">Rates</a></li>
                </ul>
                <div class="tab-content">
                  <div class="tab-pane active" id="tab_conta">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group" gff:classforerror="name">

                                <label>Account name</label> 

                                <input type="text" th:field="*{name}" class="form-control input-sm">
                            </div>
                        </div>

                        <div class="form-group col-md-4" gff:classforerror="balance">

                            <label>Balance</label>

                            <div class="input-group">
                                <span class="input-group-addon"><i class="fa fa-dollar"></i></span>
                                <input type="text" th:field="*{balance}" data-thousands="." data-decimal="," data-prefix="R$ " class="form-control currency input-sm">
                            </div>
                        </div>

                    </div>

                  </div>

                  <div class="tab-pane" id="tab_tarifas">
                    <div class="row">

                        <div class="col-md-5">
                            <div class="form-group">

                                <label>Rate Name</label> 

                                <input id="nomeTarifa" type="text" th:field="${rate.name}" class="form-control input-sm">
                            </div>
                        </div>

                        <div class="form-group col-md-3">

                            <label>Value</label>

                            <div class="input-group">
                                <span class="input-group-addon"><i class="fa fa-dollar"></i></span>
                                <input id="valorTarifaID" type="text" th:field="${rate.value}" data-thousands="." data-decimal="," data-prefix="R$ " class="form-control currency input-sm">
                            </div>
                        </div>  

                        <div class="form-group col-md-2">

                            <a class="btn btn-info pull-left" th:href="@{/account/add}">
                                <i class="fa fa-plus"></i>
                                Add
                            </a>

                        </div>

                        <div class="col-md-12">

                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>Name</th>
                                        <th>Value</th>
                                    </tr>
                                </thead>

                                <tbody>
                                  <tr>
                                    <td data-title="Name"></td>
                                    <td data-title="Value"></td>
                                  </tr>
                                </tbody>

                                <tfoot>
                                    <tr>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>

                    </div>
                  </div>
                </div>
              </div>
            </div>


        </div>
    </div>

    <div class="box-footer">
        <button id="salveButtonID" type="submit" class="btn btn-primary ajax">Salve</button>

        <a class="btn btn-default ajax" th:href="@{/}">Cancel</a>
    </div>

</form>

0 个答案:

没有答案