在 Spring MVC 中配置 Apache Wicket

Configure Apache Wicket in Spring MVC

我的 spring 应用程序通过 @RestController 注释提供 REST API,并且运行良好。 现在我想在我的项目中集成一些视图,我想使用 Apache Wicket,但我遇到了问题。我创建了 WebApplication class:

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。

您需要在 wicket 过滤器中提及 Spring 应用程序配置,而不是 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>

Wicket 使用“/”作为主页的挂载点。如果您想在“/index.html”上另外收听,则必须使用 mountPage("index.html", getHomePage()).

显式挂载它