2014-05-17 106 views
1

在Google搜索超過3個小時後,我放棄了這一點。完全用java配置替換web.xml(Spring MVC 4 - Tomcat 7/8)

我有這樣的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> 

OK,我也只是用servlet的工作,和春季安全。 但是,我想配置(在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 }; 
    } 
} 

回答

2

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

+0

好的,那麼我必須保留web.xml。 – gaspo53

+1

@ gaspo53是的,你可以使用兩者。 –