• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to modify JSF page during runtime?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"/>
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not use Dependency Injection in the backing bean? That's pretty much what they mean by Inversion of Control, you know.
 
Vladislav Simovic
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 200
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 <h:outputText value="{StubBeanName.variable}"/> with <h:outputText 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
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic