1

我正在尝试使用 Java 的 Wicket 框架,并从构建一个简单的登录应用程序开始。有两个页面,即包含登录表单的 login.java(和 .html)和仅显示在登录页面中输入的登录名和密码的 NextPage.java(和 html)。为了实现从页面传递变量,我使用了 wicket 提供的 PageParameters 类。

登录.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.

4

2 回答 2

2

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.

于 2015-01-06T09:12:59.077 回答
0

Use the new API instead: PageParameters#get(String).

于 2015-01-05T18:58:21.500 回答