Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不使用web.xml将Angular 2与Spring集成_Angular_Spring Mvc - Fatal编程技术网

不使用web.xml将Angular 2与Spring集成

不使用web.xml将Angular 2与Spring集成,angular,spring-mvc,Angular,Spring Mvc,嗨,我想把Angular 2和Spring结合起来。我想避免在编译时每次在webapp中手动复制dist文件夹的内容。我正试图直接通过pom来完成 我的项目pom如下所示: <!-- Plugin to execute command "npm install" and "npm run build" inside /angular directory --> <plugin> <groupId&g

嗨,我想把Angular 2和Spring结合起来。我想避免在编译时每次在webapp中手动复制dist文件夹的内容。我正试图直接通过pom来完成

我的项目pom如下所示:

 <!-- Plugin to execute command "npm install" and "npm run build" inside 
            /angular directory -->
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <workingDirectory>angular</workingDirectory>
                <installDirectory>temp</installDirectory>
            </configuration>
            <executions>
                <!-- It will install nodejs and npm -->
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <configuration>
                        <nodeVersion>v8.9.1</nodeVersion>
                        <npmVersion>5.5.1</npmVersion>
                        <nodeDownloadRoot>https://nodejs.org/dist/</nodeDownloadRoot>
                        <npmDownloadRoot>http://registry.npmjs.org/npm/-/</npmDownloadRoot>
                    </configuration>
                </execution>

                <!-- It will execute command "npm install" inside "/angular" directory -->
                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>

                <!-- It will execute command "npm build" inside "/angular" directory 
                    to clean and create "/dist" directory -->
                <execution>
                    <id>npm build</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run build</arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Plugin to copy the content of /angular/dist/ directory to output 
            directory (ie/ /target/transactionManager-1.0/) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.2</version>
            <executions>
                <execution>
                    <id>default-copy-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <overwrite>true</overwrite>
                        <!-- <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/</outputDirectory> -->
                        <outputDirectory>${project.basedir}/src/main/webapp</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/angular/dist</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
public void onStartup(ServletContext servletContext) throws ServletException {

    System.out.println("IN Initializer : onStartup");
    WebApplicationContext context = getContext();
    servletContext.addListener(new ContextLoaderListener(context));
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

}

 private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("com.tour.config");
        return context;
    }
appConfig如下所示:

@Bean
public ViewResolver setupViewResolver() {
    UrlBasedViewResolver  resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/");
    resolver.setSuffix(".html");
    resolver.setViewClass(JstlView.class);
    return resolver;
}

 @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
    }
但在点击url时,不会加载索引。 另外,我不能使用web.xml,因此欢迎文件标签也不能使用。欢迎提出任何建议。提前谢谢

编辑:添加代码结构截图


编辑2:我通过扩展WebMVCConfigureAdapter而不是WebMvcConfigurationSupport解决了这个问题。但是,WebMVCConfigureAdapter现在已被弃用。任何关于如何解决这一问题的线索都将不胜感激。谢谢。

我将Angular和Spring应用程序放在两个独立的项目中。为了自动化dist dir的部署,我使用了一个4行npm脚本


我在本演示中演示了如何做到这一点:

添加您的代码结构screenshot@lucumt我已经编辑并添加到问题中这是你的pom吗?我找不到4行npm代码?另外,我没有使用SpringBootNPM脚本,它们不在pom中,而是在客户端的package.json中:@YakovFain:great presentation。您的工作意味着有角应用程序作为主要前端显示。。。对于将angular应用程序作为子项目添加到spring mvc应用程序中,并使build cmd同时包含服务器和angular子应用程序,您建议进行什么更新?(当前应用程序有不同的主前端,并希望在新窗口/选项卡中提供一个链接,该链接应重定向到新的角度子应用程序)@j4v1如果需要在单独的选项卡/窗口中打开该链接,则无需执行任何特殊操作。只需添加一个指向已部署Angular应用程序的主HTML文件的链接,浏览器就会加载它。在用其他技术编写的页面中添加一个角度小部件并不是那么简单。有一个实验项目,角度元素,应该允许这一点。