[英]How to remove # from URL , SpringBoot & AngularJs application?
This is my TuckeyRewriteFilter filter class 这是我的TuckeyRewriteFilter过滤器类
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);
}
}
}
This is my springboot main class 这是我的springboot主班
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 this file is from resources folder configuration is works fine, loading login page, but still I have to pass /#login, then it redirect to /login URL, but on browser refresh I ma getting 404 error. 这是我的urlrewrite.xml,该文件来自资源文件夹,配置正常,正在加载登录页面,但仍然必须传递/#login,然后将其重定向到/ login URL,但是在浏览器刷新时出现404错误。 index.html, I have added , I don't want extra domain name after my port id. index.html,我已经添加了,我不想在端口ID之后添加额外的域名。
<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>
In your js router file (config phase) you need to add: 在您的js路由器文件(配置阶段)中,您需要添加:
$locationProvider.hashPrefix('');
$locationProvider.html5Mode(true);
and in your index.html
file, you need to add: 在您的index.html
文件中,您需要添加:
<base href="/">
After implementing above on client side, you need to configure url mapping on server side as well. 在客户端上实现上述功能后,您还需要在服务器端配置url映射。 With #
, the server ignores whatever is followed after it. 使用#
,服务器将忽略后面的所有内容。
http://localhost:8080/#i_will_be_ignored/and_so_do_i
So, when you configure angularjs
to remove #
from URLs, your above request 因此,当您配置angularjs
从网址中删除#
时,您的上述要求
http://localhost:8080/i_will_be_ignored/and_so_do_i
will now hit server with @path('i_will_be_ignored')
. 现在将使用@path('i_will_be_ignored')
命中服务器。 Now, that will give you 404
because you never mapped any API for it. 现在,这将为您提供404
因为您从未为其映射任何API。 Thats the reason, you are getting 404
on page refresh. 那就是原因,页面刷新是404
。
You need to map all the URLs that doesn't match to index.html
followed by the unmatched url. 您需要将所有不匹配的URL映射到index.html
然后是不匹配的url。 Once that's done, angularjs
will take it from there and redirects to appropriate route
. 完成后, angularjs
将从那里获取它并将其重定向到适当的route
。 I hope this will help you. 我希望这能帮到您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.