dcsimg
December 2, 2016
Hot Topics:

Wicket: The First Steps

  • April 23, 2007
  • By Karthik Gurumurthy
  • Send Email »
  • More Articles »

Now that you are done specifying the init-param, the only thing you are left with is accessing the same and setting it on Wicket's ApplicationSettings object. Change the HelloWorldApplication class like this:

public class HelloWorldApplication extends WebApplication {

    public HelloWorldApplication(){
       String deploymentMode = getWicketServlet().getInitParameter("configuration");
       configure(deploymentMode);
    }

    public Class getHomePage() {
       return Login.class;
    }

}

Alas, Wicket doesn't seem to be too happy with the modifications that you made:

But you've got to appreciate the fact that it informs you of the corrective action that it expects you to take (see Listing 15).

Listing 15. HelloWorldApplication.java
public class HelloWorldApplication extends WebApplication {

   public void init(){
      String deploymentMode =
         getWicketServlet().getInitParameter(
                         Application.CONFIGURATION);

      configure(deploymentMode);
   }
   public HelloWorldApplication(){}

   public Class getHomePage() {
      return Login.class;
   }
}

Actually, you are not required to set the deployment mode in the init as in Listing 15. Just setting the servlet initialization parameter against the key configuration should be sufficient. Wicket takes care of setting the deployment mode internally.

Specifying the Configuration Parameter

Wicket looks for the presence of a system property called wicket.configuration first. If it doesn't find one, it looks for the value corresponding to a servlet initialization parameter named configuration. In the absence of the preceding settings, it looks for an identical servlet context parameter setting. If none of the preceding listed lookups succeed, Wicket configures the application in development mode by default. Note that the value for configuration has to be either development or deployment identified by fields wicket.Application.DEVELOPMENT and wicket.Application.DEPLOYMENT, respectively.

Instead of refreshing the same page on every request, you'll next provide a personalized greeting in the form of a Welcome page once the user has logged in.

Displaying the Welcome Page

Listing 16 represents a simple Welcome page that has a placeholder for displaying a personalized greeting.

Listing 16. Welcome.html
<html>
  <title>Welcome to Wicket Application</title>
  <body>
       Welcome To Wicket Mr <span wicket:id="message">Message goes here</span>
  </body>
</html>

Welcome.html has a span tag marked as a Wicket component. This corresponds to Wicket's Label component. The Welcome page provides a personalized greeting to the user and accordingly accepts the userId/name as the label content (see Listing 17).

Listing 17. Welcome.java
import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;

public class Welcome extends WebPage {

   private String userId;

   public Welcome(){
    add(new Label("message",new PropertyModel(this,"userId")));
   }

   public String getUserId() {
     return userId;
   }

   public void setUserId(String userId) {
     this.userId = userId;
   }
}

Rendering a different page in response to the user input is as simple as setting it as the response page as shown in Listing 18.

Listing 18. Login.java
public class Login extends WebPage {
  //..
 public Login(){
    form = new LoginForm("loginForm");
    //..
  }

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

    @Override
    public void onSubmit() {
        String userId = Login.this.getUserId();
        String password = Login.this.getPassword();

        /* Instantiate the result page and set it as the response page */
        Welcome welcomePage = new Welcome();
        welcomePage.setUserId(userId);
        setResponsePage(welcomePage);
    }
  }

}

You can directly access the Welcome page by typing the URL on the browser and passing in the value for userId as a page parameter. The only change required would be that the Welcome constructor needs to be modified to accept the page parameter being passed into it. You will add another constructor that accepts an argument of type PageParameters (see Listing 19).

Listing 19. Welcome Page That Accepts PageParameters in the Constructor
import wicket.PageParameters;
public class Welcome extends WebPage {
    //..
   public Welcome(){
    //..
   }

   public Welcome(PageParameters params){
     this();

     /*
     * PageParameters class has methods to get to the parameter value
     * when supplied with the key.
     */

     setUserid(params.getString("userId"));
    }
    //..
}

and the URL to access the same would be http://localhost:7000/wicket/helloworld?wicket:bookmarkablePage=:com.apress.wicketbook.forms.Welcome&userId=wicket.

Currently, you don't have any authentication built into your application and therefore any user ID/password combination is acceptable. Go ahead and enter the values and click the Login button. This will take you to a primitive-looking Welcome page, shown in Figure 5, that displays a personalized greeting. If you are looking to navigate to the other sample pages developed sometime back, one option is to access them directly by typing in the URL on the browser, and the other could be to get to them through HTML links. Let's try getting the latter to work.

Figure 5. Accessing the Welcome page through the URL passing in PageParameters

Bookmarkable Page

You must be curious about the parameter bookmarkablePage in the URL. Actually, there is nothing special that makes the page bookmarkable. Any page is considered bookmarkable if it has a public default constructor and/ or a public constructor with a PageParameters argument. A bookmarkable page URL can be cached by the browser and can be used to access the page at a later point in time, while a nonbookmarkable page cannot be accessed this way. A non-bookmarkable page URL makes sense only in the context it was generated. If the page wants to be bookmarkable and accept parameters off the URL, it needs to implement the Page(PageParameters params) constructor.





Page 5 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.


Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel