Wicket 和 Spring 集成
我有一个检票口联系表单,并且我收到了表单对象。现在我需要将此对象传递给 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>
我的问题很简单。
- 我应该做什么才能让我 调用Spring的onSubmit方法 服务?
- 有人可以告诉我需要什么吗 修改我的 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.
- What should i do which can make my
onSubmit method call the Spring
Service? - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Wicket-Spring 集成展示了如何将 Spring Bean(例如您的 IContactService bean)注入 Wicket 页面的各种方法。
基本上,配置完组件注入器后,您最终会得到以下代码:
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:
mhaller 的
@SpringBean
答案 当然是有效并被许多人认为是最佳实践。但我更喜欢更标准的 Spring 方法,其中您的 Wicket 应用程序具有您需要的服务。现在在您的组件中,调用
当然有一些缺点,最大的一个是您无法在没有应用程序的情况下轻松测试这样的组件。
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.Now in your component, call
There are of course some drawbacks, the biggest one being that you can't easily test such a component without the application.