Spring MVC VersionResourceResolver / ContentVersionStrategy在JSP中无法正常工作

时间:2016-07-15 01:04:48

标签: spring jsp spring-mvc

我有一个Spring MVC(4.3.0)应用程序,并注册了一个VersionResourceResolver,并在ResourceHandlerRegistry中添加了ContentVersionStrategy。我启用了ResourceUrlEncodingFilter。

    @Bean
    public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
        return new ResourceUrlEncodingFilter();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        boolean devMode = this.env.acceptsProfiles("local");
        //boolean useResourceCache = !devMode;
        Integer cachePeriod = devMode ? 0 : (60 * 60 * 24 * 365); //If dev clear cache else 1 year

        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/")
                .setCachePeriod(cachePeriod)
                .resourceChain(false)
                .addResolver(new VersionResourceResolver()
                    .addContentVersionStrategy("/**"))
                .addTransformer(new AppCacheManifestTransformer());
}

当我使用c:url或spring:url标签访问JSP页面上的/ resources(JS,Images,CSS等)中的任何内容时,“版本化”URL不显示(意思是:没有哈希代码) URL)。例如:

<link href="<c:url value="/resources/css/views/login.css" />" rel="stylesheet">

在检查页面时生成:/myapp/resources/css/views/login.css作为URL字符串。

但是,如果我在我的Controller中使用ResourceURLProvider,我 会看到网址中的哈希码:

@Autowired
private ResourceUrlProvider mvcResourceUrlProvider;

@RequestMapping(value = { "/" }, method = RequestMethod.GET)
public String projectBaseRedirect() {

    logger.debug("js =  '" + this.mvcResourceUrlProvider.getForLookupPath("/resources/js/views/overview.js") + "'");
    logger.debug("css =  '" + this.mvcResourceUrlProvider.getForLookupPath("/resources/css/views/overview-page.css") + "'");

    return "redirect:/admin/overview";
}

日志消息产生了这个:

2016-07-09 11:47:19   DEBUG AdminLoginController:35 - js =  '/resources/js/views/overview-36d1ff98d627d92a72d579eca49dbd8a.js'
2016-07-09 11:47:19   DEBUG AdminLoginController:36 - css =  '/resources/css/views/overview-page-d47f10e5bcf0fdd67bd8057479b523f0.css'

为什么这在控制器中工作而在我的JSP页面上没有?

我也在使用Spring Security(4.1.0)......

2 个答案:

答案 0 :(得分:5)

由于您的示例中缺少它,您的项目中也可能缺少它。您需要为您正在使用的模板引擎提供资源提供程序。如果是JSP,请注册过滤器:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

  @Bean
  public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
    return new ResourceUrlEncodingFilter();
  }
}

或者使用带有该过滤器的过滤器注册bean:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

  @Bean
  public FilterRegistrationBean filterRegistrationBean() {
    final FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new ResourceUrlEncodingFilter());
    filterRegistrationBean.addUrlPatterns("/*");
    return filterRegistrationBean;
  }
}

答案 1 :(得分:3)

罗森对迈克尔答案的评论正是我所需要的缺失胶水。在将此代码放在扩展AbstractAnnotationConfigDispatcherServletInitializer的类中之后,它与我原始帖子中的其他代码完美配合!

[DllImport("RawInput.dll", CallingConvention = CallingConvention.StdCall)]
private static extern MouseRawData GetMouseRawData(IntPtr lParam);
.
.
.
protected override void WndProc(ref Message m)
{
    switch(m.Msg)
    {
        case WM_CREATE:
            if (AttachMouseListener(this.Handle))
                Console.WriteLine("It works!");
            break;
        case WM_INPUT:
            MouseRawData data = GetMouseRawData(m.LParam);
            break;
        default:
            base.WndProc(ref m);
            break;
     }
}