如何加载存储在我的/ WEB-INF文件夹中的xsd文件

时间:2022-02-18 14:07:40

I want to load a xsd file that is stored in:

我想加载存储在以下位置的xsd文件:

/WEB-INF/myxsd.xsd

I will be referencing this in my controller's action, not sure how to do that.

我将在我的控制器的动作中引用它,不知道如何做到这一点。

Also, since I will be referencing this all the time, is it possible for me to load it once as oppose to per request?

另外,由于我将一直引用这个,我可以加载一次而不是按照请求加载吗?

public String create() {

   // load xsd file here 

}

Do you use a relative path or full path?

你使用相对路径还是完整路径?

Update

I have this code already that needs the xsd file where I will validate against the schema.

我已经有了这个代码需要xsd文件,我将根据模式进行验证。

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemaFile);

schemaFile is what I need help initializing, it seems newSchema has a few overloads (file, URI, etc.), but this is a local file so it makes sense to get the file correct? Validator validator = schema.newValidator();

schemaFile是我需要帮助初始化的东西,似乎newSchema有一些重载(文件,URI等),但这是一个本地文件,所以让文件正确是有意义的吗? Validator validator = schema.newValidator();

I need help loading this xsd file from my /Web-inf/ folder.

我需要帮助从我的/ Web-inf /文件夹加载这个xsd文件。

2 个解决方案

#1


2  

The ServletContext has a method getRealPath(). So servletContext.getRealPath("WEB-INF") will give you the absolute path of the WEB-INF directory.

ServletContext有一个方法getRealPath()。所以servletContext.getRealPath(“WEB-INF”)将为您提供WEB-INF目录的绝对路径。

Use ServletContext#getResourceAsStream().

To load it only once per request, you can create a field and load it lazily.

要为每个请求仅加载一次,您可以创建一个字段并加载它。

But even better would be to load it as context attribute with a lifecycle listener.

但更好的方法是将其作为具有生命周期监听器的上下文属性加载。

#2


1  

IMHO, the Spring way of doing this would be to inject a SchemaFactory and Resource into the controller and to initialise the Schema only once. N.B. As per Javadocs Schema is thread-safe.

恕我直言,Spring的做法是将SchemaFactory和Resource注入控制器并仅初始化Schema一次。注:根据Javadocs Schema是线程安全的。

public class MyController ... implements InitializingBean {
    private SchemaFactory schemaFactory;
    private Schema schema;
    private Resource schemaResource;

    public void setSchemaFactory(SchemaFactory schemaFactory) {
        this.schemaFactory = schemaFactory;
    }

    public void setSchemaResource(Resource schemaResource) {
        this.schemaResource = schemaResource;
    }

    public void afterPropertiesSet() throws Exception {
        Source xsdSource = new StreamSource(schemaResource.getInputStream());
        schema = schemaFactory.newSchema(xsdSource);
    }

    public void create() {
        // use schema
    }
}

And the spring config:

而春天的配置:

<bean id="schemaFactory" 
      class="javax.xml.validation.SchemaFactory"
      factory-method="newInstance">
    <constructor-arg>
        <util:constant static-field="javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI"/>
    </constructor-arg>
</bean>

<bean id="myController" class="...MyController">
    <property name="schemaFactory" ref="schemaFactory" />
    <property name="resource" value="/WEB-INF/myxsd.xsd" />
</bean>

#1


2  

The ServletContext has a method getRealPath(). So servletContext.getRealPath("WEB-INF") will give you the absolute path of the WEB-INF directory.

ServletContext有一个方法getRealPath()。所以servletContext.getRealPath(“WEB-INF”)将为您提供WEB-INF目录的绝对路径。

Use ServletContext#getResourceAsStream().

To load it only once per request, you can create a field and load it lazily.

要为每个请求仅加载一次,您可以创建一个字段并加载它。

But even better would be to load it as context attribute with a lifecycle listener.

但更好的方法是将其作为具有生命周期监听器的上下文属性加载。

#2


1  

IMHO, the Spring way of doing this would be to inject a SchemaFactory and Resource into the controller and to initialise the Schema only once. N.B. As per Javadocs Schema is thread-safe.

恕我直言,Spring的做法是将SchemaFactory和Resource注入控制器并仅初始化Schema一次。注:根据Javadocs Schema是线程安全的。

public class MyController ... implements InitializingBean {
    private SchemaFactory schemaFactory;
    private Schema schema;
    private Resource schemaResource;

    public void setSchemaFactory(SchemaFactory schemaFactory) {
        this.schemaFactory = schemaFactory;
    }

    public void setSchemaResource(Resource schemaResource) {
        this.schemaResource = schemaResource;
    }

    public void afterPropertiesSet() throws Exception {
        Source xsdSource = new StreamSource(schemaResource.getInputStream());
        schema = schemaFactory.newSchema(xsdSource);
    }

    public void create() {
        // use schema
    }
}

And the spring config:

而春天的配置:

<bean id="schemaFactory" 
      class="javax.xml.validation.SchemaFactory"
      factory-method="newInstance">
    <constructor-arg>
        <util:constant static-field="javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI"/>
    </constructor-arg>
</bean>

<bean id="myController" class="...MyController">
    <property name="schemaFactory" ref="schemaFactory" />
    <property name="resource" value="/WEB-INF/myxsd.xsd" />
</bean>