Wicket 9: 使用JavaScriptFilteredIntoFooterHeaderResponse似乎会导致renderHead出现问题

huangapple go评论107阅读模式
英文:

Wicket 9: usage of JavaScriptFilteredIntoFooterHeaderResponse seems to cause trouble with renderHead

问题

我使用Wicket 9,并包括wicket-spring-boot-starter 3.0.4。

在我的演示项目中,需要将JavaScript放在<body>的末尾。因此,我按照https://ci.apache.org/projects/wicket/guide/9.x/single.html#_put_javascript_inside_page_body中的说明进行操作。

我还通过在我的页面中实现Component#renderHead(IHeaderResponse)来添加资源(CSS和JS)。

在Wicket 8中(来自wicket-spring-boot-starter 2.1.9),一切都按预期工作。

但是在Wicket 9中,为了使我的应用程序运行,我首先在WicketApplicationInitConfiguration#init(WebApplication)中添加了webApplication.getCspSettings().blocking().disabled();。我的应用程序启动了,但生成的页面不包含<head>部分,因此由于缺少资源,应用程序无法正常工作。

为了使<head>元素在我的页面中出现并包含所有引用,我注释掉了所有与在<body>中呈现JavaScript相关的代码。
但是我的应用程序不使用这个"解决方法"。

我做错了什么吗?

  1. @ApplicationInitExtension
  2. public class DemoWicketInitializer implements WicketApplicationInitConfiguration {
  3. @Override
  4. public void init(WebApplication webApplication) {
  5. webApplication.getCspSettings().blocking().disabled();
  6. webApplication.getHeaderResponseDecorators().add(response ->
  7. new ResourceAggregator(
  8. new JavaScriptFilteredIntoFooterHeaderResponse(response, "scripts")
  9. )
  10. );
  11. webApplication.getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
  12. }
  13. }
  1. @WicketHomePage
  2. public class MyPage extends WebPage {
  3. @Override
  4. public void renderHead(IHeaderResponse response) {
  5. super.renderHead(response);
  6. response.render(JavaScriptHeaderItem.forUrl(
  7. "https://api.mapbox.com/mapbox-gl-js/v1.8.1/mapbox-gl.js"));
  8. response.render(JavaScriptHeaderItem.forUrl(
  9. "https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.9/mapbox-gl-draw.js"));
  10. // 这段JS代码必须出现在<body>中。################################
  11. response.render(JavaScriptHeaderItem.forReference(new PackageResourceReference(getClass(),
  12. "js/demoMapbox.js")));
  13. response.render(CssHeaderItem.forUrl(
  14. "https://api.mapbox.com/mapbox-gl-js/v1.8.1/mapbox-gl.css"));
  15. response.render(CssHeaderItem.forUrl(
  16. "https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.9/mapbox-gl-draw.css"));
  17. }
  18. public MyPage(PageParameters parameters) {
  19. // 一些组件
  20. add(new HeaderResponseContainer("scriptBlock", "scripts"));
  21. }
  22. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org" lang="de">
  3. <head>
  4. </head>
  5. <body>
  6. <h1>Wicket und Mapbox</h1>
  7. <div id="ndsmap" style="width: 800px; height: 500px"></div>
  8. <a href="#" wicket:id="sendPostDrawingAction">als POST</a><br>
  9. <h4>Zeichnung als GeoJSON</h4>
  10. <p wicket:id="drawingJson"></p>
  11. <h4>Polygone WGS84</h4>
  12. <p wicket:id="geoWGS84"></p>
  13. <h4>Polygone UTM32N</h4>
  14. <p wicket:id="geoUTM32N"></p>
  15. <wicket:container wicket:id="scriptBlock"/>
  16. </body>
  17. </html>
  1. public class ServletInitializer extends SpringBootServletInitializer {
  2. @Override
  3. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  4. return application.sources(DemoWicketMapboxApplication.class);
  5. }
  6. }
英文:

I'm using Wicket 9 by including wicket-spring-boot-starter 3.0.4 .

In my demo project, it's necessary to put JavaScript at the end of &lt;body&gt;. So I followed the explanations in https://ci.apache.org/projects/wicket/guide/9.x/single.html#_put_javascript_inside_page_body

I also added resources (CSS and JS) via an implementation of Component#renderHead(IHeaderReponse) in my page.

All works as expected with Wicket 8 (from wicket-spring-boot-starter 2.1.9).

With Wicket 9 , to make my App run, I first put webApplication.getCspSettings().blocking().disabled(); in WicketApplicationInitConfiguration#init(WebApplication). My App starts, but the generated Page doesn't contain a &lt;head&gt; section, and so the app doesn't work because of missing resources.

To make the &lt;head&gt; element appear in my page with all references, I commented out all code that refers to the rendering of JavaScript in &lt;body&gt;.
But my app doesn't work with this "workaround".

Am I doing something wrong?

<!-- language: lang-java -->

  1. @ApplicationInitExtension
  2. public class DemoWicketInitializer implements WicketApplicationInitConfiguration {
  3. @Override
  4. public void init(WebApplication webApplication) {
  5. webApplication.getCspSettings().blocking().disabled();
  6. webApplication.getHeaderResponseDecorators().add(response -&gt; new ResourceAggregator(
  7. new JavaScriptFilteredIntoFooterHeaderResponse(response, &quot;scripts&quot;)));
  8. webApplication.getMarkupSettings().setDefaultMarkupEncoding(&quot;UTF-8&quot;);
  9. }
  10. }

<!-- language: lang-java -->

  1. @WicketHomePage
  2. public class MyPage extends WebPage {
  3. @Override
  4. public void renderHead(IHeaderResponse response) {
  5. super.renderHead(response);
  6. response.render(JavaScriptHeaderItem.forUrl(
  7. &quot;https://api.mapbox.com/mapbox-gl-js/v1.8.1/mapbox-gl.js&quot;));
  8. response.render(JavaScriptHeaderItem.forUrl(
  9. &quot;https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.9/mapbox-gl-draw.js&quot;));
  10. // this piece of JS has to appear in &lt;body&gt;. ################################
  11. response.render(JavaScriptHeaderItem.forReference(new PackageResourceReference(getClass(),
  12. &quot;js/demoMapbox.js&quot;)));
  13. response.render(CssHeaderItem.forUrl(
  14. &quot;https://api.mapbox.com/mapbox-gl-js/v1.8.1/mapbox-gl.css&quot;));
  15. response.render(CssHeaderItem.forUrl(
  16. &quot;https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.9/mapbox-gl-draw.css&quot;));
  17. }
  18. public MyPage(PageParameters parameters) {
  19. // some components
  20. add(new HeaderResponseContainer(&quot;scriptBlock&quot;, &quot;scripts&quot;));
  21. }
  22. }

<!-- language: lang-html -->

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:wicket=&quot;http://wicket.apache.org&quot; lang=&quot;de&quot;&gt;
  3. &lt;head&gt;
  4. &lt;/head&gt;
  5. &lt;body&gt;
  6. &lt;h1&gt;Wicket und Mapbox&lt;/h1&gt;
  7. &lt;div id=&quot;ndsmap&quot; style=&quot;width: 800px; height: 500px&quot;&gt;&lt;/div&gt;
  8. &lt;a href=&quot;#&quot; wicket:id=&quot;sendPostDrawingAction&quot;&gt;als POST&lt;/a&gt;&lt;br&gt;
  9. &lt;h4&gt;Zeichnung als GeoJSON&lt;/h4&gt;
  10. &lt;p wicket:id=&quot;drawingJson&quot;&gt;&lt;/p&gt;
  11. &lt;h4&gt;Polygone WGS84&lt;/h4&gt;
  12. &lt;p wicket:id=&quot;geoWGS84&quot;&gt;&lt;/p&gt;
  13. &lt;h4&gt;Polygone UTM32N&lt;/h4&gt;
  14. &lt;p wicket:id=&quot;geoUTM32N&quot;&gt;&lt;/p&gt;
  15. &lt;wicket:container wicket:id=&quot;scriptBlock&quot;/&gt;
  16. &lt;/body&gt;
  17. &lt;/html&gt;

<!-- language: lang-java -->

  1. public class ServletInitializer extends SpringBootServletInitializer {
  2. @Override
  3. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  4. return application.sources(DemoWicketMapboxApplication.class);
  5. }
  6. }

答案1

得分: 3

看起来CSP机制正在干扰JavaScriptFilteredIntoFooterHeaderResponse。我将在JIRA上提出这个问题。与此同时,作为一种解决方法,您可以使用setHeaderResponseDecorator(已弃用)而不是getHeaderResponseDecorators:

setHeaderResponseDecorator(response -> new ResourceAggregator(
new JavaScriptFilteredIntoFooterHeaderResponse(response, "scripts")));

这应该使您的应用程序正常工作。

更新

如在jira上指出,这不是一个错误。由于现在我们已经默认添加了一个CSP的ResourceAggregator,要添加一个进一步的响应装饰器,我们应该执行以下操作:

getHeaderResponseDecorators().add(response ->
new JavaScriptFilteredIntoFooterHeaderResponse(response, "scripts"));

我们将相应地更新Wicket 9.x及其迁移指南的文档。

英文:

it looks like CSP mechanism is interfering with JavaScriptFilteredIntoFooterHeaderResponse. I will open an issue about this on JIRA. In the meantime as workaround you can use setHeaderResponseDecorator (which is deprecated) instead of getHeaderResponseDecorators:

  1. setHeaderResponseDecorator(response -&gt; new ResourceAggregator(
  2. new JavaScriptFilteredIntoFooterHeaderResponse(response, &quot;scripts&quot;)));

This should make your app work.

UPDATE

as pointed out on jira this is not a bug. Since now we already add a ResourceAggregator by default with CSP, to add a further response decorator we should do the following:

  1. getHeaderResponseDecorators().add(response -&gt;
  2. new JavaScriptFilteredIntoFooterHeaderResponse(response, &quot;scripts&quot;));

We will update the documentation for Wicket 9.x and its migration guide accordingly.

huangapple
  • 本文由 发表于 2020年7月30日 00:29:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63158243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定