Wicket Guard #4

Simple wrapper for creating bookmarkable page links

Lets say you want to create a parametrizable bookmarkable link like this:

<a href="/cityPage?city=london">London</a>

Creating a dynamic parametrized bookmarkable link needs the following items in wicket:

  • a wicket:id for the anchor’s href
  • a key for the parameter
  • the value for the key
  • a wicket:id for the displaying the text of the link
  • the actual text itself

Here is a simple wrapper method that can do all in one stroke:

	/**
	 * Usage:
	 *   createBookmarkablePageLink("linkWicketId", Page.class, "paramKey", $param.value, "textWicketId", $text);
	 */
	public static BookmarkablePageLink createBookmarkablePageLink(String _wicketId, Class _pageClass, String _paramKey, String _paramValue, String _wicketHrefTextId, String _hrefText) {
		PageParameters param = new PageParameters();
		param.add(_paramKey, _paramValue);
		BookmarkablePageLink link = new BookmarkablePageLink(_wicketId, _pageClass, param);
		link.add(new Label(_wicketHrefTextId, _hrefText));
		
		return link;
	}

Here is how the call would look for the above html:

createBookmarkablePageLink("cityLinkWicketId", CityPage.class "city", "london", "cityNameWicketId", "London");

And the corresponding wicket html:

<a wicket:id="cityLinkWicketId" href="#"><span wicket:id="cityNameWicketId">$someCityName</span></a>
Advertisements

Wicket Guard #3

ListView vs DataView/ListProvider

One thing to remember when using DataView instead of ListView is that the DataView does not have a setListProvider(), neither the ListProvider has a setList(). In other words, once these objects are initialized with the list, there is no way to set another new List object. ListView has a setList() which can be called for eg during a onSubmit() to update the list.

The correct way to handle that is, on refreshing, clear out the old items from the list and add new items. When the page/form is displayed again, the new values will be used.

For eg

  List list = new ArrayList();
  //list.add(...)
  DataView dv = new DataView("dataView", new ListProvider("listProvider", list));

  protected void onSubmit() {
    list.clear();
    list.add(...);
    //thats it, no need, (rather cannot) set the list to ListProvider again
  }

So when querying list from say Spring’s JdbcTemplate queryList() methods, be aware that it returns a new List object, which the ListProvider will not pickup. You can simply do a list.clear(), followed by list.addAll(newList);

Wicket Guard #1

Wicket Guard #1 – Cannot use wicket:id elements in substituted (replaced) panels.

I was using a panel html, but wanted to have the flexibility of designing it in main page also. So I copy the panel’s contents in the Main page too just for designing. This panel content should eventually be replaced by Wicket at runtime.

Panel.html

<wicket:panel>
  <div id="panelContainer">
    <ul>
      <li wicket:id="element">...</li>
    </ul>
  </div>
</wicket:panel>

Main.html

<div id="panel" wicket:id="panel">
    <ul>
      <li wicket:id="element">...</li>
    </ul>
</div>

When I ran the above code, wicket complained that <div> element does not have a close tag. Wicket obviously replaces the contains within div#panel in the Main.html with the Panel.html contents. Then why would it complain? After a bit of digging, found out that the Main.html also contains a not-so-obvious <li wicket:id=”element”>. Wicket does not blindly replace the panel within div#panel, instead it errors out if the element within that has a wicket:id.

Corrected Main.html

<div id="panel" wicket:id="panel">
    <ul>
      <li>...</li>
    </ul>
</div>

So now I have the ability to design Main.html and still have the panel in a separate file.

Enter-prise Java or Enter-the-dragon Java?

Java frameworks have become penny a dozen with no end in sight of any java developer not dissatisfied with any given frameworks. Every other framework claims it is better than the other one, because its APIs provide much flexibility. The truth is no framework provides a complete solution.

Debates on decisions on which framework to use has pretty much become part of a new enterprise project.  JSF is pretty heavyweight? Can Spring’s  xml configuration pretty soon get out of control ? Is mixing @Annotations, column names, field lengths, constraints, primary key, foriegn key, join columns in the POJO a really good approach ?  (One of these days we shall have an annotation to have all the data within the pojo.  Everyone wants Ajax behaviour now. Is Tapestry worth it or will Tapestry 6 be a complete rewrite yet again ? May be just stick to servlet and JSP ? How about EJB3 and reliving its nightmares ? Flex anyone ?

Blogs and Forums are full of debates about Spring vs Seam, JSF vs SpringFaces, Struts vs Tapestry, Yin vs Yang frameworks. There are people who adore JSF and there are who abhor JSF. I dont think its anybodys fault. It probably has to do more with how the brains are wired. Left brained guys like JSF, while the right brained guys like Spring. Or vice-versa.

Two of the technologies I have been tracking very actively recently are Wicket and Groovy.

When I start writing code using Wicket, Im reminded of my Swing days – oh how nice for a change it is to write ONLY in Java? Instead of all the lame scriptlets of two line Java codes thrusted into html (JSP), opening curly braces in header.jsp and closing it in catch.jsp. Or XMLs containing Java like code (like ActionScript). Or XMLs defining flows and subflows with hard-to-follow ELs. Im realizing that JSP is really an insult to a good programmer’s intelligence and XML is an insult to human capacity to understand languages. Is XML the “human readable format for machines” ? Duh?

There are other component frameworks like Wicket, but Wicket’s simple, easy to understand API really is cool and intuitive. No WTextField or WLabel .. its simply TextField or Label. That simplicity makes a lot of difference. It just wires to the brain quickly. No xmls or logic in the views. Whew! Models and Nesting models take a bit to understand, but it can reduce cyclomatic code complexity quickly. With Model 2 architecture, most of the OO stuff in Java has been relegated to extending MyServiceImpl from AbstractServiceImpl and a few inheritances here and there. Wicket changes all that. It lets the HTML itself be designed with inheritance, encapsulate components and hide unwanted details. 

Groovy meanwhile is what can be termed as the “Missing Java Functions”.

I hope Wicket grows quicker and Groovy’s feature-set start attracting more developers. After all programming is an art and it is all about enjoying the art.