-
1. Re: How to load custom .xml file from WEB-INF?
sijalica Sep 2, 2012 11:26 AM (in response to sijalica)Also tried /target/classes/WEB-INF/mapping.xml and all combinations like /target/... taget/... /classes/... classes/... etc...
-
2. Re: How to load custom .xml file from WEB-INF?
sfcoy Sep 2, 2012 11:31 AM (in response to sijalica)You need to use javax.servlet.ServletContext#getResourceAsStream(java.lang.String).
Otherwise, if you must use a classloader, then your resource must be somewhere in WEB-INF/classes.
-
3. Re: How to load custom .xml file from WEB-INF?
sijalica Sep 2, 2012 11:51 AM (in response to sfcoy)I cannot access ServletContext from this class:
public class MyModelMapper { private static class SingletonHolder { private static Mapper INSTANCE= new DozerBeanMapper() { { List<String> mappingFileUrls= new ArrayList<String>(); // InputStream resourceAsStream = this.getClass().getResourceAsStream("/WEB-INF/mapping.xml"); // InputStream resourceAsStream2 = this.getClass().getResourceAsStream("WEB-INF/mapping.xml"); // InputStream resourceAsStream3 = this.getClass().getResourceAsStream("mapping.xml"); // InputStream resourceAsStream4 = this.getClass().getResourceAsStream("/mapping.xml"); // System.out.println(this.getClass().getClassLoader().getResource("/WEB-INF/mapping.xml")); // System.out.println(this.getClass().getClassLoader().getResource("/mapping.xml")); // System.out.println(this.getClass().getClassLoader().getResource("classes/WEB-INF/mapping.xml")); // System.out.println(this.getClass().getClassLoader().getResource("/classes/WEB-INF/mapping.xml")); // System.out.println(this.getClass().getClassLoader().getResource("target/classes/WEB-INF/mapping.xml")); // System.out.println(this.getClass().getClassLoader().getResource("/target/classes/WEB-INF/mapping.xml")); // System.out.println(this.getClass().getClassLoader().getResource("core/target/classes/WEB-INF/mapping.xml")); // System.out.println(this.getClass().getClassLoader().getResource("/core/target/classes/WEB-INF/mapping.xml")); // System.out.println("a:"+ this.getClass().getClassLoader().getResource("/resources/mapping.xml")); // System.out.println(Thread.currentThread().getContextClassLoader().getResource("/resources/mapping.xml")); // System.out.println(Thread.currentThread().getContextClassLoader().getResource("/mapping.xml")); // System.out.println(Thread.currentThread().getContextClassLoader().getResource("classes/WEB-INF/mapping.xml")); // System.out.println(Thread.currentThread().getContextClassLoader().getResource("/classes/WEB-INF/mapping.xml")); // System.out.println(Thread.currentThread().getContextClassLoader().getResource("target/classes/WEB-INF/mapping.xml")); // System.out.println(Thread.currentThread().getContextClassLoader().getResource("/target/classes/WEB-INF/mapping.xml")); // System.out.println(Thread.currentThread().getContextClassLoader().getResource("core/target/classes/WEB-INF/mapping.xml")); // System.out.println(Thread.currentThread().getContextClassLoader().getResource("/core/target/classes/WEB-INF/mapping.xml")); // System.out.println(Thread.currentThread().getContextClassLoader().getResource("mapping.xml")); // System.out.println(Thread.currentThread().getContextClassLoader().getResource("/WEB-INF/mapping.xml")); // System.out.println(this.getClass().getClassLoader().getResource("WEB-INF/mapping.xml")); // System.out.println(this.getClass().getClassLoader().getResource("webapp/WEB-INF/mapping.xml")); // System.out.println(this.getClass().getClassLoader().getResource("/webapp/WEB-INF/mapping.xml")); // mappingFileUrls.add(this.getClass().getClassLoader().getResource("/WEB-INF/mapping.xml").getFile()); // setMappingFiles(mappingFileUrls); } }; } public static <D> D map(Object source, Class<D> destinationType) { SingletonHolder.INSTANCE.map(source, destinationType); } }
-
4. Re: How to load custom .xml file from WEB-INF?
sijalica Sep 2, 2012 11:57 AM (in response to sijalica)And if I turn this MyModelMapper to @ApplicationScoped bean, how would I access ServletContext? Via CDI? I tried and I get "null" value when I debug the class...?
-
5. Re: How to load custom .xml file from WEB-INF?
nickarls Sep 2, 2012 12:57 PM (in response to sijalica)Tried a ServletContextListener.contextInitalized(SCE e) and stash the context away?
-
6. Re: How to load custom .xml file from WEB-INF?
sijalica Sep 2, 2012 1:05 PM (in response to nickarls)Well, I solved it the following way:
I put the file to:
src/main/resources/mapping.xml
instead of webapp/WEB-INF
and loaded it only by providing "mapping.