Micronaut + Angular : Refresh page

∥☆過路亽.° 提交于 2021-02-08 08:14:32

问题


I'm building an application with Micronaut and Angular 4.

I've configured the Micronaut to Serving Static Resources

micronaut:
    router:
        static:
            resources:
                enabled: true
                mapping: /**
                paths: classpath:public

Everything works well (DefaultURL route:http://localhost:8080/dashboard). The Angular app is loaded and the user interacts with the app and navigate through the routes correctly.

In the controller I configured the server to redirect to index.html.If the path does not exist in the server.

@Get("/{[path:[^\\.]*}")
    @Secured(SecurityRule.IS_ANONYMOUS)
    @Produces(MediaType.TEXT_HTML)
    public HttpResponse<?> refresh() {
        return HttpResponse.redirect(URI.create("/index.html"));
    }

But when the user refreshes the page (e.g. pressing F5).

If the current Url is "http://localhost:8080/userdetails/status" after refresh the Angular app goes to the default route "http://localhost:8080/dashboard", instead of the route which the user was in "http://localhost:8080/userdetails/status"

Please help me on this Thanks


回答1:


Find the Working Code below which I have been implemented.

@Controller("/")
public class IndexController {
    @Inject
    ResourceResolver res;


    @Get("/{[path:[^\\.]*}")
    @Secured(SecurityRule.IS_ANONYMOUS)
    @Produces(MediaType.TEXT_HTML)
    public HttpResponse<?> refresh(HttpRequest<?> request) {
        StreamedFile indexFile = new StreamedFile(res.getResource("classpath:public/index.html").get());
        return HttpResponse.ok(indexFile);
    }
}



回答2:


It's because you're redirecting to /index.html. The information from the previous request is lost. Instead of redirecting you should instead render the html directly.

There are several tools to do so including using the resourceResolver to resolve the html on the classpath. You can return a StreamedFile instance to render the stream to the response.

The resolver can be injected and you would construct the streamed file yourself.

https://docs.micronaut.io/latest/api/io/micronaut/core/io/ResourceResolver.html https://docs.micronaut.io/latest/api/io/micronaut/http/server/types/files/StreamedFile.html



来源:https://stackoverflow.com/questions/52630974/micronaut-angular-refresh-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!