2014-11-15 71 views
0

我很短的時間使用Spring,但是對於wicket我是新手。但我想嘗試一下這個框架。使用Java Conf配置Spring Wicket而不是xml

我想配置這兩個框架togheter。但我想避免使用xml配置文件。

我知道,檢票口可以這種方式進行配置,根據此:

http://wicket.apache.org/guide/guide/single.html#helloWorld_2 這:

https://github.com/bitstorm/Wicket-tutorial-examples/tree/master/SpringInjectionExample

但現在我得到的唯一錯誤:

lis 15, 2014 4:31:15 PM org.apache.catalina.core.StandardContext filterStart 
SEVERE: Exception starting filter com.derp.wicket.ProjectFilter 
java.lang.NoSuchMethodError: org.apache.wicket.protocol.http.WebApplication.setMetaData(Lorg/apache/wicket/MetaDataKey;Ljava/lang/Object;)Lorg/apache/wicket/Application; 
    at org.apache.wicket.spring.injection.annot.SpringComponentInjector.<init>(SpringComponentInjector.java:115) 
    at org.apache.wicket.spring.injection.annot.SpringComponentInjector.<init>(SpringComponentInjector.java:92) 
    at com.derp.wicket.WicketApplication.init(WicketApplication.java:52) 
    at org.apache.wicket.Application.initApplication(Application.java:823) 
    at org.apache.wicket.protocol.http.WicketFilter.init(WicketFilter.java:424) 
    at org.apache.wicket.protocol.http.WicketFilter.init(WicketFilter.java:351) 
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) 
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) 
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105) 
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4603) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5210) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714) 
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:581) 
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1686) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 

lis 15, 2014 4:31:15 PM org.apache.catalina.core.StandardContext startInternal 
SEVERE: Error filterStart 
lis 15, 2014 4:31:15 PM org.apache.catalina.core.StandardContext startInternal 

Here my confs:

package com.derp.common.init; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration.Dynamic; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.ContextLoaderListener; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.filter.HiddenHttpMethodFilter; 
import org.springframework.web.servlet.DispatcherServlet; 

public class Initializer implements WebApplicationInitializer { 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 
     ctx.register(WebAppConfig.class); 
     servletContext.addListener(new ContextLoaderListener(ctx)); 

     ctx.setServletContext(servletContext); 

     Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); 
     servlet.addMapping("/"); 
     servlet.setLoadOnStartup(1); 
     // Allow to use Put and Delete method for REST architecture 
     registerHiddenFieldFilter(servletContext); 
    } 

    private void registerHiddenFieldFilter(ServletContext aContext) { 
     aContext.addFilter("hiddenHttpMethodFilter", new HiddenHttpMethodFilter()).addMappingForUrlPatterns(null ,true, "/*"); 
    } 
} 


package com.derp.common.init; 

import java.util.Properties; 

import javax.annotation.Resource; 
import javax.sql.DataSource; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.core.env.Environment; 
import org.springframework.jdbc.datasource.DriverManagerDataSource; 
import org.springframework.orm.hibernate4.HibernateTransactionManager; 
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.view.JstlView; 
import org.springframework.web.servlet.view.UrlBasedViewResolver; 

@Configuration 
@ComponentScan("com.derp") 
@EnableWebMvc 
@EnableTransactionManagement 
@PropertySource("classpath:application.properties") 
public class WebAppConfig { 

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver"; 
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password"; 
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url"; 
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username"; 

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect"; 
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql"; 
    private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO = "hibernate.hbm2ddl.auto"; 
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_SERVICES = "services.entitymanager.packages.to.scan"; 
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_COMMON = "common.entitymanager.packages.to.scan"; 
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_CMS = "cms.entitymanager.packages.to.scan"; 

    @Resource 
    private Environment env; 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 

     dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER)); 
     dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL)); 
     dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME)); 
     dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD)); 
     return dataSource; 
    } 

    @Bean 
    public LocalSessionFactoryBean sessionFactory() { 
     LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); 
     sessionFactoryBean.setDataSource(dataSource()); 
     //sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN)); 
     sessionFactoryBean.setPackagesToScan(new String[] { 
       env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_SERVICES), 
       env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_COMMON), 
       env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_CMS) 
       }); 
     sessionFactoryBean.setHibernateProperties(hibProperties()); 
     return sessionFactoryBean; 
    } 

    private Properties hibProperties() { 
     Properties properties = new Properties(); 
     properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT)); 
     properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL)); 
     properties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO)); 
     return properties; 
    } 

    @Bean 
    public HibernateTransactionManager transactionManager() { 
     HibernateTransactionManager transactionManager = new HibernateTransactionManager(); 
     transactionManager.setSessionFactory(sessionFactory().getObject()); 
     return transactionManager; 
    } 

    @Bean 
    public UrlBasedViewResolver setupViewResolver() { 
     UrlBasedViewResolver resolver = new UrlBasedViewResolver(); 
     resolver.setPrefix("/WEB-INF/pages/"); 
     resolver.setSuffix(".jsp"); 
     resolver.setViewClass(JstlView.class); 
     return resolver; 
    } 

} 

package com.derp.wicket; 

import org.apache.wicket.request.mapper.parameter.PageParameters; 
import org.apache.wicket.spring.injection.annot.SpringBean; 
import org.apache.wicket.markup.html.basic.Label; 
import org.apache.wicket.markup.html.WebPage; 

import com.derp.wicket.ejbBean.EnterpriseMessage; 

public class HomePage extends WebPage { 
    private static final long serialVersionUID = 1L; 

    @SpringBean 
    private EnterpriseMessage enterpriseMessage; 

    public HomePage(final PageParameters parameters) { 
     super(parameters); 

     add(new Label("message", enterpriseMessage.message)); 
    } 
} 

package com.derp.wicket; 


import javax.servlet.annotation.WebInitParam; 
import javax.servlet.annotation.WebFilter; 

import org.apache.wicket.protocol.http.WicketFilter; 

@WebFilter(value = "/*", initParams = { @WebInitParam(name = "applicationClassName", value = "com.derp.wicket.WicketApplication"), 
     @WebInitParam(name="filterMappingUrlPattern", value="/*") 
}) 
public class ProjectFilter extends WicketFilter { 
} 



package com.derp.wicket; 

import org.apache.wicket.protocol.http.WebApplication; 
import org.apache.wicket.spring.injection.annot.SpringComponentInjector; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 


/** 
* Application object for your web application. If you want to run this application without deploying, run the Start class. 
* 
* @see org.wicketTutorial.Start#main(String[]) 
*/ 
public class WicketApplication extends WebApplication { 
    /** 
    * @see org.apache.wicket.Application#getHomePage() 
    */ 
    @Override 
    public Class<HomePage> getHomePage() { 
     return HomePage.class; 
    } 

    /** 
    * @see org.apache.wicket.Application#init() 
    */ 
    @Override 
    public void init() { 
     super.init(); 

     AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 
     ctx.scan("com.derp.wicket.ejbBean"); 
     ctx.refresh(); 

     getComponentInstantiationListeners().add(new SpringComponentInjector(this, ctx)); 
    } 
} 



<!DOCTYPE html> 
<html xmlns:wicket="http://wicket.apache.org"> 
    <head> 
     <meta charset="utf-8" /> 
     <title></title> 
    </head> 
    <body> 

    <h2> <wicket:container wicket:id="message"></wicket:container></h2>  
     wwiicckkeet 
    </body> 
</html> 

任何人都可以幫助我解決這個問題嗎?

回答

4

看來你正在將Wicket與Spring Web MVC混合在一起。下面我貼我的一些代碼,或者你可以嘗試在GitHub上找到的東西:春季啓動+檢票:https://github.com/Pentadrago/spring-boot-example-wicket

WebApplicationInitializer看起來是這樣的:

public class WebAppInitializer implements WebApplicationInitializer { 

    @Override 
    public void onStartup(ServletContext container) throws ServletException { 
     AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
     container.addListener(new ContextLoaderListener(context)); 
     context.register(ApplicationConfiguration.class); 

     FilterRegistration filter = container.addFilter("wicket.myproject", WicketFilter.class); 
     filter.setInitParameter("applicationClassName", WicketApplication.class.getName()); 
     filter.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, "/*"); 
     filter.addMappingForUrlPatterns(null, false, "/*"); 
    } 

} 

ApplicationConfiguration類使用有看起來像這樣:

@Configuration 
@ComponentScan 
public class ApplicationConfiguration { 

    @Bean 
    public TestService testService(){ 
     return new TestServiceImpl(); 
    } 

} 

我繼承WebApplication類看起來是這樣的:

public class WicketApplication extends WebApplication { 

    @Override 
    public Class<? extends WebPage> getHomePage() { 
     return HomePage.class; 
    } 

    @Override 
    public void init() { 
     super.init(); 
     getComponentInstantiationListeners().add(new SpringComponentInjector(this)); 
    } 
} 

在你的聚甲醛中,你應該有這個代表:

<dependency> 
     <groupId>org.apache.wicket</groupId> 
     <artifactId>wicket-core</artifactId> 
     <version>6.17.0</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>4.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-web</artifactId> 
    <version>4.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>javax.servlet-api</artifactId> 
    <version>3.0.1</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version>4.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.apache.wicket</groupId> 
    <artifactId>wicket-spring</artifactId> 
    <version>6.17.0</version> 
</dependency> 
相關問題