1

In this code I want use script variable in jsf page

<script>
var str;
function demo()
{
  str = 'This is demo';
 } 
</script>

<h:commandButton value="Confirm" action="#{mybean2.getOutcome}" actionListener="#{mybean2.attrListener}">

<f:attribute name="entitle" value="here I want to place script variable "/>
</commandButton>

1 Answer 1

1

Provided you are using Primefaces and its brilliant addon Primefaces Extensions, then you could solve your problem in a following fashion:

XHTML

<pe:remoteCommand id="myCommand" name="sendPerson" process="@this"  actionListener="#{myBean.myAction}">  
    <pe:methodSignature parameters="java.lang.String, com.yourproject.model.Person" /> 
    <pe:methodParam name="headline"/>   
    <pe:methodParam name="person">  
        <pe:convertJson />  
    </pe:methodParam>  
</pe:remoteCommand>  

<script type="text/javascript">  
    var headline = 'This is demo';
    person = {  
        name: 'John',
        surname: 'Doe',  
        age: 30,  
        profession: 'Ventriloquist'  
    };  
</script>  

<p:commandButton value="Submit Person" type="button" onclick="sendPerson(headline, JSON.stringify(person))" /> 

Your bean would need two properties String ,a com.yourproject.model.Person Object that has all the properties that JavaScript object has - 3 Strings and an int and a method that takes those two params:

Bean

private String headline;
private Person person;

public void myAction(final String headline, final Person person) {
    System.out.println(headline + " " + person);
}

Setters/Getters

Here's the link Primefaces Extensions MethodParam

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Kuba, but I am using jsf1.2, I think Primefaces are not supported to my project
I see, well in this case you'd be better off with a kind of jQuery walk-around. PS. It's always a good thing to post your technology stack alongside your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.