Wicket 和 Spring 集成

发布于 2024-10-09 23:35:45 字数 2395 浏览 4 评论 0原文

我有一个检票口联系表单,并且我收到了表单对象。现在我需要将此对象传递给 Spring 服务。

package com.mysticcoders.mysticpaste.web.pages;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import com.mysticcoders.mysticpaste.model.Contact;
import org.apache.wicket.model.CompoundPropertyModel;

import com.mysticcoders.mysticpaste.services.IContact;


public class FormPage extends WebPage
{

    private Contact contact;
    private IContact icontact;

    public FormPage()
    {
        // Add a FeedbackPanel for displaying our messages
        FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
        add(feedbackPanel);

        Form<Object> form = new Form<Object>("contactForm",
                new CompoundPropertyModel<Object>(contact))
        {
            private static final long serialVersionUID = 1L;

            protected void onSubmit(Contact contact)
            {               
                icontact.saveContact(contact);
            }
        };  
        form.add(new TextField<Object>("name"));
        form.add(new TextField<Object>("email"));
        form.add(new TextField<Object>("country"));
        form.add(new TextField<Object>("age"));

        add(form);
        // add a simple text field that uses Input's 'text' property. Nothing
        // can go wrong here

   }


}

我非常确定我们需要对应用程序上下文 xml 做一些事情,我可能需要在其中进行接线。

My Application-context.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">        

    <bean id="WicketApplication" class="com.mysticcoders.mysticpaste.web.pages.WicketApplication" />

</beans>

我的问题很简单。

  1. 我应该做什么才能让我 调用Spring的onSubmit方法 服务?
  2. 有人可以告诉我需要什么吗 修改我的 application-context.xml 这样一次 表单被提交,它联系 Spring 服务类。

I have a wicket contact form, and i receive the form object. Now i need to pass this object to Spring Service.

package com.mysticcoders.mysticpaste.web.pages;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import com.mysticcoders.mysticpaste.model.Contact;
import org.apache.wicket.model.CompoundPropertyModel;

import com.mysticcoders.mysticpaste.services.IContact;


public class FormPage extends WebPage
{

    private Contact contact;
    private IContact icontact;

    public FormPage()
    {
        // Add a FeedbackPanel for displaying our messages
        FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
        add(feedbackPanel);

        Form<Object> form = new Form<Object>("contactForm",
                new CompoundPropertyModel<Object>(contact))
        {
            private static final long serialVersionUID = 1L;

            protected void onSubmit(Contact contact)
            {               
                icontact.saveContact(contact);
            }
        };  
        form.add(new TextField<Object>("name"));
        form.add(new TextField<Object>("email"));
        form.add(new TextField<Object>("country"));
        form.add(new TextField<Object>("age"));

        add(form);
        // add a simple text field that uses Input's 'text' property. Nothing
        // can go wrong here

   }


}

I am pretty much sure that we need to do something with application-context xml where i may need to wire out.

My Application-context.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">        

    <bean id="WicketApplication" class="com.mysticcoders.mysticpaste.web.pages.WicketApplication" />

</beans>

My Question is simple.

  1. What should i do which can make my
    onSubmit method call the Spring
    Service?
  2. Could someone let me know what needs
    to modified in my
    Application-context.xml so that once
    the form gets submitted, it contacts
    the Spring Service class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷→糖丶微凉 2024-10-16 23:35:46

Wicket-Spring 集成展示了如何将 Spring Bean(例如您的 IContactService bean)注入 Wicket 页面的各种方法。

基本上,配置完组件注入器后,您最终会得到以下代码:

public class FormPage extends WebPage
{
   @SpringBean
   private IContact icontact;
   ...
     Form<Object> form = new Form<Object>("contactForm",
            new CompoundPropertyModel<Object>(contact))
    {
        private static final long serialVersionUID = 1L;

        protected void onSubmit(Contact contact)
        {               
            icontact.saveContact(contact);
        }
    };  

Wicket-Spring integration shows various ways on how to inject Spring Beans (e.g. your IContactService bean) into Wicket pages.

Basically, after configuration of the component injector, you end up with the following code:

public class FormPage extends WebPage
{
   @SpringBean
   private IContact icontact;
   ...
     Form<Object> form = new Form<Object>("contactForm",
            new CompoundPropertyModel<Object>(contact))
    {
        private static final long serialVersionUID = 1L;

        protected void onSubmit(Contact contact)
        {               
            icontact.saveContact(contact);
        }
    };  
枯寂 2024-10-16 23:35:46

mhaller 的 @SpringBean 答案 当然是有效并被许多人认为是最佳实践。但我更喜欢更标准的 Spring 方法,其中您的 Wicket 应用程序具有您需要的服务。

public class YourWicketApp extends WebApplication{
    public static YourWicketApp get(){
        return (YourWicketApp) Application.get();
    }
    private ServiceA serviceA;
    // getter and setter for serviceA here
}

现在在您的组件中,调用

YourWicketApp.get().getServiceA();

当然有一些缺点,最大的一个是您无法在没有应用程序的情况下轻松测试这样的组件。

The @SpringBean answer by mhaller is of course valid and considered a best practice by many. But I prefer a more standard Spring approach, where your Wicket Application has the services you need.

public class YourWicketApp extends WebApplication{
    public static YourWicketApp get(){
        return (YourWicketApp) Application.get();
    }
    private ServiceA serviceA;
    // getter and setter for serviceA here
}

Now in your component, call

YourWicketApp.get().getServiceA();

There are of course some drawbacks, the biggest one being that you can't easily test such a component without the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文