aspose file tools*
The moose likes JSF and the fly likes How to modify JSF page during runtime? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to modify JSF page during runtime?" Watch "How to modify JSF page during runtime?" New topic
Author

How to modify JSF page during runtime?

Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
I have made web server project in NetBeans, and created a few .xhtml pages and beans for them. Now I want to able to modify these pages during runtime and show the modified version in browser.

So lets say I am on index.xhtml page. And I want to go to start.xhtml page. In index page bean i have a method that needs to read the index page file and change some components on it. Then save those changes, and show that modified page to user.

Problem is i dont know where these pages are kept and how to get them. I have seen that they are in war/web-inf but i cant access them. Does anyone know how can this be done?

If some more info is needed I am happy to provide it.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1027
Modifying files during run-time smells fishy to me.

Why can you not just utilize JSP and have those parts of the page dynamic?
That is the whole point of a web application. That you have a bunch of templates, and you customize it for each viewer.

Check out this old article
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Let me try to explain how i got to idea to modify pages during runtime.

My masters project is to build a module for testing databases. That module needs to be integrated into larger interface. My module doesnt have any resources of his own, every resource he needs he gets form server.

What my module does? When server makes new instance of my module it passes him some resources. Then module returns a proper JSF page that will be included into main interface page. Backend JSF mechanism creates a bean for that page. That bean is created automatically. So how can that bean take the needed resources from module?
One way is to put those resources into session, and than bean takes them from there. Problem is there can be multiple database testing and at same time multiple instances of module can exist in users session. How does the bean know which data to take from session? He doesnt.

So one way is for module to have some id. Then module manually creates bean and pusts it into session with name of his id. Then he takes template JSF page, and every occurence of template bean name changes with new bean name. SO its something like this:

session.setAttribute("modul12", new DataBaseBean());

and then change every <h: outputText value="TemplateBean.name"/> with <h: outputText value="modul12.name"/>
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

Why not use Dependency Injection in the backing bean? That's pretty much what they mean by Inversion of Control, you know.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Dieter Quickfend wrote:Why not use Dependency Injection in the backing bean? That's pretty much what they mean by Inversion of Control, you know.


Can you elaborate more on this, I am not familiar with this tehnique?
Guy deLyonesse
Ranch Hand

Joined: Apr 12, 2011
Posts: 200

Vladislav Simovic wrote:
Can you elaborate more on this, I am not familiar with this tehnique?


Prettymuch any decent JSF book explains it.
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Guy deLyonesse wrote:
Vladislav Simovic wrote:
Can you elaborate more on this, I am not familiar with this tehnique?


Prettymuch any decent JSF book explains it.


Since i havent been using any JSF books (just internet knowledge), it would be really helpful for me i f you can reccomend a couple of books, that deal with this issue.
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
I opened another topic, but it seems from answers that i got there dependency injection cant solve my problem. So back to my original problem:

How can you access a page during runtime open it like some common text file and modify contents? From what i have seen pages are stored into .war and i have no clues how to acees them let alone, create new page with modified content.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1027
I still maintain that editing JSP pages at runtime is a bad idea.
In the example you gave, the only customisation you wanted to make was the name of the bean in session to access the data for.

And you don't need to do it at all.

You asked the question: "How does the bean know which data to take from session?"'
Your answer was: "He doesn't."
My answer to that question would be: You tell it.

Presumably your backing bean should only reference/work with one specific module at a time.
You say you can have multiple instances of module, so I would keep a Collection of them in session - probably as a map in fact.

You then retrieve the correct module you want to work with from the session map, set it on your backing bean, and display the page.

By decoupling the page from your data storage, you can reuse that same JSP template page for all of your module data, and avoid having to generate files at runtime.









Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Stefan Evans wrote:I still maintain that editing JSP pages at runtime is a bad idea.
In the example you gave, the only customisation you wanted to make was the name of the bean in session to access the data for.

And you don't need to do it at all.

You asked the question: "How does the bean know which data to take from session?"'
Your answer was: "He doesn't."
My answer to that question would be: You tell it.

Presumably your backing bean should only reference/work with one specific module at a time.
You say you can have multiple instances of module, so I would keep a Collection of them in session - probably as a map in fact.

You then retrieve the correct module you want to work with from the session map, set it on your backing bean, and display the page.

By decoupling the page from your data storage, you can reuse that same JSP template page for all of your module data, and avoid having to generate files at runtime.



When bean is created he would have to access session, and retrieve that collection of bean instances. Problem with that solution is - which instance does bean take, they are all same to him, and there can be any number of them. Based solely on contents of module he can not make desicion if that is the right module for him. You must have in mind that i am not buliding whole system, just the module, so i cant change main interface.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1027
Ok then. Lets assume that you would generate a new jsp page. You now have a page for each module.
How would you then decide which page to invoke?

Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Stefan Evans wrote:Ok then. Lets assume that you would generate a new jsp page. You now have a page for each module.
How would you then decide which page to invoke?



Ok the module puts manually bean into session via session.setAttribute("modulInstance123", modul); Because i made the bean i can pass some parameters to it, like resources. Then module retrieves the page that needs including as a text file. Then everywhere on the page replaces <hutputText value="{StubBeanName.variable}"/> with <hutputText value="{modulInstance123.variable}"/> This is saved into new page that is returned to interface and then included into project. In theory it solves my problem.

Tricky part is getting JSF page and reading it like txt file.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1027
Right so you have multiple pages returned and included in your project.
How do you decide which JSP page (out of the numerous available) to invoke?

How is this different from knowing which module to invoke?


Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Stefan Evans wrote:Right so you have multiple pages returned and included in your project.
How do you decide which JSP page (out of the numerous available) to invoke?

How is this different from knowing which module to invoke?




First question I have answered. Module gets template page, modifies it, saves and returns to intarface. Because every page has its own unique bean (by unique, i mean unique named in session), everything works fine.

To second question - bean is created automatically by using default constuctor with no args. You cant pass parameters to it. So how is the bean aware which module is his? Module - bean is not billateral communication. I create a new instance of module, but bean is created by JSF servlet. If there is way to pass parameters to backing bean it would solve all my problems.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1027
>First question I have answered. Module gets template page, modifies it, saves and returns to intarface.
>Because every page has its own unique bean (by unique, i mean unique named in session), everything works fine.

By "returning" the page to the interface does that mean it is being executed right now? Is it the same request?
ie you make a request to your module, which returns a page you should go to.

In that case, you should just set a request attribute in your module, and your generic page should refer to that attribute.

I'm struggling a bit here, because I can't visualise the flow of http request/response.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to modify JSF page during runtime?