在这段代码中我想在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>
答案 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 String
和int
以及一个方法采取这两个参数:
<强>豆强>
private String headline;
private Person person;
public void myAction(final String headline, final Person person) {
System.out.println(headline + " " + person);
}
Setters/Getters