这是我的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);
}
}
}
这是我的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这个文件来自资源文件夹配置是可以正常工作的,加载登录页面,但是我仍然要传递/#login,然后它重定向到/login ,但是在浏览器刷新时我得到了404错误。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>
发布于 2018-06-10 03:33:04
在您的js路由器文件(配置阶段)中,您需要添加:
$locationProvider.hashPrefix('');
$locationProvider.html5Mode(true);
在您的index.html
文件中,您需要添加:
<base href="/">
更新1
在客户端实现上述功能后,您还需要在服务器端配置url映射。使用#
时,服务器会忽略它之后的任何内容。
http://localhost:8080/#i_will_be_ignored/and_so_do_i
因此,当您配置angularjs
以从URL中删除#
时,您的上述请求
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
。我希望这能对你有所帮助。
https://stackoverflow.com/questions/50777792
复制相似问题