springboot 设置model.addAttribute(dto);HTML页面无法获取值

conttroller code

@Controller
@RequestMapping("/web/member")
public class MerchantWebController {

    @RequestMapping(value = "/update")
    public String update(Model model, Integer id) {
        Merchant merchant = new Merchant();
        merchant.setId(1);
        merchant.setMerchantAccount("2222");
        model.addAttribute(merchant); //直接设置dto
        model.addAttribute("id",id);  //设置单个值
        return "web/member/update";
    }
}

dto code

public class Merchant implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
	@TableId(value="id", type= IdType.AUTO)
	private Integer id;
	
    /**
     * 商家姓名
     */
    @TableField("merchant_name")
	private String merchantName;
	
    /**
     * 商家账号
     */
	@TableField("merchant_account")

	private String merchantAccount;
	
    /**
     * 商家密码
     */
	@TableField("merchant_password")

	private String merchantPassword;
	
    /**
     * 商家经营范围
     */
	@TableField("merchant_scope")

	private String merchantScope;
	
    /**
     * 商家的店铺名称
     */
	@TableField("merchant_shop_name")

	private String merchantshopname;
	


	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getMerchantName() {
		return merchantName;
	}

	public void setMerchantName(String merchantName) {
		this.merchantName = merchantName;
	}

	public String getMerchantAccount() {
		return merchantAccount;
	}

	public void setMerchantAccount(String merchantAccount) {
		this.merchantAccount = merchantAccount;
	}

	public String getMerchantPassword() {
		return merchantPassword;
	}

	public void setMerchantPassword(String merchantPassword) {
		this.merchantPassword = merchantPassword;
	}

	public String getMerchantScope() {
		return merchantScope;
	}

	public void setMerchantScope(String merchantScope) {
		this.merchantScope = merchantScope;
	}

	public String getMerchantshopname() {
		return merchantshopname;
	}

	public void setMerchantshopname(String merchantshopname) {
		this.merchantshopname = merchantshopname;
	}

	@Override
	public String toString() {
		return "Merchant{" +
			", id=" + id +
			", merchantName=" + merchantName +
			", merchantAccount=" + merchantAccount +
			", merchantPassword=" + merchantPassword +
			", merchantScope=" + merchantScope +
			", merchantshopname=" + merchantshopname +
			"}";
	}
}

html 接收例子

dto接收方式 注意加上merchant

         <input type="商家账号" class="form-control" id="merchantAccount"  name="merchantAccount" th:value="${merchant.merchantAccount}">

普通字段

<div class="form-group">
    <label >namelabel>
    <input th:value="${id}">
div>

附 html 循环语句

<h2>用户列表h2>

<div th:each="user:${userList}">
  <input type="hidden" name="userId" th:value="${user.userId}"/>
  用户姓名:<input type="text" name="password" th:value="${user.username}"/>
  登录密码:<input type="text" name="username" th:value="${user.password}"/>
div>

你可能感兴趣的:(spring-boot)