2016-01-25 60 views
1

我有我的Spring應用程序,通過@RestController註解提供REST API,它工作得很好。 現在我想在我的項目中集成一些View,我想使用Apache Wicket,但是我遇到了問題。我創建了WebApplication類:配置Apache Wicket的Spring MVC中

public class WicketApp extends WebApplication { 

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

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

,並在同一個文件夾中的Index.java和index.html文件。

但它不工作,我有一個404錯誤。這是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

<!-- The definition of the Root Spring Container shared by all Servlets 
    and Filters --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

<context-param> 
    <param-name>contextClass</param-name> 
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
</context-param> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>com.myapp.AppConfig</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<filter> 
    <filter-name>TestApplication</filter-name> 
    <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> 
    <init-param> 
     <param-name>applicationClassName</param-name> 
     <param-value>com.myapp.web.wicket.WicketApp</param-value> 
    </init-param> 
</filter> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/ws/*</url-pattern> 
</servlet-mapping> 

<filter-mapping> 
    <filter-name>TestApplication</filter-name> 
    <url-pattern>/app/*</url-pattern> 
</filter-mapping> 

在我的配置文件,我添加了這種方法:

@Configuration 
    @EnableWebMvc 
    @EnableTransactionManagement 
    @ComponentScan({ "com.myapp.*" }) 
    @PropertySource(value = { "classpath:hibernate.properties" }) 
    public class AppConfig { 

     //other configurations   

     @Bean 
     public WicketApp application() { 
      return new WicketApp(); 
     } 

我真的不知道問題出在哪裏?

Spring版本是4.1.4 的Apache Wicket的版本是7.1.0 我使用Maven。

+0

什麼網址給404?你的配置看起來不錯。 @ Mihir的答案也是有效的,但不是強制性的。您是否在日誌中看到Wicket在開發模式下啓動的消息? –

+0

/myapp/app/Index.html 我可以看到消息 – zuno

+0

沒有什麼配置爲「index.html的」聽。你需要使用'#mountPage(「index.html」,getHomePage())'。 –

回答

1

Wicket使用「/」作爲主頁的裝載點。如果您想要額外聽取「/index.html」,則必須使用mountPage("index.html", getHomePage())將其明確安裝。

1

您需要提及如下代替WicketApp檢票口過濾器春天應用程序配置。

 <filter-name>WicketFilter</filter-name> 
    <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> 
    <init-param> 
     <param-name>applicationFactoryClassName</param-name> 
     <param-value> 
      org.apache.wicket.spring.SpringWebApplicationFactory 
     </param-value> 
    </init-param> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:/resources/applicationContext.xml</param-value> 
    </init-param> 
+0

它不工作。 我加入 ​​applicationFactoryClassName org.apache.wicket.spring.SpringWebApplicationFactory ​​contextConfigLocation的 類路徑:/ WEB-INF /彈簧/ appServlet/servlet-context.xml zuno

+0

您可以使用公開課嘗試WicketApp擴展WebApplication實現ApplicationCont extAware? – Mihir

+0

讓我將setApplicationContext(ApplicationContext applicationContext)方法留空? – zuno