dynamic - JSF adding textfields dynamically on click of a button -
i have written following code can have single textfield followed add button , save button @ bottom.
i want first textfield , add button fixed, whenever user cicks on add button, text field gets added below present textfield , add button , save button goes down.
i have following piece of code, doesnt seem working.
<?xml version="1.0" encoding="utf-8"?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jstl/core" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>dashboard | bluewhale admin</title> <link rel="stylesheet" type="text/css" href="../css/reset.css" media="screen" /> <link rel="stylesheet" type="text/css" href="../css/text.css" media="screen" /> <link rel="stylesheet" type="text/css" href="../css/grid.css" media="screen" /> <link rel="stylesheet" type="text/css" href="../css/layout.css" media="screen" /> <link rel="stylesheet" type="text/css" href="../css/nav.css" media="screen" /> </h:head> <body> <h:form> <hr/> <h:datatable id="newsinputs" value="#{newsalerts.values}" var="item" cellspacing="10"> <h:column> <h:outputlabel value="#{item.label}" /> </h:column> <h:column> <h:inputtext value="#{item.news}" size="100" /><br/> </h:column> </h:datatable> <h:commandbutton styleclass="btn btn-blue" action="#{newsalerts.add()}" value="add"></h:commandbutton> <hr/> <h:commandbutton styleclass="btn btn-blue" action="#{newsalerts.submit}" value="save" /> </h:form> </body> </html>
the bean class follows
package com.kc.aop.bean; import java.util.arraylist; import java.util.list; import javax.faces.bean.managedbean; import javax.faces.bean.viewscoped; import com.kc.aop.vo.newsvo; @managedbean(name = "newsalerts") @viewscoped public class news { private list<newsvo> values; public news() { this.values = new arraylist<newsvo>(); newsvo newsvo = new newsvo(); newsvo.setlabel("news "+ this.values.size()+1); getvalues().add(newsvo); } public string submit() { for(newsvo newsvo : this.values) { system.out.println(newsvo.getnews()); system.out.println(newsvo.getlabel()); } return null; // save values in database } public list<newsvo> getvalues() { return values; } public void setvalues(list<newsvo> values) { this.values = values; } public string add() { newsvo newsvo = new newsvo(); newsvo.setlabel("news "+ this.values.size()+1); this.values.add(newsvo); return "success"; } }
you're returning non-null/void action method:
public string add() { // ... return "success"; }
a non-null/void outcome creates new view scope. need return null or void instead keep same view.
public string add() { // ... return null; }
or
public void add() { // ... }
there's absolutely no need change action listener suggested other answer. serves different purpose.
Comments
Post a Comment