jhipster react remove # to URL

2019-08-21 10:24发布

I'm new to the community. I built an e-commercce application with the Jhipster (Java + React Redux) Monolitica. I have the following problem: The application is configured to display a hashtag (#) in the URL for example ... http://localhost:9000/#/. I removed this parameter in React and everything was right. But when I upload the application with Gradle (./gradew) it works, http://localhost:9000/. But if you type directly into the browser http://localhost:9000/home/ I get ERROR 404, Page not found! -----> my application to check the problem (http://www.severobalanceboard.eco.br - OK), (http://www.severobalanceboard.eco.br/historia - ERROR 404) _

I think this problme by Spring.

1条回答
贼婆χ
2楼-- · 2019-08-21 10:53

Resolved, for remove Hash tag # To URL, using Jhipster and React [Spring + ReactJs]. I use the link of @Gaël Marziou told me.

follows the modifications:

==React==

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 YOU NEED CREATE THIS FILE

package 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== now be happy

查看更多
登录 后发表回答