我们如何将javascript变量设置为jsf参数

时间:2014-06-25 07:36:14

标签: javascript jsf

在这段代码中我想在jsf页面中使用脚本变量

<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 个答案:

答案 0 :(得分:1)

如果您使用Primefaces及其出色的插件Primefaces Extensions,那么您可以通过以下方式解决您的问题:

<强> 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))" /> 

你的bean需要两个属性String,一个com.yourproject.model.Person对象,它具有JavaScript对象拥有的所有属性 - 3 Stringint以及一个方法采取这两个参数:

<强>豆

private String headline;
private Person person;

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

Setters/Getters

这里是链接Primefaces Extensions MethodParam