首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从URL,SpringBoot & AngularJs应用中删除#?

如何从URL,SpringBoot & AngularJs应用中删除#?
EN

Stack Overflow用户
提问于 2018-06-10 03:28:48
回答 1查看 553关注 0票数 0

这是我的TuckeyRewriteFilter过滤器类

代码语言:javascript
运行
复制
public class TuckeyRewriteFilter extends org.tuckey.web.filters.urlrewrite.UrlRewriteFilter {

    @Override
    protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
        String confPath = filterConfig.getInitParameter("confPath");
        ServletContext context = filterConfig.getServletContext();
        try {
            final URL confUrl = getClass().getClassLoader().getResource(confPath);
            final InputStream config = getClass().getClassLoader().getResourceAsStream(confPath);
            Conf conf = new Conf(context, config, confPath, confUrl.toString(), false);
            checkConf(conf);
        } catch (Throwable e) {
            throw new ServletException(e);
        }
    }

}

这是我的springboot主类

代码语言:javascript
运行
复制
public class AdminWebApplication {

    public static final String REWRITE_FILTER_NAME = "rewriteFilter";
    public static final String REWRITE_FILTER_CONF_PATH = "urlrewrite.xml";

    @Autowired
    ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(AdminWebApplication.class, args);
    }

    @Bean
    public ObjectMapper createObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        applicationContext.getAutowireCapableBeanFactory().autowireBean(objectMapper);
        return objectMapper;
    }

    @Bean
    public FilterRegistrationBean rewriteFilterConfig() {
        FilterRegistrationBean reg = new FilterRegistrationBean();
        reg.setName(REWRITE_FILTER_NAME);
        reg.setFilter(new TuckeyRewriteFilter());
        reg.addInitParameter("confPath", REWRITE_FILTER_CONF_PATH);
        reg.addInitParameter("confReloadCheckInterval", "-1");
        reg.addInitParameter("statusPath", "/redirect");
        reg.addInitParameter("statusEnabledOnHosts", "*");
        reg.addInitParameter("logLevel", "WARN");
        return reg;
    }
}

This is my urlrewrite.xml这个文件来自资源文件夹配置是可以正常工作的,加载登录页面,但是我仍然要传递/#login,然后它重定向到/login ,但是在浏览器刷新时我得到了404错误。index.html,我补充说,我不想在我的端口id之后添加额外的域名。

代码语言:javascript
运行
复制
<urlrewrite default-match-type="wildcard">
    <rule>
        <from>/login</from>
        <to>/login.html</to>//As of now only configured for login page.
    </rule>
    <rule>
        <from>/contact</from>
        <to>/index.html</to>
    </rule>
</urlrewrite>
EN

回答 1

Stack Overflow用户

发布于 2018-06-10 03:33:04

在您的js路由器文件(配置阶段)中,您需要添加:

代码语言:javascript
运行
复制
$locationProvider.hashPrefix(''); 
$locationProvider.html5Mode(true);

在您的index.html文件中,您需要添加:

代码语言:javascript
运行
复制
<base href="/">

更新1

在客户端实现上述功能后,您还需要在服务器端配置url映射。使用#时,服务器会忽略它之后的任何内容。

代码语言:javascript
运行
复制
http://localhost:8080/#i_will_be_ignored/and_so_do_i

因此,当您配置angularjs以从URL中删除#时,您的上述请求

代码语言:javascript
运行
复制
http://localhost:8080/i_will_be_ignored/and_so_do_i

现在将使用@path('i_will_be_ignored')命中服务器。现在,这将为您提供404,因为您从未为它映射过任何API。这就是原因,你会在页面刷新时得到404

您需要将所有不匹配的url映射到index.html,后跟不匹配的url。一旦完成,angularjs将从那里获取它并将其重定向到适当的route。我希望这能对你有所帮助。

Something like this will help you out here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50777792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档