GeeCON Prague 2014*
The moose likes JSF and the fly likes how to access values in session from javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "how to access values in session from javascript" Watch "how to access values in session from javascript" New topic
Author

how to access values in session from javascript

Priti Shah
Ranch Hand

Joined: May 10, 2005
Posts: 43
Hi,

My first question is should I use javascript or JSF for minor validations like range check? Is it advisable to go to server for such validations? I am also using 'immidiate=true' when I do such validations. But I just wanted to know the preferrable way.

Second question is when I use javascript in JSF page if I want to use some localized alert messages from my session how can I do that?
If it is JSP i can do something like this
<script> alert('<%=userSession.getAttribute('invalidAliasAlert')%>')
</script>

But how to do that in JSF?

Thanks
Priti Shah
Ranch Hand

Joined: May 10, 2005
Posts: 43
Hello,

Does any one know how to do this ?

Thanks
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Read this.


GenRocket - Experts at Building Test Data
Priti Shah
Ranch Hand

Joined: May 10, 2005
Posts: 43
I am sorry but I really need some answer for that. I am also trying my self to get some work around.
Aditya Tallapally
Greenhorn

Joined: Aug 28, 2005
Posts: 10
Here is the answer for your 1st question
http://www.exadel.com/tutorial/jsf/jsftutorial-validation.html

See this example, it shows validation like min to max etc

second question, i have to see abt it.
Priti Shah
Ranch Hand

Joined: May 10, 2005
Posts: 43
Hi,

I have already tried the first approach, I wanted to know which is standard way to do such thing.

Thanks
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

I do say you need to use both approach.
1. Use java script for client side validation.
2. Also use JSF validation, it works in the cases where browser doesn't support javascript.
ansar basha
Ranch Hand

Joined: Aug 02, 2005
Posts: 34
Hi,
I doubt if can get a session from javascript, as javascript is at the
client side and the session at the server which might not have been
created when the page is being rendered or while the javascript is under
execution.
(I'm too interested to know if there is any work around or any solution for this scenerio).

Thanks
Ansar
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42042
    
  64
This depends on whether you can require JS on the client, and -if you can require it- whether you turn away users that have it turned off. If the answer is yes to both, go with JavaScript.
Of course, if this is a public site where you can't trust the users, you need to check on the server as well, because people will tamper with URLs and request parameters. But it's good practice to check on the client as well, so you can give faster feedback to the user.


Ping & DNS - my free Android networking tools app
Priti Shah
Ranch Hand

Joined: May 10, 2005
Posts: 43
Thanks Ulf,

I think I will do validation on both side.

Hi Ansar,

I am only setting some alert messages from session which is possible i can do that easily in JSP because I can use JSP and JAvascript together but I donot know how to do that in JSF.


I have set of localized alert messages in property file. I am loading this message bundle using <f:loadBundle> but now I want to use some of the messages from this bundle in my javascript.

Any clue any one??

Thanks
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, here is what I did and it worked....



I put that in an on load in the body tag just to test. Worked. I'm curious if you even tried anything or if you have been waiting around for someone to give you the answer?
Priti Shah
Ranch Hand

Joined: May 10, 2005
Posts: 43
Hi Gregg,

I have already tried that but it is not working. Here is what i did.

function validateForm()
{
alias=elements['controllerSettings:controllerSettingsForm:alias'];
if (alias == null || !validateAlias(alias.value)) {
alert("#{requestScope.message['v.common.alert.invalidalias']}");
alias.focus();
return false;
}
}

its alerting the whole string '#{requestScope.message['v.common.alert.invalidalias']}'
I also tried it with '${...' rather than "#{..."

Thanks anyways
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What is v.common.alert.invalidalias?
Priti Shah
Ranch Hand

Joined: May 10, 2005
Posts: 43
'v.commom.alert.invalidalias' is my key in properties file

v.commom.alert.invalidalias=Invalid alias

Thanks
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What J2EE version are we working with here?
Priti Shah
Ranch Hand

Joined: May 10, 2005
Posts: 43
I am using

jdk1.4
Tomcat 5.5.9
Sun RI 1.1
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

This is what I have and it works fine.

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I might mention that you probably need to make sure you have the f:loadBundle tag before you javascript and make sure it is all enclosed in the f:view tag.

Priti Shah
Ranch Hand

Joined: May 10, 2005
Posts: 43
Yes I do have <f:loadbundle> Here is the code

<f:loadBundle basename="/Message" var="message"/>
<f:subview id="controllerSettings">
<h:form id="controllerSettingsForm" onsubmit="return validateForm();">
<h utputText value="#{message['v.controller.id']}"/>
<h utputText value="#{controllerSettings.ID}"/>

....

<f:verbatim>

<script>

elements = document.forms['controllerSettings:controllerSettingsForm'].elements;

function validateForm()
{
alias=elements['controllerSettings:controllerSettingsForm:alias'];
if (alias == null || !validateAlias(alias.value)) {
alert("${requestScope.message['v.commom.alert.invalidalias']}");
alias.focus();
return false;
}
</script>
</f:verbatim>

</f:subview>

I am using 'message' bundle many times inside my subview its working fine but somehow its not working within javascript.

Thanks
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I am at a loss. From what I can tell it should work. It works fine for me.
Priti Shah
Ranch Hand

Joined: May 10, 2005
Posts: 43
I will try to figure it out.

Thanks gregg for helping out.
seb petterson
Ranch Hand

Joined: Mar 04, 2005
Posts: 118
In order for the ${...} to work outside of a jstl tag i think you need to add web-app version in web.xml. I have:



Also, you could try replacing:
${requestScope.message['v.commom.alert.invalidalias']}
with:
<cut value="${requestScope.message['v.commom.alert.invalidalias']}"/>
[ September 02, 2005: Message edited by: seb petterson ]
 
GeeCON Prague 2014
 
subject: how to access values in session from javascript