• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileWriter - Dynamic xhtml - Not Found in ExternalContext as a Resource

 
Men rai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm creating a xhtml file dynamically using FileWriter, first I create a directory and inside I create the xhtml file as follows

boolean suc=new File(directori).mkdir();
fitxer_index.createNewFile();
FileWriter fw = new FileWriter(fitxer_index);

fw.write("<?xml version='1.0' encoding='UTF-8' ?>\r\n");
fw.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n");
fw.write("<html xmlns=\"http://www.w3.org/1999/xhtml\"\r\n");
fw.write("xmlns:ui=\"http://java.sun.com/jsf/facelets\">\r\n");
fw.write("<body>\r\n");
fw.write("<ui:composition template=\"./../../template_epub.xhtml\">\r\n");
fw.write("<ui:define name=\"top\">\r\n");
fw.write("top\r\n");
fw.write("</ui:define>\r\n");
fw.write("</ui:composition>\r\n");
fw.write("</body>\r\n");
fw.write("</html>\r\n");
fw.close();

The problem is that when I access to this file just created I get the next message: /jatp/9NGV4QXCK0M0/1.xhtml Not Found in ExternalContext as a Resource
If I create another xhtml file and I access to the previous xhtml file created It works nice. If I copy the xhtml file and I access to the copy it also works nice. I make a diff and both files are the same.
It is something as if it was necessary to "close" something but I don not what. I'm clossing the FileWriter , the File does not have to be closed.
Does anybody know what is happening??
Thanks in advance.
Dave
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really shouldn't attempt to dynamically create xhtml files. Or, for that matter, JSP files, or even HTML files.

A strictly-compliant WAR is a read-only construct. Some servers under some configurations permit exploding the J2EE-standard WAR file into a directory tree and some of those servers will not actively forbid the webapp from changing what's in there. But it's not safe for a number of reasons. Most notably, that since the J2EE standard itself doesn't allow apps to be self-modifying, there's no assurance that the modifications will work, work correctly, or keep working in 3 months or so. The directories don't really belong to you and are subject to unexpected modification by the server, and the server's caching mechanisms may fail to realize what you are doing. And those are just 2 of the things that can go wrong.

In most cases, a static View definition file is sufficient. I've only written one webapp in something like 8 years that used code to modify the View's structure and it was a very special case. Which if I ever get off my lazy posterior and clean up, I'll post as an open-source product.

The code I used, however, did not create xhtml on the fly - it used control bindings to take a template xhtml and customize it.
 
Men rai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim for your comments.
I think that talking about if its right or wrong creating xhtml files it's another discussion but do you know why the file gives that error message when it's accessed just created?
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, you're violating certain fundamental architectural principles of J2EE when you do this. So it's hardly going to be a surprise when unpredictable/erratic things happen.

My superficial suspicion is that there's a cache of View Definitions in memory that isn't aware of what you're doing because that cache isn't informed about out-of-band changes to the WAR structure.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!