本教程演示了如何将Wicket与Spring框架集成 。
本文中的库:
- 小票v1.4.17
- 检票口弹簧v1.4.17
- Spring v3.0.5.RELEASE
1.项目结构
本教程的最终项目目录结构没有什么特别的,只是一个标准的Maven项目。
2.项目依赖性
获取Wicket和Spring依赖关系,要整合两者,您需要“ wicket-spring.jar ”。
<project ..>
<dependencies>
<!-- Wicket framework-->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket</artifactId>
<version>1.4.17</version>
</dependency>
<!-- Integrate Wicket with Spring -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-spring</artifactId>
<version>1.4.17</version>
</dependency>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<!-- slf4j-log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
</dependencies>
</project>
3.Spring Bean
创建一个Spring bean,使用@Service对其进行注释。
package com.mkyong.user;
public interface HelloService {
String getHelloWorldMsg();
}
package com.mkyong.user;
import org.springframework.stereotype.Service;
@Service
public class HelloServiceImpl implements HelloService {
public String getHelloWorldMsg() {
return "Spring : hello world";
}
}
4.注入弹簧容器
创建一个标准的Spring applicationContext.xml文件,启用自动组件扫描功能。
文件:applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.mkyong.user" />
</beans>
5.将Wicket与Spring集成
使用此“ addComponentInstantiationListener(new SpringComponentInjector(this));
重写Wicket应用程序init()方法addComponentInstantiationListener(new SpringComponentInjector(this));
”。
文件:Wicket应用程序类
package com.mkyong;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import com.mkyong.user.SimplePage;
public class WicketApplication extends WebApplication {
@Override
public Class<SimplePage> getHomePage() {
return SimplePage.class; // return default page
}
@Override
protected void init() {
super.init();
addComponentInstantiationListener(new SpringComponentInjector(this));
}
}
现在,您可以通过@SpringBean将Spring bean注入Wicket组件。
package com.mkyong.user;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.spring.injection.annot.SpringBean;
public class SimplePage extends WebPage {
@SpringBean
private HelloService helloService;
public SimplePage(final PageParameters parameters) {
add(new Label("msg", helloService.getHelloWorldMsg()));
}
}
<html>
<body>
<h1>Wicket + Spring integration example</h1>
<h2 wicket:id="msg"></h2>
</body>
</html>
6. web.xml
最后,让您的Web项目知道什么是Wicket和Spring。 在web.xml
声明。
档案:web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Wicket Web Application</display-name>
<filter>
<filter-name>wicket.wicketTest</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>com.mkyong.WicketApplication</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>wicket.wicketTest</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
7.演示
开始并访问– http:// localhost:8080 / WicketExamples / 。
一个简单的Wicket页面,消息从Spring返回。
下载它– Wicket-Spring-Integration-Example.zip (8KB)
参考文献
翻译自: https://mkyong.com/wicket/wicket-spring-integration-example/