[PrimeFace] Plusieurs form dans dataTable qui appellent la même méthode Java
Bonjour,
Je travaille sur une fonctionnalité fil d'actualité, sur la même page j'ai une premier formulaire qui me permet d'ajouter un article et sur la même page je liste tous les article dans dataTable. Dans ma dataTable, dans une colonne text (le contenu de mon article) j'ai un lien href comment qui ouvre ((on javascript)) un formulaire caché dans la même colonne text, ce formulaire est pour commenter l'article qui convient.
Mon problème c'est que j'arrive à commenter juste le premier article listé (dans la base de données je trouve le commentaire ajouté et le id de l'article qui convient). pour les autres et quand je debug il passe dans ma classe bean il passe dans @PostConstruct init() puis il sort il passe pas dans ma méthode creatComment(Tweet tweet).
My page xhtml new-tweet.xhtm
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
<ui:composition template="/templates/layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:define name="content">
<h:body>
<h3>New Tweet</h3>
<!--My tweet form to add a new aricle(new news)-->
<h:form id="tweet" onsubmit="return formSubmit()">
<table>
<tr>
<td><h:inputTextarea value="#{tweetBean.myTweet.text}" cols="30" rows="10" /></td>
</tr>
</table>
<p:commandButton value="Publier" action="#{tweetBean.creatTweet}" update=":form:dataTable :form:messages" />
</h:form>
<p:commandButton id="abc" value="Publier" onclick="javascript:clickBout()" action="#{commentBean.creatComment}" update=":form:dataTable"/>
<!-- here pagination of my articles (all news)-->
<h:form id="form">
<p:growl id="messages" showDetail="true"/>
<p:dataTable id="dataTable"
var="tweet"
editable="true"
editMode="row"
filterEvent="keyup"
rowKey="#{tweet.idTweet}"
value="#{tweetBean.tweetsList}"
styleClass="userDataTableStyle"
paginator="true" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,15,50"
>
<f:facet name="header">
News
</f:facet>
<p:ajax event="rowEdit" listener="#{tweetBean.onRowEdit}" update="dataTable :form:messages" />
<p:column headerText="Text" style="width:50%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tweet.text}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tweet.text}" style="width:100%"/>
</f:facet>
</p:cellEditor>
<br></br>
<!-- here my link to show comment form-->
<a id="#{tweet.idTweet}" href="javascript:toggle('#{tweet.idTweetPlus1}','#{tweet.idTweet}');" style="color: #6d84b4;">Commenter</a>
<div id="#{tweet.idTweetPlus1}" style="display: none">
<!-- this is my comment form-->
<form name="#{tweet.idTweetPlus1}">
<p:inputTextarea value="#{commentBean.myComment.text}" cols="50" rows="3" />
<!-- onclick="javascript:test()"-->
<p:commandLink actionListener="#{commentBean.creatComment(tweet)}" value="Click Me" update=":form:dataTable :form:messages">
<f:setPropertyActionListener target="#{commentBean.tweetCommented}" value="#{tweet}" />
</p:commandLink>
</form>
</div>
<div>
<!-- here the pagination of article's comments-->
<p:panel header="More Data Comments">
<p:dataTable id="moreDataTable"
value="#{tweet.commentList}"
var="comment"
paginator="false"
lazy="true"
scrollable="true"
scrollHeight="400">
<p:column>
<h:outputText value="#{comment.text}"/>
</p:column>
<f:facet name="footer">
<p:commandButton value="More comments" action="#{commentBean.increaseRowCount()}" update="moreDataTable"/>
</f:facet>
</p:dataTable>
</p:panel>
</div>
</p:column>
<!-- here columns to edit delete article ..-->
<p:column headerText="Actions">
<p:rowEditor />
</p:column>
<p:column>
<f:facet name="supprimer">
<h:outputText value="" />
</f:facet>
<p:commandButton value="" actionListener="#{tweetBean.deleteTweet2(tweet)}" update="dataTable" icon="ui-icon-close" ajax="true"/>
|
<p:commandLink id="showDialogButton" update=":dialogForm:confirmDialog" title="Delete" oncomplete="confirmation.show()" process=":form:dataTable">
<h:outputText value="Delete" />
<f:setPropertyActionListener value="#{tweet}" target="#{tweetBean.myDetailTweet}" />
</p:commandLink >
</p:column>
<!-- <p:column headerText="Text" style="width:50%" filterBy="#{tweet.text}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tweet.text}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tweet.text}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
-->
<!-- end of my articles dataTable-->
</p:dataTable>
</h:form>
<!-- here my dialogForm to confirm the removal of an article-->
<h:form id="dialogForm">
<p:confirmDialog id="confirmDialog" header="Confirm Delete Tweet" severity="alert" widgetVar="confirmation">
<f:facet name="message">
<h:outputText value="Delete #{tweetBean.myDetailTweet.idTweet} ?" />
</f:facet>
<p:commandButton id="confirm" value="Yes" oncomplete="confirmation.hide()" update=":form:messages"
actionListener="#{tweetBean.deleteTweet}"/>
<p:commandButton id="decline" value="No" onclick="confirmation.hide()" type="button" />
</p:confirmDialog>
</h:form>
</h:body>
<script type="text/javascript" src="JS/jquery1.10.2.min.js"></script>
<script type="text/javascript" src="JS/jquery.1.3.2.js"></script>
<script language="javascript">
function toggle(para1,param2) {
var ele = document.getElementById(para1);
var text = document.getElementById(param2);
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "Commenter";
}
else {
ele.style.display = "block";
text.innerHTML = "Cacher";
}
}
</script>
<script>
function clickBout()
{
alert('coucou');
//alert(string);
}
function test()
{
document.getElementById('abc').click();
//alert(document.getElementById(string).value());
}
function formSubmit()
{
document.getElementById('abc').click();
}
</script>
<script>
$(function() {
$("form").each(function() {
$(this).validate({
submitHandler: function (form) {
return false; //prevent actual submit (requirements?)
}
});
});
});
</script>
<script type="text/javascript">
function handleSubmitRequest(xhr, status, args, dialogName, formName) {
dialog = jQuery('#'+dialogName);
if(args.validationFailed) {
dialog.effect("shake", { times:3 }, 100);
} else {
clearForm(formName);
newcommentDlg.hide();
dialogForm.hide();
}
}
function clearForm(formName){
jQuery('#'+formName).each(function(){
this.reset();
});
}
</script>
<script type="text/javascript">
//<![CDATA[
function valider() {
// si la valeur du champ prenom est non vide
if(document.formSaisie.prenom.value != "") {
// alors on envoie le formulaire
document.formSaisie.submit();
}
else {
// sinon on affiche un message
alert("Saisissez le prénom");
}
}
//]]>
</script>
</ui:define>
</ui:composition> |
my tweetBean class
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| package com.nsium.jsfprimefaceshibernate.bean;
import com.nsium.nscloud.model.Comment;
import com.nsium.nscloud.model.Tweet;
import com.nsium.nscloud.service.CommentService;
import com.nsium.nscloud.service.TweetService;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.RowEditEvent;
import org.primefaces.model.LazyDataModel;
@ManagedBean(name="tweetBean")
@SessionScoped
public class TweetBean implements Serializable {
private Tweet myTweet=new Tweet();
private Tweet myDetailTweet=new Tweet();
private Comment myComment=new Comment();
private transient CommentService myCommentService;
private Tweet tweetCommented=new Tweet();
public Tweet getTweetCommented() {
return tweetCommented;
}
public void setTweetCommented(Tweet tweetCommented) {
this.tweetCommented = tweetCommented;
}
private int myRows;
public int getMyRows() {
return myRows;
}
public void setMyRows(int myRows) {
this.myRows = myRows;
}
public Comment getMyComment() {
return myComment;
}
public void setMyComment(Comment myComment) {
this.myComment = myComment;
}
private List<Tweet>myTweets;
private transient TweetService myTweetService;
private LazyDataModel<Tweet> lazyModel;
public TweetBean() {
myRows = 3;
}
@PostConstruct
public void init(){
myTweetService=new TweetService(1234);
lazyModel = new LazyTweetDataModel(myTweetService);
myCommentService=new CommentService(1234);
}
//The same job of void init()
public LazyDataModel<Tweet> getAllTweets() {
if (lazyModel == null) {
lazyModel = new LazyTweetDataModel(myTweetService);
}
return lazyModel;
}
public Tweet getMyTweet()
{
return myTweet;
}
public void setMyTweet(Tweet pTweet)
{
this.myTweet = pTweet;
}
public Tweet getMyDetailTweet()
{
return myDetailTweet;
}
public void setMyDetailTweet(Tweet myDetailTweet)
{
this.myDetailTweet = myDetailTweet;
}
public LazyDataModel<Tweet> getLazyModel()
{
return lazyModel;
}
public void setLazyModel(LazyDataModel<Tweet> lazyModel)
{
this.lazyModel = lazyModel;
}
public List<Tweet> getMyTweets() {
return myTweets;
}
public void setMyTweets(List<Tweet> myTweets) {
this.myTweets = myTweets;
}
public List<Tweet> getTweetsList()
{
if(myTweets==null)
{
myTweets=myTweetService.findTweets();
}
return myTweets;
}
public void creatTweet()
{
myTweetService.creatNewTweet(myTweet);
}
public void deleteTweet()
{
myTweetService.deleteTweet(myDetailTweet);
}
public void deleteTweet2(Tweet pTweet)
{
myTweetService.deleteTweet(pTweet);
}
public void onRowEdit(RowEditEvent event)
{
Tweet lTweet=(Tweet) event.getObject();
if(myTweetService.updateTweet(lTweet))
{
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Success : ", "The row with the id "+lTweet.getIdTweet()+" has been updated successfully.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
public void onCancel(RowEditEvent event)
{
Tweet lTweet=(Tweet) event.getObject();
if(lTweet!=null)
{
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Warning : ", "The changes to the row with the id "+lTweet.getIdTweet()+" were not made, since you have chosen to cancel.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
/// <summary>
/// Description: A method that allows us get all comments
/// </summary>
public void creatComment(Tweet tweet)
{
myComment.setTweet(tweet);
myCommentService.creatNewComment(myComment);
}
public void creatComment(ActionEvent actionEvent)
{
myComment.setTweet(tweetCommented);
myCommentService.creatNewComment(myComment);
}
} |
This is my commentBean class
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| package com.nsium.jsfprimefaceshibernate.bean;
import com.nsium.nscloud.model.Comment;
import com.nsium.nscloud.model.Tweet;
import com.nsium.nscloud.service.CommentService;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
@ManagedBean(name="commentBean")
@SessionScoped
public class CommentBean implements Serializable {
private Comment myComment=new Comment();
private Tweet tweetCommented=new Tweet();
public Tweet getTweetCommented() {
return tweetCommented;
}
public void setTweetCommented(Tweet tweetCommented) {
this.tweetCommented = tweetCommented;
}
private String myTweetId;
private List<Comment>myCommentList;
private transient CommentService myCommentService;
private CommentLazyDataModel myCommentsModel;
private int myRows;
public CommentBean() {
}
@PostConstruct
public void init() {
myCommentService=new CommentService(1234);
myCommentsModel = new CommentLazyDataModel(myCommentService);
myRows = 3;
myCommentsModel.setPageSize(myRows);
}
///Getters and Setters
public List<Comment> getMyCommentList() {
return myCommentList;
}
public void setMyCommentList(List<Comment> myCommentList) {
this.myCommentList = myCommentList;
}
public String getMyTweetId() {
return myTweetId;
}
public void setMyTweetId(String myTweetId) {
this.myTweetId = myTweetId;
}
public Comment getMyComment() {
return myComment;
}
public void setMyComment(Comment myComment) {
this.myComment = myComment;
}
public CommentLazyDataModel getMyCommentsModel() {
return myCommentsModel;
}
public void setMyCommentsModel(CommentLazyDataModel myCommentsModel) {
this.myCommentsModel = myCommentsModel;
}
public int getMyRows() {
return myRows;
}
public void setMyRows(int myRows) {
this.myRows = myRows;
}
public List<Comment> getCommentsList()
{
if(myCommentList==null)
{
myCommentList=myCommentService.findCommentList();
}
return myCommentList;
}
/// <summary>
/// Description: A method that allows us get all comments
/// </summary>
public void creatComment(Tweet tweet)
{
myComment.setTweet(tweet);
myCommentService.creatNewComment(myComment);
}
/// <summary>
/// Description: A method that allows us get all comments
/// </summary>
public void creatComment()
{
String tweetID=this.myTweetId;
System.err.println(tweetID);
//myComment.setTweet(tweetCommented);
myCommentService.creatNewComment(myComment);
}
public void increaseRowCount() {
this.myRows += 2;
myCommentsModel.setPageSize(myRows);
}
} |
Je reste à votre disposition pour toute explication complémentaire.
Merci
Farid
Primefaces 3.5, Mojarra 2.1.1, Apache-Tomcat-7.0.40, maven 2, Ide Netbeans 7.2.1.
Java EE 5|NetBeans|Hibernate|Google|PrimeFaces|Apache|Chrome