javascript-spring有条件地加载资源
我想使用JS从服务器加载另一个JS文件,但我想有条件地这样做,例如,如果请求发送的用户ID在我的数据库中,请发回该文件。 所以我想创建一个拦截器。 有没有更好的方法,因为拦截器是一种过度杀伤力
<mvc:interceptors>
<mvc:interceptor>
<bean class="com.mycomp.webservice.UserInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
public class UserInterceptor extends HandlerInterceptorAdapter {
@Autowired
UserService userService;
@Override
@Transactional
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if(!userService.userPresent(request)){
return false;
}
else{
return true;
}
}
编辑:所以我发现问题不是那么清楚。我想上传的文件是静态资源的一部分,所以我不想只把它加载到客户端,我也想缓存它。 所以我留给你a link
编辑2:这就是我最后要做的。在我的mvc资源配置下,我创建了一个拦截器来处理受保护静态资源的请求
<mvc:resources mapping="/static/**" location="/static/" />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/static/protected/**" />
<bean class="com.mycompany.interceptor.ProtectedResourcesInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
public class ProtectedResourcesInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if("your condition is true") {
System.out.println("Access Granted to protected resources");
return true;
}
return false;
}
}
拦截器将处理对/static/protected的请求,如果条件合适,它将为其提供服务。如果有人有更清洁的解决方案,请分享
共 (0) 个答案