关于GenericApplicationContext中getResource的实现(About implementation of getResource in GenericApplicationContext)
Spring-web,webmvc 4.1.6 Thymeleaf 2.1
我一直在为SpringBoot中的Thymeleaf配置manualy。 我想在ServletContextTemplateResolver中使用“classpath:”前缀设置路径。 我该怎么做才能解决这个问题?
@Bean public ServletContextTemplateResolver templateResolver() { ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); templateResolver.setPrefix("classpath:/templates/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("HTML5"); templateResolver.setCacheable(false); return templateResolver; }
但它不起作用。 原因是下面的代码。 在org.springframework.context.support.GenericApplicationContext中
@Override public Resource getResource(String location) { if (this.resourceLoader != null) { return this.resourceLoader.getResource(location); // with setting of template resolver } return super.getResource(location); // without setting of template resolver }
如果没有ServletContextTemplateResolver的任何设置,则调用GenericApplicationContext.getResource。
设置后,调用StandardRoot.getResourceAsStream。 但是这种方法没有实现支持“class:”前缀。
成功获取资源
“ClassPathResource已实现”
@Override public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); if (location.startsWith("/")) { return getResourceByPath(location); } else if (location.startsWith(CLASSPATH_URL_PREFIX)) { return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); } else { try { // Try to parse the location as a URL... URL url = new URL(location); return new UrlResource(url); } catch (MalformedURLException ex) { // No URL -> resolve as resource path. return getResourceByPath(location); } } }
getResource失败
在这段代码中,前缀没有实现,所以我意识到我不能使用“class:”前缀作为ServletContextTemplateResolver的属性。
在org.apache.catalina.webresources.StandardRoot中
@Override public InputStream getResourceAsStream(String path) { if (path == null) return (null); if (!path.startsWith("/") && GET_RESOURCE_REQUIRE_SLASH) return null; WebResourceRoot resources = context.getResources(); if (resources != null) { return resources.getResource(path).getInputStream(); } return null; }
Spring-web,webmvc 4.1.6 Thymeleaf 2.1
I have been working for manualy configuration of Thymeleaf in SpringBoot. I want to set path in ServletContextTemplateResolver with "classpath:" prefix. What can I do to deal with this problem?
@Bean public ServletContextTemplateResolver templateResolver() { ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); templateResolver.setPrefix("classpath:/templates/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("HTML5"); templateResolver.setCacheable(false); return templateResolver; }
But it doesn't work. The reason is the code below. In the org.springframework.context.support.GenericApplicationContext
@Override public Resource getResource(String location) { if (this.resourceLoader != null) { return this.resourceLoader.getResource(location); // with setting of template resolver } return super.getResource(location); // without setting of template resolver }
Without any settings of ServletContextTemplateResolver, GenericApplicationContext.getResource is called.
With setting of it StandardRoot.getResourceAsStream is called. But this method doesn't have implementation of support "class: " prefix.
Successfully to getResource
"ClassPathResource is implemented"
@Override public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); if (location.startsWith("/")) { return getResourceByPath(location); } else if (location.startsWith(CLASSPATH_URL_PREFIX)) { return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); } else { try { // Try to parse the location as a URL... URL url = new URL(location); return new UrlResource(url); } catch (MalformedURLException ex) { // No URL -> resolve as resource path. return getResourceByPath(location); } } }
Failed getResource
In this code, the prefix is not implemented so , I realized that I can't use "class:" prefix for the property of ServletContextTemplateResolver.
In org.apache.catalina.webresources.StandardRoot
@Override public InputStream getResourceAsStream(String path) { if (path == null) return (null); if (!path.startsWith("/") && GET_RESOURCE_REQUIRE_SLASH) return null; WebResourceRoot resources = context.getResources(); if (resources != null) { return resources.getResource(path).getInputStream(); } return null; }
原文:https://stackoverflow.com/questions/32157958
最满意答案
如果值上下文是全局的,那么可以使用静态变量。 请记住,如果值已更新,则会为所有用户更新该值。 如果数据不同或属于用户,则永远不要使用static,而是在
MyClass2
方法中实例化MyClass1
对象。你可以使用以下方法。
将
MyClass1.myvar1
声明为public并从MyClass2
访问。class MyClass1{ public int myvar1 = 7; } class MyClass2{ public void TestMethod(){ MyClass1 obj = new MyClass1(); int val = obj.myvar1; } }
在这种情况下,您的数据是安全的。
If the value context is global, than it is ok to use static variable. Remember that if the value is updated, its updated for all users. If the data is different or belong to a user, than never use static, instead instantiate object of
MyClass1
insideMyClass2
method .you can use following approach.
Declare
MyClass1.myvar1
as public and access fromMyClass2
.class MyClass1{ public int myvar1 = 7; } class MyClass2{ public void TestMethod(){ MyClass1 obj = new MyClass1(); int val = obj.myvar1; } }
In this case, your data is safe.
相关问答
更多-
而不是像这样注册一些JavaScript代码: Response.write(""); 使用这种内置方法有点干净: Page.ClientScript.RegisterStartupScript(this.GetType(), "showMyMessage", "ShowMessage('Successfully saved!');", true); 这比使用Response.Write要简洁得多, ...
-
网站: 网站项目即将编制。 你最终会有更多的DLL文件,这可能是一个痛苦。 当一个目录中的页面或控件需要在另一个目录中引用页面和控件时,也会出现问题,因为另一个目录可能尚未编译成代码。 另一个问题可以在发布。 如果不知道Visual Studio不断重复使用相同的名称,那么它会为页面生成的DLL文件提供新的名称。 这可能导致包含相同类名的DLL文件的几个关闭副本,这将产生大量的错误。 该网站项目是与Visual Studio 2005一起推出的,但已经证明不是非常受欢迎。 Web应用程序: Web应用程序项 ...
-
静态变量在应用程序域的生命周期中持续存在。 所以会导致静态变量“重置”的两件事情是应用程序域名重新启动或使用一个新类。 在您使用存储在aspx Page类中的静态变量的情况下,当ASP.NET决定将aspx页面重新编译到一个新类中时,您可能会丢失静态变量,用旧的类替换旧页面类。 由于这些原因,如果系统决定重新启动或替换该类( .NET不会在正在运行的应用程序域中删除或卸载类/程序集,那么您的静态变量将重置,因为您正在重新启动或替换新类。适用于App_Code文件夹中的两个aspx页面和类 如果由于任何原因认 ...
-
ASP.NET全局/静态存储?(ASP.NET Global/Static storage?)[2024-02-26]
1个工作进程中的所有请求的静态变量应该相同。 我建议你将日志添加到你的asp.net应用程序中,特别是在application_start / stop和单例的静态构造函数中,以查看发生了什么。 希望这可以帮助。 Static variable should be the same for all requests in 1 worker process. I would suggest you to add logs to your asp.net application, especially in a ... -
我的猜测是DLL是一个32位的DLL,你试图在x64环境中运行。 您可以尝试将应用程序池设置为仅32位吗? 更新:这里有一个简单的说明: http : //help.webcontrolcenter.com/KB/a1114/how-to-enable-a-32-bit-application-pool-in-iis7-dedicatedvps.aspx My guess is that the DLL is a 32-bit only DLL that you are trying to run in a ...
-
你不需要做任何事情。 只需创建MVC应用程序并添加新的Web表单页面即可。 而已。 You don't need to do anything. Simply create your MVC application and add a new web forms page. Thats it.
-
移动应用?(Mobile application ? In Asp.net /)[2023-11-02]
http://blogs.msdn.com/b/webdevtools/archive/2007/09/17/tip-trick-asp-net-mobile-development-with-visual-studio-2008.aspx ASP.Net移动Web表单发生了什么? http://blogs.msdn.com/b/webdevtools/archive/2007/09/17/tip-trick-asp-net-mobile-development-with-visual-studio-200 ... -
如果值上下文是全局的,那么可以使用静态变量。 请记住,如果值已更新,则会为所有用户更新该值。 如果数据不同或属于用户,则永远不要使用static,而是在MyClass2方法中实例化MyClass1对象。 你可以使用以下方法。 将MyClass1.myvar1声明为public并从MyClass2访问。 class MyClass1{ public int myvar1 = 7; } class MyClass2{ public void TestMethod(){ MyClass1 obj = ne ...
-
ASP.Net Web Handler(ASP.Net Web Handler)[2023-09-17]
首先,在.NET中编写的同步和异步处理程序都是托管处理程序。 “托管”只是在IIS上下文中使用的术语,它可以使用以本机,非托管代码和托管编写的处理程序。 至于你应该使用哪个,这取决于。 它将归结为处理程序执行的工作性质以及并发用户负载。 如果您有大量并发用户具有合理数量的IO,数据库,处理或外部服务调用,则异步可以提高性能。 类似地,在处理程序执行长时间运行,大量IO或其他处理的情况下,少量并发用户仍然可以受益。 但是,您不会自动假设最差(高用户并发)并立即设置异步处理程序。 它们的创作,调试和维护都比较复 ... -
.NET 2应用程序仍然应该在Framework 4.x下运行,但不一定反过来 - 这取决于您是否使用了早期版本中不存在的任何框架功能。 A .NET 2 app should still run under Framework 4.x, but not necessarily the other way round - that would depend if you've used any framework features that weren't present in the earlier ve ...