首页 \ 问答 \ 关于GenericApplicationContext中getResource的实现(About implementation of getResource in GenericApplicationContext)

关于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:”前缀。


成功获取资源

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/GenericApplicationContext.html#getResource-java.lang.String-

“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失败

https://tomcat.apache.org/tomcat-8.0-doc/api/org/apache/catalina/webresources/StandardRoot.html#getResource(java.lang.String)

在这段代码中,前缀没有实现,所以我意识到我不能使用“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

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/GenericApplicationContext.html#getResource-java.lang.String-

"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

https://tomcat.apache.org/tomcat-8.0-doc/api/org/apache/catalina/webresources/StandardRoot.html#getResource(java.lang.String)

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
更新时间:2023-07-18 06:07

最满意答案

如果值上下文是全局的,那么可以使用静态变量。 请记住,如果值已更新,则会为所有用户更新该值。 如果数据不同或属于用户,则永远不要使用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 inside MyClass2 method .

you can use following approach.

Declare MyClass1.myvar1 as public and access from MyClass2.

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页面和类 如果由于任何原因认 ...
  • 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.
  • 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 ...
  • 首先,在.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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。