apache-wicket 学习笔记(一) maven依赖与集成spring
一) 官方网站发布的新消息wicket6.0已经发布了,上一个版本是1.5。可见官方直接升级了主版本号。
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-extensions</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-auth-roles</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-spring</artifactId>
<version>6.0.0</version>
</dependency>
spring嘛,在spring3.2还没有正式发布之前还是先用3.1或者2.5,个人觉得如果不用spring-mvc的话,2.5还是挺好用的。要加的依赖jar也只有一个。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
或者
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
二) 配置spring容器
在web.xml中用监听器,每个spring的web项目都要这么做。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-beans.xml</param-value>
</context-param>
三) wicket框架的核心组件是一个servlet
web.xml 被注释掉的部分在正式部署上线的时候一定要记得不注释符号去掉。
<servlet>
<servlet-name>Application</servlet-name>
<servlet-class>org.apache.wicket.protocol.http.WicketServlet</servlet-class>
<init-param>
<param-name>applicationFactoryClassName</param-name>
<param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
</init-param>
<init-param>
<param-name>applicationBean</param-name>
<param-value>wicketApplication</param-value>
</init-param>
<!--
<init-param>
<param-name>configuration</param-name>
<param-value>DEPLOYMENT</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
四) wicketApplication Bean 要加入代码。
package com.ztgame.webapp.wicket;
import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.springframework.stereotype.Component;
import com.ztgame.webapp.wicket.page.HelloWorldPage;
@Component("wicketApplication")
public class Application extends WebApplication {
@Override
protected void init() {
super.init();
super.getComponentInstantiationListeners().add(new SpringComponentInjector(this));
}
@Override
public Class<? extends Page> getHomePage() {
return HelloWorldPage.class;
}
}
五) 在Page对象中注入业务层的Bean
package com.ztgame.webapp.wicket.page;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.spring.injection.annot.SpringBean;
import com.ztgame.webapp.wicket.service.SomeService;
public class HelloWorldPage extends WebPage {
private static final long serialVersionUID = -4345240865727174450L;
@SpringBean
private SomeService someService;
public HelloWorldPage() {
ExternalLink externalLink = new ExternalLink("externalLink", "http://yingzhuo.iteye.com", someService.getString());
super.addOrReplace(externalLink);
}
}
比较奇怪的是wicket做注入既没有使用JSR250的标注或者JSR330的标注,而是用了自己定义的一个标注。
另外,需要注意的是wicket与spring集成以后不可以通过构造方法注入,只能通过field或者property注入。