簡體   English   中英

用java配置完全替換web.xml(Spring MVC 4 - Tomcat 7/8)

[英]Completely replace web.xml with java config (Spring MVC 4 - Tomcat 7/8)

在搜索谷歌超過3個小時后,我放棄了這一點。

我有這個web.xml:

<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>ar.com.dera.simor.config</param-value>
</context-param>

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <trim-directive-whitespaces>true</trim-directive-whitespaces>
    </jsp-property-group>
</jsp-config>

<error-page>
    <error-code>404</error-code>
    <location>/not_found</location>
</error-page>

好的,我只使用servlet和Spring Security。 而且,我想配置(在Java中)jsp-config和錯誤頁面。

我怎樣才能做到這一點? 這是我的WebInitializer類:

@Configuration
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(WebConfig.class);
        rootContext.scan("ar.com.dera.simor.config");
        ServletRegistration.Dynamic appServlet = servletContext.addServlet(
                "appServlet", new DispatcherServlet(rootContext));
        appServlet.setLoadOnStartup(1);
        appServlet.addMapping("/");
        servletContext.addListener(new ContextLoaderListener(rootContext));

        servletContext.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"))
            .addMappingForUrlPatterns(null, false, "/*");
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { SecurityConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter utf8Filter = new CharacterEncodingFilter();
        utf8Filter.setEncoding("UTF-8");
        utf8Filter.setForceEncoding(true);

        return new Filter[] { utf8Filter };
    }
}

基本上,你不能。 ServletContext不提供任何從頭開始配置錯誤頁面和JSP的方法。 但是,它提供了一個getJspConfigDescriptor方法來獲取<jsp-config>配置(readonly)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM