2014-02-06 2 views
2

클라이언트 코드에 스프링 REST 서버 (v3.2)와 AngularJS가 있습니다.함께 작동하도록 스프링과 각도를 구성하는 방법

기본 시나리오에서 사용자가 기본 도메인 .com으로 이동하면 index.html이 다시 전송되고 및 그 지점에서 Angular가 통신을 관리합니다.

내 질문은 입니다. 1. 각도 파일을 반환하도록 Spring을 설정하는 방법. 2. 사용자가 기본 도메인으로 이동하지 않고 .com/books/moby-dick으로 이동하여이라고 가정 한 Moby-Dick 북의 JSON 표현을 반환하는 상황을 처리하는 방법 클라이언트가

좋은 자습서를 높이 평가할 것입니다. 이 내 웹 initialzer 클래스입니다 :

public class WebAppInitializer implements WebApplicationInitializer { 

    private static Logger LOG = LoggerFactory.getLogger(WebAppInitializer.class); 

    @Override 
    public void onStartup(ServletContext servletContext) { 
     WebApplicationContext rootContext = createRootContext(servletContext); 

     configureSpringMvc(servletContext, rootContext); 

     FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", CORSFilter.class); 
     corsFilter.addMappingForUrlPatterns(null, false, "/*"); 

//  configureSpringSecurity(servletContext, rootContext); 
    } 

    private WebApplicationContext createRootContext(ServletContext servletContext) { 
     AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 

//  rootContext.register(CoreConfig.class, SecurityConfig.class); 
     rootContext.register(CoreConfig.class); 

     servletContext.addListener(new ContextLoaderListener(rootContext)); 
     servletContext.setInitParameter("defaultHtmlEscape", "true"); 

     return rootContext; 
    } 


    private void configureSpringMvc(ServletContext servletContext, WebApplicationContext rootContext) { 
     AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext(); 
     mvcContext.register(MVCConfig.class); 

     mvcContext.setParent(rootContext); 
     ServletRegistration.Dynamic appServlet = servletContext.addServlet(
       "webservice", new DispatcherServlet(mvcContext)); 
     appServlet.setLoadOnStartup(1); 
     Set<String> mappingConflicts = appServlet.addMapping("/"); 

     if (!mappingConflicts.isEmpty()) { 
      for (String s : mappingConflicts) { 
       LOG.error("Mapping conflict: " + s); 
      } 
      throw new IllegalStateException(
        "'webservice' cannot be mapped to '/'"); 
     } 
    } 

이 내 MVC 구성 파일입니다 : 이것에 대해 다음을위한 # 1 방법

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = {"com.yadazing.rest.controller"}) 
public class MVCConfig extends WebMvcConfigurerAdapter { 

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

답변

3

(면책 조항 : 나는 오전 JHipster의 저자)

당신은 JHipster을 볼 수 있습니다. Spring 백엔드와 AngularJS 프론트 엔드를 사용하여 이러한 응용 프로그램을 생성 할 수 있습니다.

발전기가 필요한 것 (보안 등)을 훨씬 뛰어 넘기 때문에 sample application을 살펴볼 수도 있습니다.

+0

재미 있고 아프다. 감사 – special0ne

0

:

registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); 
+0

모든 URL을 계속 처리하려면 봄이 필요합니다. 이 URL은 moby-dick의 JSON 표현을 반환한다고 가정합니다. 하지만 브라우저에서 GET 요청을 보내면/책/moby-dick의보기를 렌더링하는 Angular를 다시 보내야합니다. – special0ne

관련 문제