user2173142
user2173142

Reputation: 39

How can we set the javascript variable as jsf parameter

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>

Upvotes: 1

Views: 1590

Answers (1)

Kuba
Kuba

Reputation: 2089

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

Upvotes: 1

Related Questions