i have a little problem. I have want to get the value from my inputarea i am tipping in, but it doesnt update properly. the String value is always null.
xhtml:
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Pragma" content="No-cache" />
<meta http-equiv="Cache-Control"
content="no-store,No-cache,must-revalidate,post-check=0,pre-check=0,max-age=0" />
<meta http-equiv="Expires" content="-1" />
<link type="text/css" rel="stylesheet"
href="#{request.contextPath}/resources/css/styles.css" />
<link rel="shortcut icon" href="#{resource['Icon.png']}"
type="image/x-icon" />
<title>#{msg['title.index']}</title>
</h:head>
<h:body style="background:#f5f5f5;">
<h:form>
<p:commandButton value="#{bean.button}"
style="margin-left:10px;margin-top:10px;" />
<p:inputTextarea value="#{bean.text}" autoResize="true"
placeholder="#{msg['label.placeholder']}" rows="3" cols="90"
style="margin-top:250px;margin-left:5px;">
</p:inputTextarea>
</h:form>
</h:body>
</html>
Bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name = "bean")
#SessionScoped
public class Controller {
private String text;
private String button = "Click";
public String getText() {
System.out.println(text);
return text;
}
public void setText(String text) {
this.text = text;
}
public String getButton() {
return button;
}
public void setButton(String button) {
this.button = button;
}
}
It doesnt seem like he cant find my bean, because the value i gave the button is shown correctly in the browser. But teh input doesnt work.
I have worked with jsf already and i never had problems with passing values through the bean. Another project of me is using the same method like here and it works. But in this case my inputs doesnt go to the bean. Help please.
You have to submit your form for the bean to take the value. Else the form is never sent.
So add an action to your command button which is the method that is gonna be called when the button is pressed.
<p:commandButton value="#{bean.button}"
style="margin-left:10px;margin-top:10px;" action="#{bean.submit}" />
Write in your bean
public void submit(){}
your method can contain anything you want.
There are nice jsf tutorial here those are the ones I used to start (I didn't finish though).
Related
This problem might be related to the one I described earlier here:
PrimeFaces 7.0 <p:textEditor HTML-sanitizer discards text formatting, such as centering
but at least trying to switch off the HTML sanitizer in PrimeFaces 8 did not do the trick, the problem is still there. To reproduce it:
1.) Take the example from the PrimeFaces showcase, as it is shown here:
https://www.primefaces.org/showcase/ui/input/textEditor.xhtml
In case you use PrimeFaces 8.0, just augment the line
<p:textEditor widgetVar="editor1" value="#{editorViewTest.text}" height="300" style="margin-bottom:10px"/>
by the attribute secure="false" as follows:
<p:textEditor widgetVar="editor1" secure="false" value="#{editorViewTest.text}" height="300" style="margin-bottom:10px"/>
Write just a single word, i.e. Header and format it by both aligning it centrally and make it a large text. Click on the "Submit" button. The text that comes in the setter() method of the backing bean IS NOT CENTERED, but is only large.
To demonstrate:
1.) This is how the text is formatted in the editor:
2.) This is what I recieve in the debugger:
For comparison, this is my facelet:
<!DOCTYPE html>
<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:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
lang="en">
<h:head>
<f:facet name="first">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</f:facet>
<title>Heimdi</title>
</h:head>
<h:body>
<h:form>
<p:textEditor widgetVar="editor1" secure="false" value="#{editorViewTest.text}" height="300" style="margin-bottom:10px"/>
<p:commandButton value="Submit"/>
</h:form>
</h:body>
</html>
and this is the backing bean:
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
#Named("editorViewTest")
#RequestScoped
public class EditorView {
private String text;
private String text2;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
}
i have my xhtml page below:
<?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">
<h:head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title> Portal Financeiro </title>
<link rel="icon" type="image/png" href="" />
</h:head>
<h:body>
<h:form id="form">
<h:inputText value="#{testeBean.name}" />
<h:commandButton value="Enviar" action="#{testeBean.enviar}" />
</h:form>
</h:body>
</html>
Behind my bean:
package br.com.teste.controller;
import javax.faces.bean.RequestScoped;
import javax.inject.Named;
#Named
#RequestScoped
public class TesteBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void enviar(){
System.out.println("NAME " + name );
}
}
The problem is: When i submit the form the value of the field name returns null.
My configuration:
I have the file beans.xml inside WEB-INF
I'm working with Jboss 7.1.0 or Glassfish 4.0 whatever gives the same problem.
You have a bad import, CDI annotations are in javax.enteprise.context package. Thus you should import javax.enteprise.context.RequestScoped. Or for this particular combination (#RequestScoped + #Named) you can use built-it stereotype called #Model.
Here is my project structure:
eclipse project structure
this is my HelloWorld.java
package com.tutorialspoint.test;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name = "helloWorld")
#RequestScoped
public class HelloWorld
{
public HelloWorld()
{
System.out.println("HelloWorld started!");
}
public String getMessage()
{
return "JSF2!";
}
}
and this is my index.xhtml
<?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 lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
HEllo form <h:outputLabel value="#{helloWorld.getMessage()}" />
</h:body>
</h:body>
</html>
the output that i get after entering localhost:8080/demojsf/ is HEllo form and not
HEllo from jsf2.
What is wrong here?
Below is the way of using h:outputLabel using for attribute of h:outputLabel
public class HelloWorld
{
public String message= "JSF2!";
public HelloWorld()
{
System.out.println("HelloWorld started!");
}
public String getMessage()
{
return message;
}
}
<h:outputLabel for="msgID" value="HEllo form " />
<h:outputText id="msgID" value="#{helloWorld.message}"/>
The h:outputLabel will be calling the getter method of the attribute. So change the code as below,
<h:outputLabel value="#{helloWorld.message}" />
Please find below a working code sample for me,
<!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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<head>
<title>JSF Hello World</title>
</head>
<body>
<f:view>
<h:form>
<h2>
<h:outputLabel value="#{jsfHelloWorldBean.message}" />
</h2>
</h:form>
</f:view>
</body>
</html>
And my bean
package devmanuals;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name="jsfHelloWorldBean")
#RequestScoped
public class JsfHelloWorldBean {
//String message;
public String getMessage(){
return "JSF!2";
}
}
your code works fine on my side. I used JSF2.1, JDK7, and Netbeans 7.3, well except for the double </h:body>
I have written the following code so that I can have a single textfield followed by a add button and a save button at the bottom.
I want the first textfield and add button to be fixed, but whenever a user cicks on add button, a text field gets added below the present textfield and the add button and save button goes down.
I have the following piece of code, but it doesnt seem to 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 is as 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 from action method:
public String add() {
// ...
return "success";
}
A non-null/void outcome creates a new view scope. You need to return null or void instead to keep the same view.
public String add() {
// ...
return null;
}
or
public void add() {
// ...
}
There's absolutely no need to change it by an action listener as suggested by the other answer. It serves a completely different purpose.
See also:
Recommended JSF 2.0 CRUD frameworks
You need to use actionListener or at least modify action to return void or null in your <h:commandButton /> since you stay in the same view. By using action with a return value, the ViewScope is broken and recreated.
View code :
<h:commandButton styleClass="btn btn-blue" actionListener="#{newsAlerts.add}" value="Add" />
<hr/>
<h:commandButton styleClass="btn btn-blue" actionListener="#{newsAlerts.submit}" value="Save" />
Bean code :
public void add(ActionEvent event)
{
NewsVO newsVO = new NewsVO();
newsVO.setLabel("News "+ this.values.size()+1);
this.values.add(newsVO);
}
public void submit(ActionEvent event)
{
for(NewsVO newsVO : this.values)
{
System.out.println(newsVO.getNews());
System.out.println(newsVO.getLabel());
}
}
More info :
Action vs ActionListener
I have the following code and I want to get the selected row.
#ManagedBean
#SessionScoped
public class ElementTableData {
private List<Element> elementList;
private DataModel<Element> model;
private HtmlDataTable htmlDataTable;
private Element element;
private List<Element> selectedElementList;
....
public HtmlDataTable getHtmlDataTable(){
return htmlDataTable;
}
public void setHtmlDataTable(HtmlDataTable aHtmlDataTable){
htmlDataTable = aHtmlDataTable;
}
....
}
When I am trying to make the binding
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<script type="text/javascript" src="js/global.js"></script>
</h:head>
<h:body>
<f:loadBundle
basename="messages"
var="labels" />
<h:form>
<h:dataTable binding="elementTableData.htmlDataTable" styleClass="dataTable" rowClasses="rowOdd,rowEven"
value="#{elementTableData.elementList}" var="element">
<!-- Table Title -->
<f:facet name="caption">#{labels.TableTitle}</f:facet>
I receive the following exception:
javax.faces.FacesException: javax.el.PropertyNotWritableException: /index.xhtml at line 20 and column 52 binding="elementTableData.htmlDataTable": Illegal Syntax for Set Operation
I have getter and setter for htmlDataTable and I don't understand why I am getting the exception.
Thank you very much!
Set your binding as an EL expression:
<h:dataTable binding="#{elementTableData.htmlDataTable}" ...>