Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.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
404:在Tomcat上部署Angular项目?_Angular_Tomcat_Http Status Code 404 - Fatal编程技术网

404:在Tomcat上部署Angular项目?

404:在Tomcat上部署Angular项目?,angular,tomcat,http-status-code-404,Angular,Tomcat,Http Status Code 404,我已经在Angular中开发了一个项目,并使用命令ngbuild--base href/myApp/构建了它。然后我将它部署在我的Tomcat服务器上,在刷新我的myApp页面后,我发现404找不到。我知道服务器不知道客户端路由,所以我必须告诉他,他必须返回到“index.html” 这就是为什么我将我的“tomcat/conf/web.xml”配置为: <error-page> <error-code>404</error-code> &l

我已经在Angular中开发了一个项目,并使用命令
ngbuild--base href/myApp/
构建了它。然后我将它部署在我的Tomcat服务器上,在刷新我的myApp页面后,我发现404找不到。我知道服务器不知道客户端路由,所以我必须告诉他,他必须返回到“index.html”

这就是为什么我将我的“tomcat/conf/web.xml”配置为:

<error-page> 
    <error-code>404</error-code>
    <location>/myApp/index.html</location>
</error-page>

404
/myApp/index.html
但这个问题仍然存在


你知道如何解决这个问题吗?

你需要重新路由到index.html。这是一个已知的问题,您可以在此找到更多信息。我还建议使用和来复制所需的文件

首先,您需要创建一个名为WEB-INf的文件夹。在该文件夹中,您需要放置urlrewrite库和urlrewrite.xml文件。使用grunt,您可以使用命令创建包含all的war文件,包括angular dist文件夹

这是urlrewrite.xml文件的一个示例:

<urlrewrite>

<rule match-type="wildcard">
    <from>
        /home //example route, all www.yourapp.com/home would redirect to home using the index.html
    </from>
    <to last="true">
        index.html
    </to>
</rule>
<rule match-type="wildcard">
    <from>
        /otherroute  //same but with other route and no using * wont work
    </from>
    <to last="true">
        index.html
    </to>
</rule>

我知道我必须重写,但我不知道如何在tomcat服务器上完成。我只是用更好的解释编辑了评论,希望能有所帮助:)谢谢你的解决方案,但这不是解决这个问题的更简单的方法吗?
module.exports = function(grunt) {

grunt.loadNpmTasks('grunt-war');

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    war: {
        target: {
            options: {
                war_dist_folder: 'dist', //war creation destination
                war_name: 'warname',
                webxml_display_name: 'YourAppsname',
                webxml_webapp_version: 'YoursAppversionnumber',
                webxml_webapp_extras: [
                    "<filter>\n<filter-name>UrlRewriteFilter</filter-name>\n<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>\n</filter>\n<filter-mapping>\n<filter-name>UrlRewriteFilter</filter-name>\n<url-pattern>/*</url-pattern>\n<dispatcher>REQUEST</dispatcher>\n<dispatcher>FORWARD</dispatcher>\n</filter-mapping>"
                ],
            },
            files: [{
                    expand: true,
                    cwd: 'dist',
                    src: ['**'],
                    dest: ''
                },
                {
                    expand: true,
                    cwd: 'src/assets/deployment', //folder where the lib folder and urlrewrite.xml file are in your project
                    src: ['**'],
                    dest: 'WEB-INF'
                }
            ]
        }
    }
});

grunt.registerTask('default', ['war']);};
ng build --prod --base-href=/yourapphref/ && grunt