Search code examples
javaeclipseeclipse-lunawicket-6

Why is there an error when using containsKey() and getString() on type PageParameters?


I am trying out my hand at the Wicket framework for Java, and starting out by building a simple login application. There are two pages, namely, login.java (and .html), which contains the login form, and NextPage.java (and html) which just displays the login and password that were entered in the login page. To achieve the passing of variables from the page, I am utilizing the PageParameters class provided by wicket.

login.java:

package com.myassignment;

import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.mapper.parameter.PageParameters;

public class login extends WebPage {

    private Form form;
    private TextField userIDField;
    private PasswordTextField passwordField;


    public login(){

        userIDField = new TextField("user_id", new Model(""));
        passwordField = new PasswordTextField("password", new Model(""));

        userIDField.add(new TextValidator());
        passwordField.add(new TextValidator());
        userIDField.setRequired(true);
        passwordField.setRequired(true);

        Form form = new Form("login_form") {

            @Override
            protected void onSubmit() {
                String USRNAME = login.this.getUsername();
                String PWD = login.this.getPassword();
                System.out.println("You entered USER_ID: "+ USRNAME +" and PASSWORD: " + PWD);
                PageParameters para = new PageParameters();
                para.add("username", USRNAME);
                para.add("password", PWD);
                setResponsePage(NextPage.class, para);
            }
        };

        form.add(userIDField);
        form.add(passwordField);
        add(form);
    }

    protected String getUsername() {
        return userIDField.getDefaultModelObjectAsString();
    }
    protected String getPassword() {
         return passwordField.getModelObject();
    }
}

NextPage.java:

package com.myassignment;

import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.WebPage;


public class NextPage extends WebPage {

    private Label unameLabel;
    private Label passwordLabel;

    public NextPage(PageParameters para) {

        System.out.println(para);

        String uname = ""+para;
        String password = ""+para;

        if(para.containsKey("uname")){
            uname = para.getString("uname");
        }

        unameLabel = new Label("username_label", uname);
        passwordLabel = new Label("password_label", password);
        add(unameLabel);
        add(passwordLabel);

    }

}

However, there is an error in the code, The method containsKey(String) is undefined for the type PageParameters and The method getString(String) is undefined for the type PageParameters that is not supposed to exist, according to tutorials on the web, as well as the documentation in apache.wicket.org, which clearly mentions containsKey as an inherited method of PageParameters. I've been trying to look for a solution for hours now, but have not reached anywhere yet. I a rookie in Java, and absolutely new to Wicket.

NOTE: When i remove the containsKey() and getString() code portions, then it successfully prints the username and password to the system console upon clicking submit.

I am using Apache Wicket 6.18, jdk 1.7, and Eclipse Juno IDE.


Solution

  • Since containsKey() and getString() are no longer present in the Wicket 6.x documentation (they were there in 1.4x), hence getNamedKeys().contains() and get() worked, and should be the correct way, I am guessing.

    The following is the code fragment for the error portion, now:

    //previous code...
    
        if(para.getNamedKeys().contains("username"))
        {
            uname = para.get("username").toString();
        }
    
    //next code....
    

    Credits to user JB Nizet for the input and helping me find the answer.