2

Spring REST サーバー (v3.2) とクライアント コード用の AngularJS があります。

基本的なシナリオでの私の理解から、ユーザーはベース ドメイン .com に移動し、index.html が送り返され、その時点から Angular が通信を管理します。

私の質問は次のとおりです。1. Angular ファイルを返すように Spring を設定する方法。2. ユーザーがベース ドメインを経由せずに .com/books/moby-dick に移動する状況を処理する方法。現在、クライアントによってレンダリングされると想定されていた Moby-Dick 本の JSON 表現を返します。

優れたチュートリアルは高く評価されます。これは私のウェブイニシャライザクラスです:

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 構成ファイルです。

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

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

2 に答える 2

3

(免責事項: 私は JHipster の作成者です)

Spring バックエンドと AngularJS フロントエンドを使用して、そのようなアプリケーションを生成するJHipsterを見ることができます。

ジェネレーターは必要なもの (セキュリティなど) をはるかに超えているため、サンプル アプリケーションもご覧ください。

于 2014-02-10T10:45:28.957 に答える
0

#1の場合、これはどうですか:

registry.addResourceHandler("/index.html").addResourceLocations("/index.html");
于 2014-02-06T13:48:33.397 に答える