2015-09-25 74 views
2

servletMapping當我嘗試到web.xml的這一部分遷移到我的Spring MVC 4.0(3.0 Servlet的web.xml中沒有;-) anylonger JavaConfig:Spring MVC的JavaConfig遷移問題從web.xml中

的部分的web.xml

<servlet> 
    <servlet-name>rest</servlet-name>    
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>rest</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 

我沒有真正知道如何爲我WebAppInitalizer做到這一點是如下:

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

import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

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

    } 

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

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

} 

是否有身體知道如何做到這一點?

很多感謝

乾杯

約翰

回答

1

請試試這個,

下面的代碼是AppConfig.java,

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.view.JstlView; 
import org.springframework.web.servlet.view.UrlBasedViewResolver; 

@Configuration 
@ComponentScan("com.basepackage") 
@EnableWebMvc 
public class AppConfig extends WebMvcConfigurerAdapter { 
    @Bean 
    public UrlBasedViewResolver setupViewResolver() { 
     UrlBasedViewResolver resolver = new UrlBasedViewResolver(); 
     resolver.setPrefix("/WEB-INF/jsp/"); 
     resolver.setSuffix(".jsp"); 
     resolver.setViewClass(JstlView.class); 
     return resolver; 
    } 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/*"); 
    } 
} 

,這是AppInitializer.java,

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

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

public class AppInitializer implements WebApplicationInitializer { 

    @Override 
    public void onStartup(ServletContext context) throws ServletException { 
     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 
     ctx.register(AppConfig.class); 
     ctx.setServletContext(context); 

     ServletRegistration.Dynamic servlet = context.addServlet("dispatcher", new DispatcherServlet(ctx)); 

     servlet.setLoadOnStartup(1); 
     servlet.addMapping("/"); 

    } 

} 

,需要的依賴關係,

<properties> 
    <springframework.version>4.0.6.RELEASE</springframework.version> 
</properties> 
<dependencies> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.1.0</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet.jsp</groupId> 
     <artifactId>javax.servlet.jsp-api</artifactId> 
     <version>2.3.1</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 
</dependencies> 
+0

嗨古納。感謝您的答案和代碼。我現在也覆蓋了'onstartup'方法,但不知道我是否正確執行了。你正在使用'AppConfig.class'。是我的'RootConfig.class'嗎?所以你的'ctx.register(AppConfig.class);'將由'rootContext.register(getRootConfigClasses()[0]);'在我的編碼?謝謝約翰。 –

+0

對不起#John,我忘了首先添加AppConfig類。請嘗試編輯答案..謝謝你.. – Guna

+0

嗨古納。感謝您的更新。有效。輝煌。乾杯。約翰 –