How to download the xsd file, which is stored in the / WEB-INF folder

I want to download the xsd file, which is stored in:

/WEB-INF/myxsd.xsd 

I will refer to this in my controller action, not sure how to do this.

Also, since I will be referencing this all the time, can I download it once as opposed to a query?

 public String create() { // load xsd file here } 

Are you using relative path or full path?

Update

I already have this code that needs an xsd file where I will check the circuit.

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

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

I need help downloading this xsd file from the / Web-inf / folder.

+4
source share
2 answers

ServletContext has a getRealPath () method. Thus, servletContext.getRealPath ("WEB-INF") will provide you with an absolute path to the WEB-INF directory.

Use ServletContext # getResourceAsStream ().

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

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

+2
source

IMHO, Spring's way is to inject SchemaFactory and Resource into the controller and initialize the schema only once. Notabene According to Javadocs Schema is thread safe.

 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 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
source

Source: https://habr.com/ru/post/1383187/


All Articles