`
shazhifeng
  • 浏览: 126735 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Wicket里面使用Ajax的小例子

阅读更多

<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
      <h4>这里测试Ajax</h4>
      <span wicket:id="count"></span>
      <br/>
      <a wicket:id="ajaxLink">Click</a>
     
      <form wicket:id="ajaxForm">
          <span wicket:id="feedback"></span><br/>
          name:<input type="text" wicket:id="name"/>
          <input type="button" value="test" wicket:id="ajaxButton"/>
      </form>
     
      <form wicket:id="dropForm">
          <select wicket:id="first"></select>
          <select wicket:id="second"></select>
          <input type="submit" value="submit"/>
      </form>     
</body>
</html>

AjaxTest.java

public class AjaxTest extends WebPage{
    private int count;
    private String name;
   
    private List firstList = new ArrayList();
    private List secondList = new ArrayList();
    private String first;
    private String second;
   
    public AjaxTest(){
        //////////////////////测试计算器////////////////////////////
        final Label countLabel = new Label("count",new PropertyModel(this,"count"));
        //要使用Ajax,必须调用setOutputMarkupId方法输出MarkupId
        countLabel.setOutputMarkupId(true);
        add(countLabel);
       
        AjaxLink ajaxLink = new AjaxLink("ajaxLink"){
            @Override
            public void onClick(AjaxRequestTarget target) {
                count++;
                //一定要给AjaxRequestTarget添加子控件
                target.addComponent(countLabel);
            }           
        };
        add(ajaxLink);
       
       
        ///////////////////////测试验证表单提交/////////////////////
        Form ajaxForm = new Form("ajaxForm",new CompoundPropertyModel(this));
        ajaxForm.setOutputMarkupId(true);
        add(ajaxForm);
        final FeedbackPanel feedback = new FeedbackPanel("feedback");
        feedback.setOutputMarkupId(true);
        ajaxForm.add(feedback);
        TextField nameField = new TextField("name",new PropertyModel(this,"name"));
        //设置name的最少长度为4
        nameField.add(StringValidator.minimumLength(4));
        nameField.setOutputMarkupId(true);
        ajaxForm.add(nameField);
       
        //设置一个AjaxFormValidatingBehavior,验证ajaxForm表单,事件为onkeyup
        AjaxFormValidatingBehavior behavior = new AjaxFormValidatingBehavior(ajaxForm,"onkeyup"){
            @Override
            public void onError(AjaxRequestTarget target){
                //为AjaxRequestTarget添加子控件
                target.addComponent(feedback);
                //将错误信息输出到feedback
                super.onError(target);
            }
        };
        //每秒向服务器发出请求
        behavior.setThrottleDelay(Duration.ONE_SECOND);
        //nameField添加了一个AjaxFormValidatingBehavior监听器,事件为onkeyup
        nameField.add(behavior);
       
        AjaxSubmitButton ajaxButton = new AjaxSubmitButton("ajaxButton",ajaxForm){
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form form) {
                info("AjaxButton提交!");
            }           
        };
        ajaxForm.add(ajaxButton);      
        
        /////////////////////测试联动下拉菜单////////////////////
       
        firstList.add("1");firstList.add("2");firstList.add("3");
        Form dropForm = new Form("dropForm",new CompoundPropertyModel(this));
        dropForm.setOutputMarkupId(true);
        add(dropForm);
       
        final DropDownChoice firstDrop = new DropDownChoice("first",new PropertyModel(this,"firstList"));firstDrop.setRequired(true);
        firstDrop.setOutputMarkupId(true);
        final DropDownChoice secondDrop = new DropDownChoice("second",new PropertyModel(this,"secondList"));
        secondDrop.setOutputMarkupId(true);
       
        dropForm.add(firstDrop);
        dropForm.add(secondDrop);
       
        //为firstDrop添加AjaxFormComponentUpdatingBehavior监听,事件为onChange
        firstDrop.add(new AjaxFormComponentUpdatingBehavior("onChange"){
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                getSecondList().clear();
                int index = Integer.parseInt((String)firstDrop.getModelObject());
                for(int i=1;i<=4;i++){
                    getSecondList().add(String.valueOf(index*10+i));
                }
                //为AjaxRequestTarget添加子控件
                target.addComponent(secondDrop);
            }           
        });

    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List getSecondList() {
        return secondList;
    }

    public void setSecondList(List secondList) {
        this.secondList = secondList;
    }

    public List getFirstList() {
        return firstList;
    }

    public void setFirstList(List firstList) {
        this.firstList = firstList;
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }
}

分享到:
评论

相关推荐

    wicket的ajax范例(级联选择框)

    本文档通过一个具体的例子——级联选择框,来演示如何利用Wicket框架结合Ajax技术实现前端界面的动态更新。 #### 环境准备与基本配置 为了确保示例代码能够正常运行,首先需要搭建好开发环境: - **服务器**: ...

    wicket例子和开发指南

    4. AJAX应用:使用Wicket的AJAX功能实现动态更新。 5. URL和导航实例:演示如何设置和跳转URL。 6. 国际化示例:展示如何切换不同语言的界面。 7. 组件和模板的继承:使用父组件和模板进行代码复用的例子。 通过...

    wicket开发详解

    尽管Wicket有许多优点,但它也有一些不足之处,例如社区规模相对较小,第三方库支持不如一些更主流的框架丰富。 ##### 1.7 Wicket的前景 随着Web开发技术的不断进步,Wicket也在不断发展和完善。未来,我们可以...

    wicket文档

    Wicket是一种现代的、面向对象的Web框架,它的核心特性包括强大的组件模型、自动的数据绑定、内置的支持AJAX等功能。 **1.4 Wicket的特性** - **1.4.1 更加纯粹的Java和OO** Wicket完全采用Java语言编写,遵循...

    Wicket1.4开发手记(四)[借鉴].pdf

    本篇开发手记将聚焦于使用Wicket 1.4版本创建一个基于Ajax的级联选择框,这是一个常见的功能需求,例如在用户选择一个选项后,第二个下拉框的内容会根据前者的选取动态更新。 首先,我们需要理解整个开发过程的基本...

    wicket-js:wicket-js 使在 Wicket 组件和行为中编写和处理自定义 JavaScript 和 jQuery 变得容易

    它有助于将 Wicket Ajax Behavior 侦听器与实际客户端 UI 功能分开,同时保持与 Wicket 组件的耦合。 它提供了一个(基本的),允许您在 Java 中构建 JavaScript 和 jQuery 表达式。 例子 使用 wicket-js,您可以...

    wicekt开发指南

    Wicket开发指南是一份介绍Wicket框架使用的入门教材,其作者为王磊,以电子邮件wl_***为联系方式。该教材的版本不断更新,从*.**.**.**.60.7跨越到多个时间点,最后一次更新为2006年12月31日。由于文档扫描技术原因...

    Struts2 in action 中文版.pdf

    书中还涵盖了Struts2与各种其他技术的整合,例如与Hibernate进行ORM操作,与Spring框架的深度集成,以及如何与其他MVC框架如JSF、Wicket协同工作。这些内容对于理解Struts2在企业级应用中的地位和作用至关重要。 在...

Global site tag (gtag.js) - Google Analytics