如何在HTML中清理wicket标签?

时间:2014-04-03 20:20:54

标签: java html web-applications wicket wicket-6

我是Wicket的新手。在第一次使用wicket datatable之后,我实现了wicket:panel和wicket:容器被添加到HTML中。但是,我根本没有任何标记面板或容器。我需要一些帮助来确保HTML不包含不必要的wicket标记。感谢!!!

HTML代码

<!DOCTYPE html>
<html lang="en"
      xml:lang="en"
      xmlns:wicket="http://wicket.apache.org>
   <head>
      <title> Wicket test </title>
   </head>
   <body>
      <table cellspacing="0" wicket:id="table"></table>
   </body>
</html>

Java代码

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.model.Model;

public class TestPage extends WebPage {
    public TestPage() {
        final UserProvider userProvider = new UserProvider();
        List<IColumn<UserProvider.Contact, String>> columns = new ArrayList<IColumn<UserProvider.Contact, String>>(2);
        columns.add(new PropertyColumn<UserProvider.Contact, String>(new Model<String>("First Name"), "name.first", "name.first"));
        columns.add(new PropertyColumn<UserProvider.Contact, String>(new Model<String>("Last Name"), "name.last", "name.last"));
        DefaultDataTable<UserProvider.Contact, String> dataTable = new DefaultDataTable<UserProvider.Contact, String>("table", columns, userProvider, 10);
        add(dataTable);
    }
}

UserProvider的Java代码

import java.util.*;
import java.io.Serializable;

import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

public Class UserProvider extends SortableDataProvider {
    class SortableDataProviderComparator implements Comparator<Contact>, Serializable {
        public int compare(final Contact o1, final Contact o2) {
            PropertyModel<Comparable> model1 = new PropertyModel<Comparable>(o1, (String) getSort().getProperty());
            PropertyModel<Comparable> model2 = new PropertyModel<Comparable>(o2, (String) getSort().getProperty());
            int result = model1.getObject().compareTo(model2.getObject());
            if (!getSort().isAscending()) {
                result = -result;
            }
            return result;
        }
    }

    private List<Contact> list = new ArrayList<Contact>();
    private SortableDataProviderComparator comparator = new SortableDataProviderComparator();

    public UserProvder() {
        setSort("name.first", SortOrder.ASCENDING);
        list.add(new Contact(new Name("Abbie", "Zed")));
        list.add(new Contact(new Name("Benny", "Yellen")));
        list.add(new Contact(new Name("Charles", "Wukong")));
        list.add(new Contact(new Name("Dennis", "Rose")));
        list.add(new Contact(new Name("Elaine", "Poppy")));
        list.add(new Contact(new Name("Peter", "Jax")));
    }

    public Iterator<Contact> model(long first, long count) {
        List<Contact> newList = new ArrayList<Contact>(list);
        Collections.sort(newList, comparator);
        return newList.subList((int) first, (int) (first + count)).iterator();
    }

    public IModel<Contact> model(final Object object) {
        return new AbstractReadOnlyModel<Contact>() {
            @Override
            public Contact getObject() {
                return (Contact) object;
            }
        };
    }

    public long size() {
        return list.size();
    }

    class Contact implements Serializable {
        private final Name name;
        public Contact(final Name name) {
            this.name = name;
        }
        public Name getName() {
            return name;
        }
    }

    class Name implements Serializable {
        private String firstName;
        private String lastName;
        public Name(final String fName, final String lName) {
            firstName = fName;
            lastName = lName;
        }
        public String getFirst() {
            return firstName;
        }
        public String getLast() {
            return lastName;
        }
        public setFirst(final String firstName) {
            this.firstName = firstName;
        }
        public setLast(final String lastName) {
            this.lastName = lastName;
        }
    }
}

上面的Java代码来自一些在线示例。从代码,wicket:panel和wicket:容器没有被使用;但是,一旦部署了这个webapp。客户端接收带有unsed wicket标签的HTML页面。

1 个答案:

答案 0 :(得分:4)

在Application类的init()方法中,调用

getMarkupSettings().setStripWicketTags(false);

您还可以使用setStripComments()从结果HTML中删除评论,并使用setCompressWhitespace()

压缩空白区域

参考:http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/settings/IMarkupSettings.html