英文:
ThymeLeaf do not parse javascript
问题
我正在尝试使用Spring Boot/Thymeleaf提供一个Angular应用程序。这是发送HTML/CSS/JavaScript的类的样子:
@Controller
public class ResourceProvider {
@RequestMapping(method = RequestMethod.GET, value = "/")
public String root() {
return "index";
}
@RequestMapping(method = RequestMethod.GET, value = "/{css}.css")
public String css(@PathVariable(value = "css") String css, Model model) {
return css + ".css";
}
@RequestMapping(method = RequestMethod.GET, value = "/{js}.js")
public String js(@PathVariable(value = "js") String js, Model model) {
return js + ".js";
}
}
所有文件都能够正常找到。但是当index.html
请求JavaScript文件(特别是main
和polyfills
)时,Thymeleaf抛出了一个解析错误。
这里您提到的错误信息包括一些代码,我不会进行翻译。如果您有关于如何禁止Thymeleaf处理JavaScript文件的问题,请提出。
感谢。
英文:
I am trying to serve an Angular app using spring boot/thymeleaf. This is what my class looks like that sends html/css/javascript:
@Controller
public class ResourceProvider {
@RequestMapping(method = RequestMethod.GET, value = "/")
public String root() {
return "index";
}
@RequestMapping(method = RequestMethod.GET, value = "/{css}.css")
public String css(@PathVariable(value = "css") String css, Model model) {
return css + ".css";
}
@RequestMapping(method = RequestMethod.GET, value = "/{js}.js")
public String js(@PathVariable(value = "js") String js, Model model) {
return js + ".js";
}
}
All of the files are found fine. But when index.html requests the javascript files (main and polyfills in particular), Thymeleaf throws a parse error:
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "Zone_symbol_prefix||"" (template: "polyfills.718fa2660ef118dd.js" - line 1, col 89)
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "source";let xi;function Tr(t){const e=xi;return xi=t,e}function TC(t,e=x.Default){if(void 0===xi)throw new D(-203,!1);return null===xi?Ah(t,void 0,e):xi.get(t,e&x.Optional?null:void 0,e)}function C(t,e=x.Default){return(function CC(){return Vl}()||TC)(O(t),e)}function G(t,e=x.Default){return C(t,ho(e))}function ho(t){return typeof t>"u"||"number"==typeof t?t:0|(t.optional&&8)|(t.host&&1)|(t.self&&2)|(t.skipSelf&&4)}function zl(t){const e=[];for(let n=0;n<t.length;n++){const r=O(t[n]);if(Array.isArray(r)){if(0===r.length)throw new D(900,!1);let i,s=x.Default;for(let o=0;o<r.length;o++){const a=r[o],l=MC(a);"number"==typeof l?-1===l?i=a.token:s|=l:i=a}e.push(C(i,s))}else e.push(C(r))}return e}function Fi(t,e){return t[Hl]=e,t.prototype[Hl]=e,t}function MC(t){return t[Hl]}function Fn(t){return{toString:t}.toString()}var At=(()=>((At=At||{})[At.OnPush=0]="OnPush",At[At.Default=1]="Default",At))(),Rt=(()=>{return(t=Rt||(Rt={}))[t.Emulated=0]="Emulated",t[t.None=2]="None",t[t.ShadowDom=3]="ShadowDom",Rt;var t})();const un={},X=[],po=ne({ɵcmp:ne}),Gl=ne({ɵdir:ne}),ql=ne({ɵpipe:ne}),Ph=ne({ɵmod:ne}),cn=ne({ɵfac:ne}),ki=ne({" (template: "main.b51697c6f79772d3.js" - line 1, col 89)
Is there a way to just send the javascript files without Thymeleaf processing them? I have no variables that need to be evaluated in these files, I just need them sent to the client.
Thanks.
答案1
得分: 1
因此,答案就是将所有的JavaScript和CSS移到/static文件夹,而不是/templates文件夹,因为Thymeleaf试图将JavaScript解析为HTML,这显然会导致错误。新的资源提供者类如下:
@Controller
public class ResourceProvider {
@RequestMapping(method = RequestMethod.GET, value = "/")
public String root() {
return "index";
}
}
英文:
So the answer was to simply move all of the javascript and css to the /static folder rather than /templates because Thymeleaf was trying to parse the javascript as HTML, which led to an error obviously. So the new resource provider class is
@Controller
public class ResourceProvider {
@RequestMapping(method = RequestMethod.GET, value = "/")
public String root() {
return "index";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论