jhipster react删除#到URL

时间:2019-01-18 11:32:34

标签: reactjs url hash jhipster

我是社区的新手。我使用Jhipster(Java + React Redux)Monolitica构建了一个电子商务应用程序。我有以下问题:应用程序配置为在URL中显示井号(#),例如... http://localhost:9000/#/。我在React中删除了此参数,一切正常。但是,当我使用Gradle(./gradew)上传应用程序时,它可以运行,http://localhost:9000/。但是,如果您直接在浏览器http://localhost:9000/home/中键入,则会收到错误404,找不到页面! ----->我的应用程序要检查问题(http://www.severobalanceboard.eco.br-确定),(http://www.severobalanceboard.eco.br/historia-错误404) _

我认为这是Spring的问题。

1 个答案:

答案 0 :(得分:1)

已解决,使用Jhipster和React [Spring + ReactJs]将哈希标签#To URL删除。我使用@GaëlMarziou告诉我的链接。

进行修改:

==反应==

1-com / mycompany / myapp / src / main / webapp / app / app.tsx

import { BrowserRouter as Router } from 'react-router-dom'; 
// import { HashRouter as Router } from 'react-router-dom';

2-com / mycompany / myapp / src / main / webapp / index.html

<!-- <base href="./"/> -->
<base href="/"/> 

3-com / mycompany / myapp / webpack / webpack.prod.js

},
  devServer: {
    historyApiFallback: true, /*insert this line - only use for develop*/
    stats: options.stats,
    hot: true,
    contentBase: './build/www',
    proxy: [{
      context: [
...

== JAVA ==

4-com / mycompany / myapp / src / main / java / br / com / MyApp / config / WebConfigurer.java

@Bean 
    public Html5RouteFilter html5RouteFilter() { 
        return new Html5RouteFilter(); 
    } 

5-com / mycompany / myapp / src / main / java / br / com / MyApp / web / Html5RouteFilter.java 您需要创建此文件

打包com.mycompany.myapp.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * Filter that distinguishes between client routes and server routes  when you don't use '#' in client routes.
 */
public class Html5RouteFilter extends OncePerRequestFilter {

    private Logger log = LoggerFactory.getLogger(getClass());


    // These are the URIs that should be processed server-side
    private static final Pattern PATTERN = Pattern.compile("^/((api|content|i18n|management|swagger-ui|swagger-resources)/|error|h2-console|swagger-resources|favicon\\.ico|v2/api-docs).*");

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain)
        throws ServletException, IOException {
        if (isServerRoute(request)) {
            filterChain.doFilter(request, response);
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("/");
            rd.forward(request, response);
        }
    }

    protected static boolean isServerRoute(HttpServletRequest request) {
        if (request.getMethod().equals("GET")) {
            String uri = request.getRequestURI();
            if (uri.startsWith("/app")) {
                return true;
            }
            return PATTERN.matcher(uri).matches();
        }
        return true;
    }
}

=== END == 现在要开心