Método no Bean conta caracteres restantes no java bean

Estou tentando fazer um contador de caracteres restantes no <h:inputTextArea>

1 - Primefaces é mandatório neste caso?

2- Como faria o método charactersRemaining

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:ui="http://xmlns.jcp.org/jsf/facelets"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:jsf="http://xmlns.jcp.org/jsf">

<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title>testarea</title>
	
   
</h:head>

	<h:body>
   
	
    
    
     
    	<h1>JSF 2 textarea example</h1>
 
		<h:form>
			<table>
    		<tr>
    			<td valign="top">Address :</td>
    			<td>
    			<h:inputTextarea value="#{user.address}" rows="10" id="textarea"  cols="30" />
    			<h:inputTextarea id="tweet" style="width: 400px;" value="#{user.address}">
   					 <f:ajax event="keydown" render="charactersRemaining"/>
    				 <f:ajax event="keyup" render="charactersRemaining"/>
					 </h:inputTextarea>
                <h:outputText id="charactersRemaining" 
                value="#{user.charactersRemaining}"/> characters left
    			
    			</td> 
    		</tr> 
    		</table>
    		<h:commandButton value="Submit" action="user" />
    	</h:form>
 
    </h:body>
</html>


package com.marcel.model;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
 
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable {
 
	private String address;
	

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	public String charactersRemaining() {
	
	 return ;
    }

}

Não.

Você precisa ter em mente que deve ter um limite de caracteres (x) e um método com ajax para ler cada caractere inserido, permitindo a validação e, se necessáiro, a definição no front.

1 curtida

fiz assim:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:ui="http://xmlns.jcp.org/jsf/facelets"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:jsf="http://xmlns.jcp.org/jsf">

<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title>testarea</title>
	
   
</h:head>

	<h:body>
        
    	<h1>JSF 2 textarea example</h1>
 
		<h:form>
			<table>
    		<tr>
    			<td valign="top">Address :</td>
    			<td>
    			
    			<h:inputTextarea id="tweet" style="width: 400px;" value="#{user.address}" size="50" rows="10" cols="30"  >
   					 <f:ajax event="keydown" render="charactersRemaining"/>
    				 <f:ajax event="keyup" render="charactersRemaining"/>
					 </h:inputTextarea>
                <h:outputText id="charactersRemaining" 
                value="#{user.charactersRemaining}"/> characters left
    			
    			</td> 
    		</tr> 
    		</table>
    		<h:commandButton value="Submit" action="user" />
    	</h:form>
 
    </h:body>
</html>

package com.marcel.model;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import java.io.Serializable;
 
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable {
 
	private String address;
	
	int count;
	

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	
	
	public String charactersRemaining(ActionEvent event){
    	Object searchWord =  event.getSource();
    	
    	if(searchWord != null) {
    	
    	this.count = count + 1;
    	
    	String mostra = Integer.toString(count);
    	
    	return mostra;
    	}
    	
    	return null;
    }

}

Mas deu erro:

**Type**  Exception Report

**Message**  /demo4.xhtml @36,54 value="#{user.charactersRemaining}": Property [charactersRemaining] not found on type [com.marcel.model.UserBean]

**Description**  The server encountered an unexpected condition that prevented it from fulfilling the request.