Wicket形式的{不必要的)标记

时间:2017-03-18 09:06:36

标签: java wicket

使用Wicket(版本7.5.0)生成简单的表单页面时,我获得了额外的标记,这似乎是不必要的(隐藏字段放置在带有内联CSS的<div>中):

<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
  <head>
    <meta charset="utf-8" />
    <title>Apache Wicket Quickstart</title>
    <link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:regular,bold' rel='stylesheet' type='text/css' />
    <link rel="stylesheet" href="mystyle.css" type="text/css" media="screen" title="Stylesheet"/>
  </head>
  <body>
    <form method="post" wicket:id="ItemForm" id="ItemForm1" action="./tf?1-1.IFormSubmitListener-ItemForm">
      <div style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden">
        <input type="hidden" name="ItemForm1_hf_0" id="ItemForm1_hf_0" />
      </div>
      <p>
        <label for="name">
          <span>Item name:</span>
        </label>
        <input type="text" name="p::name" wicket:id="name" value="">
      </p>
      <p>
        <label for="price">
          <span>Item price:</span>
        </label>
        <input type="text" name="price" wicket:id="price" value="0">
      </p>
      <section>
        <input type="submit" value="Submit">
      </section>
    </form>
  </body>
</html>

相关的Java类是:

// Package name and imports omitted    
public final class ItemFormPage extends WebPage {

      @EJB(name = "ejb/item")
      Item it;

      public ItemFormPage() {
        Form f = new Form("ItemForm") {
          @Override
          public void onSubmit() {
            setResponsePage(new ItemDisplay());
          }
        };

        f.setDefaultModel(new CompoundPropertyModel(it));
        f.add(new TextField("name"));
        f.add(new TextField("price"));
        add(f);
      }
    }

我是Wicket的新手,从代码中可以看出这一点。有没有办法避免产生上述看似不必要的标记?换句话说,我错过了什么或者我应该报告错误吗?

1 个答案:

答案 0 :(得分:3)

此隐藏input用于提交包含基于锚点的组件的表单,例如SubmitLink

例如,您有一个Form,并且您希望有两种方式提交它(使用不同的2个按钮):

Form<Void> form = new Form<Void>("form") {
    @Override
    protected void onSubmit() {
        // central form onSubmit
    }
};

SubmitLink submitter1 = new SubmitLink("submitter1") {
    @Override
    public void onSubmit() {
        System.out.println("submitter 1 called");
    }
};
form.add(submitter1);

SubmitLink submitter2 = new SubmitLink("submitter2") {
    @Override
    public void onSubmit() {
        System.out.println("submitter 2 called");
    }
};
form.add(submitter2);

当您点击两个提交者中的任何一个时,其名称将被添加到该输入中,Wicket将找到正确的SubmitLink组件并调用其onSubmit()