Here is how I programmatically create a <h:commandButton>:
private HtmlCommandButton generateButton(String id,String label){
HtmlCommandButton button = (HtmlCommandButton)app.createComponent
(HtmlCommandButton.COMPONENT_TYPE);
button.setId(id);
button.setValue(label);
button.setOnclick("processInput(id)");
return button;
}
When the button is clicked, I need to execute the processInput(id) function. However, when I click that button, nothing happens.
Onclick function will add the processInput as a JavaScript function . You need to pass the id in your Java method like,
setOnClick("processInput(" + id + ")");
To add an action method you need to use setActionExpression,
HtmlCommandButton button = new HtmlCommandButton();
FacesContext fc = FacesContext.getCurrentInstance();
ELContext ctx = fc.getELContext();
String expression = "#{processInput('id')}";
Class[] parameterTypes = new Class[1];
parameterTypes[0]=String.class;
MethodExpression me = fc.getApplication().getExpressionFactory().
createMethodExpression(ctx,
expression, returnType, parameterTypes);
button.setActionExpression(me);
button.setOnclick("alert('');");
Related
I have a dynamically generated Datatable, like this
DataTable dataTable = new DataTable();
dataTable.setValue(relatorioVOList);
dataTable.setVar("rVO");
Column checkBoxColumn = new Column();
checkBoxColumn.getChildren().add(this.viewComponentBuilder.createExpressionTextWithLink("#{rVO.iRelatorio}","#{rVO.nNome}"));
dataTable.getColumns().add(checkBoxColumn);
public HtmlForm createExpressionTextWithLink(String iRelatorioExpressionValue, String valueExpressionValue) {
HtmlForm form = new HtmlForm();
HtmlCommandLink link = new HtmlCommandLink();
//config
FacesContext context = FacesContext.getCurrentInstance();
Application application = context.getApplication();
ExpressionFactory ef = application.getExpressionFactory();
ELContext elc = context.getELContext();
//value that is the reports name
ValueExpression nameValueExp = ef.createValueExpression(elc, valueExpressionValue, Object.class);
link.setValueExpression("value", nameValueExp);
//action that goes to method teste when link is clicked
MethodExpression methodExpression = createMethodExpression("#{componenteC.teste(rVO.iRelatorio)}", String.class, Integer.class);
link.setActionExpression(methodExpression);
form.getChildren().add(link);
return form;
}
private static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createMethodExpression(
facesContext.getELContext(), expression, returnType, parameterTypes);
}
In ComponenteC, a RequestScopedBean, the teste function
public String teste(Integer iRelatorio) {
System.out.println("cheguei");
return "componente";
}
The goal is that the teste function will generate an url according to the iRelatorio parameter.
The issue here is that the function is never called. I tried replacing the rVO.iRelatorio with an explicit 10, "#{componenteC.teste(10)}" and even then the action seems to not be fired.
The report name is displayed correctly.
Dynamically created UIInput, UICommand and UINamingContainer components must have a fixed id assigned. Otherwise it will get an autogenerated one which is not necessarily the same when the view is restored. The component ID is used in request parameter names in submitted form data, which JSF would then use to collect the submitted input values and identify the invoked commands during apply request values phase. If the component ID changes, then JSF won't be able to perform the apply request values phase as intented.
Thus, act accordingly:
dataTable.setId("tableId");
// ...
form.setId("formId");
// ...
link.setId("linkId");
There are other potential causes, but they are not visible in the information provided so far in the question. To cover that, take your time to carefully read the following related answers on "dynamically" creating components/views:
Create inputtext dynamically
How does the 'binding' attribute work in JSF? When and how should it be used?
How to create dynamic JSF form fields
That said, you're really better off using XHTML to declare and create components instead of all that Java code mess. XHTML(+XML) is much more declarative and readable and therefore better understandable and maintainable. JSTL may be very helpful in this all.
Good day, so guys, what I'm wanting to do is the following, I add a HtmlSelectOneMenu via bean in JSF page, and like it when the guy changed his value, HtmlSelectOneMenu him to add another, add the HtmlSelectOneMenu'm getting no problem, however like to know how do I add an event to them via BEAN course ehehee
HtmlSelectOneMenu select = new HtmlSelectOneMenu();
HtmlOutputLabel lblData = new HtmlOutputLabel();
lblData.setValue(list.get(contador).getItem().getNome());
UISelectItem item = new UISelectItem();
item.setItemLabel("NÃO");
item.setItemValue(false);
UISelectItem item2 = new UISelectItem();
item2.setItemLabel("SIM");
item2.setItemValue(true);
select.getChildren().add(item);
select.getChildren().add(item2);
FacesContext context = FacesContext.getCurrentInstance();
ELContext elContext = context.getELContext();
Application app = context.getApplication();
ExpressionFactory expressionFactory = app.getExpressionFactory();
ValueExpression ve = expressionFactory.createValueExpression(elContext, "#{projetoBean.qualRespondeu}", String.class);
select.setValueExpression("valeu", ve);
UIPanel container = (UIPanel) FacesContext.getCurrentInstance().getViewRoot().findComponent(":formProjeto:painel");
container.getChildren().add(lblData);
container.getChildren().add(select);
contador++;
Thanks for answers Kolossus however, decided as follows:
AjaxBehavior ajax = new AjaxBehavior();
ajax.setAsync(true);
ajax.setUpdate(":formProjeto");
MethodExpression ex = expressionFactory.createMethodExpression(elContext, "#{projetoBean.adicionaPergunta()}", null, new Class[]{});
ajax.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(ex, ex));
select.addClientBehavior("change", ajax);
Try the following steps:
Create the AjaxBehaviorListener (the actual processing of the event):
MyAjaxBehaviorListener implements AjaxBehaviorListener{
#Override
public void processAjaxBehavior(AjaxBehaviorEvent e) throws AbortProcessingException {
//perform the event processing here
}
}
Attach the listener to an AjaxBehavior
AjaxBehavior ajaxBehavior = (AjaxBehavior) FacesContext.getCurrentInstance().getApplication() .createBehavior(AjaxBehavior.BEHAVIOR_ID);
MyAjaxBehaviorListener listener = new MyAjaxBehaviorListener();
ajaxBehavior.setExecute(Collections.singletonList("#this")); //sets the execute attribute of the event handler
ajaxBehavior.addAjaxBehaviorListener(listener);
Attach the AjaxBehavior to the component
item.addClientBehavior("change",ajaxBehavior); //set the triggering event to "change"
Using primeFaces I have the following button:
<p:commandButton value="Submit" action="#{createDeal.saveDeal}" update="myPanel" />
This works just fine. However I want to generate that button using java.
I have the following code:
CommandButton submit = new CommandButton();
submit.setValue("Submit");
submit.setUpdate("myPanel");
FacesContext facesCtx = FacesContext.getCurrentInstance();
Application app = facesCtx.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesCtx.getELContext();
MethodExpression methodExpression =null;
methodExpression = elFactory.createMethodExpression(elContext,"#{createDeal.saveDeal}",String.class, new Class[]{});
submit.addActionListener(new MethodExpressionActionListener(methodExpression));
submit.setActionExpression(methodExpression);
createButtons.getChildren().add(submit);
When I click submit, my form validates (which is done using the setRequired function on the input), but the form never hits my createDeal class. What am I doing wrong that the inline button works, but the java generated one does not.
One note. The button created inline using primefaces, is there on page load. The button attempted to be added with java is not done until after an Ajax call is made to generate both the form AND button.
Any assistance would be helpful.
Thanks.
Thanks to BalusC for all your help. I am still not sure how I missed this! The following works:
CommandButton submit = new CommandButton();
submit.setValue("Submit");
submit.setUpdate("myPanel");
submit.setId("create"+panelClass);
FacesContext facesCtx = FacesContext.getCurrentInstance();
ELContext elContext = facesCtx.getELContext();
Application app = facesCtx.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
MethodExpression methodExpression =null;
methodExpression = elFactory.createMethodExpression(elContext,"# {createDeal.saveDeal}",null, new Class[]{});
submit.setActionExpression(methodExpression);
createButtons.getChildren().add(submit);
The null pointer exception was unrelated. It looks like simply adding the ID and removing the ActionListener did the trick. Much obliged!
i use Primefaces 3.0.1 and build up a menubar with programmatically filled model. I need some Links like depotDetails.xhtml?id=1 But if i use these URL for my menuitem
item.setUrl("depotDetail.xhtml?id=1"); // that dont work
so i tried to add an ActionListener:
FacesContext facesContext = FacesContext.getCurrentInstance();
ValueExpression target = facesContext.getApplication().getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(), "#{DepotBean.currentDepot}",String.class);
ValueExpression value = facesContext.getApplication().getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(), "ehnemeneee",String.class);
ActionListener handler = new SetPropertyActionListenerImpl(target, value);
item.addActionListener(handler);
but that also dont work. Can anybody help?
Greets Thomas
I got a solution ... but is it the right way?
// building the menu model ..
UIParameter param = null;
for(Depots testdepot: depotList){
param = new UIParameter();
param.setName("currentDepotId");
param.setValue(testdepot.getIdDepot());
item = new MenuItem();
item.setValue(testdepot.getDepotName());
// calling menuListener
item.addActionListener(new MenuListener());
item.setUpdate("messages");
item.setId("menuItemDepot"+testdepot.getIdDepot());
item.getChildren().add(param);
submenu.getChildren().add(item);
}
// MenuListener
public void processAction(ActionEvent event) throws AbortProcessingException {
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
// get param id
String name = (String) event.getComponent().getAttributes().get("currentDepotId");
// set session var
session.setAttribute("currentDepotId", name);
fc.getExternalContext().redirect(request.getContextPath() + "/userarea/depotDetail.xhtml");
}
// read the session in the depot bean
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
String currentDepotId = session.getAttribute("currentDepotId");
I know about startElement, endElement, and writeAttribute methods on ResponseWriter. My problem is that I want to for example output a h:commandLink by declaring it like HtmlCommandLink link = new HtmlCommandLink(); .
How can I output other UIComponents like this in my own component? I might want to use some RichFaces ajax stuff in my components aswell so hoping I can avoid making it all by scratch.
Edit: What I'm trying to do is create my own tag library with the following tag <myTags:commentTree>. Every comment have a reply button, when the reply button is clicked I render the reply form beneath the comment. Once that is rendered, I would like to output for example the richfaces <a4j:commandButton> component. This have to be done inside my own java tag file which Ive called for CommentsTreeUI.java.
Normally I output all my elements that display the forms and buttons with writer.startElement("input", myComponent); writer.writeAttribute("type", "button", null); but if I could instead do for example startElement("a4j:commandbutton", myComponent) that would help my ALOT since it has all the built in ajax features etc.
Any clues?
This problem was solved by adding new components by using
HtmlCommandButton button = new HtmlCommandButton();
button.encodeAll(context);
You can do something like this:
HtmlCommandLink link = new HtmlCommandLink();
getChildren().add(link);
It does depend on what you want to do with the child components though i.e. if you want them surrounded with custom HTML (in an HTML list, for example) you will need something a bit more complex.
One approach to making composite controls is to use the binding attribute to associate the tag with your own code:
<f:view>
<h:form>
<h:panelGroup binding="#{compositeControlBean.panelGrid}" />
</h:form>
</f:view>
The bean configuration in faces-config.xml:
<managed-bean>
<managed-bean-name>compositeControlBean</managed-bean-name>
<managed-bean-class>
composite.CompositeControlBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
The bean code:
/**
* Configure this bean in request scope as "compositeControlBean".
*/
public class CompositeControlBean {
private transient UIComponent panelGrid;
public UIComponent getPanelGrid() {
if (panelGrid == null) {
panelGrid = createCompositePanel();
}
return panelGrid;
}
public void setPanelGrid(UIComponent panelGrid) {
this.panelGrid = panelGrid;
}
private UIComponent createCompositePanel() {
initContextMemebers();
UIComponent commandLink = createCommandLink();
String id = view.createUniqueId();
UIComponent panelGrid = application
.createComponent("javax.faces.HtmlPanelGroup");
panelGrid.setId(id);
panelGrid.setRendererType("javax.faces.Group");
panelGrid.getChildren().add(commandLink);
return panelGrid;
}
private UIComponent createCommandLink() {
// create control
String id = view.createUniqueId();
UIComponent commandLink = application
.createComponent("javax.faces.HtmlCommandLink");
commandLink.setId(id);
commandLink.setRendererType("javax.faces.Link");
// set attributes (bind to printHello method)
Map<String, Object> attributes = commandLink
.getAttributes();
MethodExpression action = expressionFactory
.createMethodExpression(elContext,
"#{compositeControlBean.printHello}",
String.class, new Class<?>[0]);
attributes.put("value", "print hello");
attributes.put("actionExpression", action);
return commandLink;
}
private transient FacesContext context;
private transient Application application;
private transient ELContext elContext;
private transient ExpressionFactory expressionFactory;
private transient UIViewRoot view;
private void initContextMemebers() {
context = FacesContext.getCurrentInstance();
application = context.getApplication();
elContext = context.getELContext();
expressionFactory = application.getExpressionFactory();
view = context.getViewRoot();
}
public String printHello() {
System.out.println("Hello");
return null;
}
}