📜 ⬆️ ⬇️

Getting Started with Netbeans and Wicket

Good day. Recently I had to use a Java framework called Wicket . On the great Habré I was looking for info about her, but it turned out to be too little and decided to share. So let's go:

After moving to NetBeans a couple of years ago, I’m not particularly inspired by the command-line games when creating web applications, so when you need to use (test, test, watch, etc.) some framework, first of all I am looking for a Plugin for NetBeans, and only then, in case of failure, I turn to alternative (for me) paths. In the case of Wicket, everything turned out well. Although there is an IDE Plugins section on the official website , there are even NetBeans on the list, but the links that I found there did not really help to find the plugin. The next step was to search for available NetBeans plugins, but failure was waiting for me there too. The final attempt is to search the NetBeans site in the Plugins section. I entered the first letters in the Name search field above the plug-in table and found - Wicket 1.4 Support (This plugin adds wicket support for NetBeans IDE 7.0.). I downloaded it and installed it (Tools -> Plugins -> Downloaded -> Add Plugins -> Install). You can read more about this step in the FAQ on the NetBeans website.
After installing the plugin, NetBeans prompts you to restart. Next, we need to create a new web project, and when selecting platforms, select Wicked 1.4.10, and go further ... (Java Web -> Web-Application -> -> -> Wicked 1.4.10 (tick) -> ->) . So NetBeans generated us a project with Hello World. Now let's deal with the project that is waiting for us:

image

Do not get lost when, running the application, you will see the result, but you will not see the files in the folder of the web page. But in the com.myapp.wicket package there are the following files:
')
Application.java
BasePage.html
BasePage.java
FooterPanel.html
FooterPanel. java
HeaderPanel.html
HeaderPanel.java
HomePage.html
HomePage.java


As you have noticed, all the files are in pairs, Application.java alone is indecently distinguished among the pairs of html-java files. It contains information about which file (page) to launch first. You can also register certain configs, but this is outside the scope of this article.

image

As you can see, the “configurator” returns the HomePage class as a homepage. Before we consider this file a few words about the structure.

image

In this diagram, the WebPage and Panel classes are native Wicket's, and the rest are created by NetBeans. Each of the generated classes has a pair with the same name, but with the html extension. In this example, the BasePage.html file defines the general structure of the page, and BasePage.java defines the functionality and the data that is placed. With the help of DOM, Wicket finds the necessary elements from HTML files and replaces them with what you write in the corresponding java files. This is easily explained with the help of the figure in which are shown next to HomePage.java (left) and HomePage.html (right):

image

As can be seen from the figure, a new Label is added to our homepage. It takes two parameters, where the first is the keyword, by which Wicket determine where to insert the Label, and the second is the actual content. Analysis of other pages and panels can be done in a similar way, I leave it to the reader to do it myself, and in the meantime I will show you how to create a simple form using this wonderful framework. To do this, you first need to choose where to create our form. Let it be BasePage.java and BasePage.html . First create an HTML form.

<form wicket:id="forma">
<input type="text" wicket:id="login" />
<input type="password" wicket:id="password" />
/>




With this uncomplicated code, we have created the simplest form of authorization.
Now let's move on to the most interesting - to the BasePage.java file. Everything is a little more complicated here. The algorithm is as follows:
We create TextField and PasswordTextField objects to link them to page elements. Next, we create a class that will be inherited from the Form class, it will in turn be associated with our form, in the same class we override the onSubmit () method, which is responsible for what happens when the user clicks on the submit button. Then it remains only to add TextField and PasswordTextField objects to the form and add the form itself to the page. As you can see, everything is logical, and there is nothing supernatural. Below is the code from the BasePage.java page.

public abstract class BasePage extends WebPage {

TextField login;
PasswordTextField password;

public BasePage() {
super();
add(new HeaderPanel("headerpanel", "Welcome To Wicket"));
add(new FooterPanel("footerpanel", "Powered by Wicket and the NetBeans Wicket Plugin"));
add(new footer("footer"));

login = new TextField("login", new Model(""));
password = new PasswordTextField("password", new Model(""));
Form form = new LoginForm("forma");
form.add(login);
form.add(password);
add(form);

}

class LoginForm extends Form{
public LoginForm(String id)
{
super(id);
}

public void onSubmit()
{
System.out.println("Login:"+login.getModelObject()+" -- Password:"+password.getModelObject());
}
}
}


That's all. Now when you click on the button, in the console you can see the entered username and password. All success and happiness.

Source: https://habr.com/ru/post/127047/


All Articles