Java ApacheWicket:按下按钮后更新标签的值

Java ApacheWicket:按下按钮后更新标签的值,java,forms,wicket,wicket-1.5,wicket-6,Java,Forms,Wicket,Wicket 1.5,Wicket 6,我已经开始学习ApacheWicket web框架,在完成一项任务时遇到了一些困难 如果你能从附件中看到,我去了 尝试此基本输入表单功能 我的问题是:最初我不想在标签字段中显示任何内容,但一旦单击设置消息按钮,我就想更新标签值。我该怎么做?谢谢 更新: echo.java中的代码如下所示。PropertyModel随附标签,用于更新值 /* * Licensed to the Apache Software Foundation (ASF) under one or more * cont

我已经开始学习ApacheWicket web框架,在完成一项任务时遇到了一些困难

如果你能从附件中看到,我去了

尝试此基本输入表单功能

我的问题是:最初我不想在标签字段中显示任何内容,但一旦单击设置消息按钮,我就想更新标签值。我该怎么做?谢谢

更新:

echo.java中的代码如下所示。PropertyModel随附标签,用于更新值

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.examples.echo;

import org.apache.wicket.examples.WicketExamplePage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;


/**
 * The simplest form application possible. Just prints any user input to a label.
 * 
 * @author Eelco Hillenius
 */
public class Echo extends WicketExamplePage
{
    private String message = "[type your message to the world here]";

    /**
     * Constructor.
     */
    public Echo()
    {
        // This model references the page's message property and is
        // shared by the label and form component
        PropertyModel<String> messageModel = new PropertyModel<String>(this, "message");

        // The label displays the currently set message
        add(new Label("msg", messageModel));

        // Add a form to change the message. We don't need to do anything
        // else with this form as the shared model is automatically updated
        // on form submits
        Form<?> form = new Form("form");
        form.add(new TextField<String>("msgInput", messageModel));
        add(form);
    }

    /**
     * @return the message
     */
    public String getMessage()
    {
        return message;
    }

    /**
     * @param message
     *            the message to set
     */
    public void setMessage(String message)
    {
        this.message = message;
    }
}
/*
*根据一个或多个许可证颁发给Apache软件基金会(ASF)
*贡献者许可协议。请参阅随附的通知文件
*本作品提供了有关版权所有权的更多信息。
*ASF根据Apache许可证2.0版将此文件许可给您
*(以下简称“许可证”);除非符合以下要求,否则不得使用此文件
*执照。您可以通过以下方式获得许可证副本:
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
包org.apache.wicket.examples.echo;
导入org.apache.wicket.examples.WicketExamplePage;
导入org.apache.wicket.markup.html.basic.Label;
导入org.apache.wicket.markup.html.form.form;
导入org.apache.wicket.markup.html.form.TextField;
导入org.apache.wicket.model.PropertyModel;
/**
*最简单的应用程序形式。只需将任何用户输入打印到标签。
* 
*@作者Eelco Hillenius
*/
公共类Echo扩展WicketExamplePage
{
private String message=“[在此处向世界键入您的消息]”;
/**
*构造器。
*/
公共回声()
{
//此模型引用页面的消息属性,并且
//由标签和表单组件共享
PropertyModel messageModel=新的PropertyModel(此“消息”);
//标签显示当前设置的消息
添加(新标签(“msg”,messageModel));
//添加表单以更改邮件。我们不需要执行任何操作
//否则,此表单作为共享模型将自动更新
//按表格提交
表格=新表格(“表格”);
添加(新的文本字段(“msgInput”,messageModel));
添加(表格);
}
/**
*@回信
*/
公共字符串getMessage()
{
返回消息;
}
/**
*@param消息
*要设置的消息
*/
公共无效设置消息(字符串消息)
{
this.message=消息;
}
}
html文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org">
<head>
    <title>Wicket Examples - echo</title>
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
    <span wicket:id="mainNavigation"/>

    <form wicket:id="form">
        <input type="text" wicket:id="msgInput" value="" size="50" />
        <input type="submit" value="set message" />
    </form>
    <span wicket:id="msg" id="msg">Message goes here</span>

</body>
</html>

Wicket示例-echo
信息就在这里
试试这个:

add(new Label("msg", messageModel)) {
  @Override public boolean isVisible() {
    return !messageModel.getObject().equals(message);
  }
};
文本字段需要知道何时应该呈现它。请注意,如果要通过Ajax使组件可见,则需要在
标签上设置
setOutputMarkupId(true)
setOutputMarkupPlaceholderTag(true)
,例如
AjaxSubmitButton

当执行比
字符串
比较更复杂的任务时,您应该从重写的
onConfigure
方法调用
setVisible
,如注释中所述<代码>isVisible
在渲染阶段可能会被多次调用,以避免计算。如下所示:

add(new Label("msg", messageModel)) {
  @Override protected void onConfigure() {
    super.onConfigure();
    setVisible(!messageModel.getObject().equals(message));
  }
};
不过,千万别忘了调用
super
方法


为了提高代码的可读性,我个人更喜欢覆盖
isVisible
方法,以完成像
String
比较这样的简单任务。JIT编译器通常会处理其余部分。

您目前有哪些代码?我还添加了代码示例,可能不仅仅是将标签创建为新标签,而是将其放入一个var中,并从label类调用一个方法。Ps只是猜测我从来没有使用过jsp。或者,如果你可以从jsp代码调用js函数,那么你可以使用javascript来实现它“最初”是什么意思?如果这意味着第一页请求没有任何消息,并在提交表单后显示消息,那么null作为初始值字符串message=null就足够了。由于性能开销,不建议直接重写方法isVisible()以根据算法确定可见性,该方法在组件渲染期间被多次调用。而是重写onConfigure()并调用super.onConfigure(),而不是设置可见性setVisible(!messageModel.getObject().equals(message));谢谢马丁和拉弗的建议。我通过在页面第一次加载时使标签组件不可见来解决这个问题。仅当按下按钮且输入文本包含某些值时才可见。