ThymeLeaf 不解析 JavaScript。

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

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文件(特别是mainpolyfills)时,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:
ThymeLeaf 不解析 JavaScript。

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";
	}
}

huangapple
  • 本文由 发表于 2023年3月12日 10:49:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710812.html
匿名

发表评论

匿名网友

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

确定