You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Mike Mander <wi...@gmx.de> on 2011/08/01 17:35:50 UTC
Why isn't my stateless form recreated from page parameters automatically?
Hi,
i would like to build up a stateless page for collecting an itemId and
an amount from my user.
The website should act as a direct shopping formular. The user only
should insert the data
and on form submission the itemId is checked for existence.
A helper button should clear the field values from the appropriate row.
All has to work
without any javascript.
My intention was that i create the stateless page, present it to the
user, collect the data,
submit the form, clear the row data and present the stateless page again
to the user
with data in row cleared. But i don't get it to work.
My question: If i post the form an page is coming back everything is
recreated and the values
from the page parameters are not deployed to appropriate components
automatically.
Do i have to do this by hand? Or is there a way?
I use wicket 1.4.17.
Thanks for helping me
Mike
<code>
HomePage.java
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.devutils.stateless.StatelessComponent;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.PropertyModel;
@StatelessComponent
public class HomePage extends WebPage {
public HomePage(final PageParameters parameters) {
super(parameters);
final StatelessForm<?> form;
add(form = (StatelessForm<?>) new StatelessForm<Void>("form"));
form.add(new ListView<ItemModel>("item", items()) {
@Override
protected void populateItem(final ListItem<ItemModel> item) {
item.add(new TextField<String>("itemId", new
PropertyModel<String>(item.getModel(), "itemId")));
item.add(new TextField<Integer>("amount", new
PropertyModel<Integer>(item.getModel(), "amount")));
item.add(new Button("clearContent") {
@Override
public void onSubmit() {
item.getModelObject().clear();
}
});
}
}.setReuseItems(true));
}
private List<ItemModel> items() {
return Arrays.asList(new ItemModel(0), new ItemModel(1));
}
}
</code>
ItemModel.java
<code>
import org.apache.wicket.util.string.Strings;
public class ItemModel {
private String _itemId = null;
private int _amount = 1;
private int _pos = 0;
public ItemModel(int pos) {
_pos = pos;
System.out.println(this);
}
public ItemModel(String key) {
if (!Strings.isEmpty(key)) {
String[] parts = key.split("-");
setPos(Integer.valueOf(parts[0]).intValue());
setItemId(String.valueOf(parts[1]));
setAmount(Integer.valueOf(parts[2]).intValue());
}
}
public int getAmount() {
return _amount;
}
public void setAmount(int amount) {
_amount = amount;
}
public String getItemId() {
return _itemId;
}
public void setItemId(String itemId) {
_itemId = itemId;
}
public int getPos() {
return _pos;
}
public void setPos(int pos) {
_pos = pos;
}
public String createKey() {
StringBuilder key = new
StringBuilder().append(getPos()).append("-").append(getItemId()).append("-").append(getAmount());
return key.toString();
}
public void clear() {
setItemId(null);
setAmount(1);
}
}
</code>
HomePage.html
<code>
<html
xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd"
>
<head>
<title>Wicket Quickstart Archetype Homepage</title>
</head>
<body>
<strong>Wicket Quickstart Archetype Homepage</strong>
<br/><br/>
<a href="/">Home</a>
<form wicket:id="form">
<table>
<tr>
<td>Item</td>
<td>Amount</td>
<td>Clear content</td>
</tr>
<tr wicket:id="item">
<td><input wicket:id="itemId" /></td>
<td><input wicket:id="amount" /></td>
<td><input type="submit" wicket:id="clearContent" /></td>
</tr>
</table>
</form>
</body>
</html>
</code>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Why isn't my stateless form recreated from page parameters automatically?
Posted by Joe Fawzy <jo...@gmail.com>.
Hi
it seems that
item.add(new TextField<String>("itemId", new
PropertyModel<String>(item.**getModel(),
"itemId")));
should be
item.add(new TextField<String>("itemId", new
PropertyModel<String>(item.**getModelObject(),
"itemId")));
Joe
On Mon, Aug 1, 2011 at 6:35 PM, Mike Mander <wi...@gmx.de> wrote:
> Hi,
>
> i would like to build up a stateless page for collecting an itemId and an
> amount from my user.
> The website should act as a direct shopping formular. The user only should
> insert the data
> and on form submission the itemId is checked for existence.
> A helper button should clear the field values from the appropriate row. All
> has to work
> without any javascript.
>
> My intention was that i create the stateless page, present it to the user,
> collect the data,
> submit the form, clear the row data and present the stateless page again to
> the user
> with data in row cleared. But i don't get it to work.
>
> My question: If i post the form an page is coming back everything is
> recreated and the values
> from the page parameters are not deployed to appropriate components
> automatically.
> Do i have to do this by hand? Or is there a way?
>
> I use wicket 1.4.17.
>
> Thanks for helping me
> Mike
>
> <code>
> HomePage.java
> import java.util.Arrays;
> import java.util.List;
>
> import org.apache.wicket.**PageParameters;
> import org.apache.wicket.devutils.**stateless.StatelessComponent;
> import org.apache.wicket.markup.html.**WebPage;
> import org.apache.wicket.markup.html.**form.Button;
> import org.apache.wicket.markup.html.**form.StatelessForm;
> import org.apache.wicket.markup.html.**form.TextField;
> import org.apache.wicket.markup.html.**list.ListItem;
> import org.apache.wicket.markup.html.**list.ListView;
> import org.apache.wicket.model.**PropertyModel;
>
> @StatelessComponent
> public class HomePage extends WebPage {
>
> public HomePage(final PageParameters parameters) {
> super(parameters);
> final StatelessForm<?> form;
> add(form = (StatelessForm<?>) new StatelessForm<Void>("form"));
> form.add(new ListView<ItemModel>("item", items()) {
>
> @Override
> protected void populateItem(final ListItem<ItemModel> item) {
> item.add(new TextField<String>("itemId", new
> PropertyModel<String>(item.**getModel(), "itemId")));
> item.add(new TextField<Integer>("amount", new
> PropertyModel<Integer>(item.**getModel(), "amount")));
> item.add(new Button("clearContent") {
> @Override
> public void onSubmit() {
> item.getModelObject().clear();
> }
> });
> }
> }.setReuseItems(true));
> }
>
> private List<ItemModel> items() {
> return Arrays.asList(new ItemModel(0), new ItemModel(1));
> }
> }
> </code>
>
> ItemModel.java
> <code>
> import org.apache.wicket.util.string.**Strings;
>
> public class ItemModel {
>
> private String _itemId = null;
> private int _amount = 1;
> private int _pos = 0;
>
> public ItemModel(int pos) {
> _pos = pos;
> System.out.println(this);
> }
>
> public ItemModel(String key) {
> if (!Strings.isEmpty(key)) {
> String[] parts = key.split("-");
> setPos(Integer.valueOf(parts[**0]).intValue());
> setItemId(String.valueOf(**parts[1]));
> setAmount(Integer.valueOf(**parts[2]).intValue());
> }
> }
>
> public int getAmount() {
> return _amount;
> }
>
> public void setAmount(int amount) {
> _amount = amount;
> }
>
> public String getItemId() {
> return _itemId;
> }
>
> public void setItemId(String itemId) {
> _itemId = itemId;
> }
>
> public int getPos() {
> return _pos;
> }
>
> public void setPos(int pos) {
> _pos = pos;
> }
>
> public String createKey() {
> StringBuilder key = new StringBuilder().append(getPos(**
> )).append("-").append(**getItemId()).append("-").**append(getAmount());
> return key.toString();
> }
>
> public void clear() {
> setItemId(null);
> setAmount(1);
> }
> }
> </code>
>
> HomePage.html
> <code>
> <html xmlns:wicket="http://wicket.**apache.org/dtds.data/wicket-**
> xhtml1.4-strict.dtd<http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd>"
> >
> <head>
> <title>Wicket Quickstart Archetype Homepage</title>
> </head>
> <body>
> <strong>Wicket Quickstart Archetype Homepage</strong>
> <br/><br/>
> <a href="/">Home</a>
> <form wicket:id="form">
> <table>
> <tr>
> <td>Item</td>
> <td>Amount</td>
> <td>Clear content</td>
> </tr>
> <tr wicket:id="item">
> <td><input wicket:id="itemId" /></td>
> <td><input wicket:id="amount" /></td>
> <td><input type="submit" wicket:id="clearContent" /></td>
> </tr>
> </table>
> </form>
> </body>
> </html>
> </code>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
> For additional commands, e-mail: users-help@wicket.apache.org
>
>