使用Thymeleaf形式与复合对象(内部有2个对象)

时间:2015-01-23 10:45:19

标签: java spring-mvc thymeleaf

问题

我正在使用Thymeleaf模板引擎的Spring-boot,我在页面中有一个表单,它需要更新2个实体但它们具有相同的属性,所以我将2个实体混合成1个复杂对象,但是当请求时我的控制器,它发生了JSON解析错误,请帮我弄清楚发生了什么事。

用于调试的控制器

    @RequestMapping(value = "shop", method = RequestMethod.POST)
    public String createShop(@RequestBody String json , final Locale locale,
            Model model, HttpSession session) {

        ObjectMapper om = new ObjectMapper();
        try {
            om.readValue(json, CreateShopForm.class);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        model.addAttribute(H2S_LAYOUT_PAGE_TITLE, messageSource.getMessage("cms.common.sidebar.profile", null, locale));
        model.addAttribute(H2S_LAYOUT_PAGE_DESC, "建立商家資料完成");
        model.addAttribute(H2S_LAYOUT_CONTENT, "modules/admin/register_success");

        return "layouts/admin";
    }

复杂对象

public class CreateShopForm{
    private Employee employee;
    private Shop shop;
    public Employee getEmployee() {
        return employee;
    }
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
    public Shop getShop() {
        return shop;
    }
    public void setShop(Shop shop) {
        this.shop = shop;
    }
}

带有Thymeleaf标签的HTML

<form enctype='application/json' th:action="@{/admin/shop}" method="post" class="form-horizontal"
  role="form" id="shop-profile-form" th:object="${createShopForm}">

  <h2 class="header smaller lighter blue">公司資料:</h2>

<!--   <div class="form-group has-error"> -->
  <div class="form-group">
    <label class="col-sm-2 col-xs-12 control-label no-padding-right">公司登記名稱</label>
    <div class="col-sm-5 col-xs-12">
      <input type="text" class="col-xs-12"
        th:field="*{shop.registrationName}" />
    </div>
<!--     <div class="help-block col-sm-5 col-xs-12 col-sm-reset inline">
      欄位必填!</div> -->
  </div>

  <div class="form-group">
    <label class="col-sm-2 col-xs-12 control-label no-padding-right">公司統一編號</label>
    <div class="col-sm-5 col-xs-12">
      <input type="text" class="col-xs-12" th:field="*{employee.taxNumber}" />
    </div>
  </div>
</form>

异常

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'shop': was expecting ('true', 'false' or 'null')
 at [Source: shop.registrationName=&shop.taxNumber=&shop.owner=&shop.phoneNumberArea=&shop.phoneNumber=&shop.county.id=1&shop.address=&shop.twReceiptContact=&shop.twReceiptPhoneNumberArea=&shop.twReceiptPhoneNumber=&shop.twReceiptPhoneNumberExt=&shop.twReceiptCounty.id=1&shop.twReceiptAddress=&shop.billingEmail=&shop.bankCode=74&shop.bankAccount=&shop.bankImageUuid=&employee.displayName=&employee.PhoneNumberArea=&employee.PhoneNumber=&employee.PhoneNumberExt=&employee.mobileNumber=&employee.email=&employee.password=&shop.displayName=&shop.urlPathname=&_csrf=5692d99c-ea8a-446d-8f4b-ebb18a12ee7d; line: 1, column: 5]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2300)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1459)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:683)
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3105)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2161)
    at com.h2s.papak.admin.controller.AdminShopController$$EP2BYKWO.createShop(AdminShopController.java:90)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springsource.loaded.ri.ReloadedTypeInvoker$2.invoke(ReloadedTypeInvoker.java:122)

0 个答案:

没有答案