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}" ...>
Related
This question already has answers here:
How to choose the right bean scope?
(2 answers)
Closed 4 years ago.
I have an application with a p:selectOneMenu component. This component is used to determine what category of file is being uploaded so I can do some work to it when the file is uploaded.
I implemented the answer from this post and it seems to call my setter methods correctly for fileType. But once the file is submitted and the handleFileUpload method is called, the fileType getter method returns null.
For example, if I select Foo then I get the output
File type changed to: Foo
But when I hit the upload button I get the output
The file type selected is null
When I expect
The file type selected is Foo
What is causing the get method to return two different results and is there a way to fix this?
main.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.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">
<meta http-equiv="refresh"
content="#{session.maxInactiveInterval};url=index.xhtml" />
<f:view contentType="text/html">
<h:head>
<title>File Upload</title>
</h:head>
<p:layout fullPage="true">
<p:layoutUnit position="center">
<ui:insert name="pagebody" />
</p:layoutUnit>
</p:layout>
</f:view>
</html>
index.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:composition 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" template="/templates/main.xhtml">
<ui:define name="pagebody">
<h:body>
<h:form id="uploadform" enctype="multipart/form-data">
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="File Type:" />
<p:selectOneMenu value="#{uploadBean.fileType}">
<f:selectItem itemLabel="Foo" itemValue="Foo"/>
<f:selectItem itemLabel="Bar" itemValue="Bar"/>
<f:ajax listener="#{uploadBean.changeFileType}" />
</p:selectOneMenu>
</h:panelGrid>
<br />
<p:fileUpload fileUploadListener="#{uploadBean.handleFileUpload}" mode="advanced"/>
</h:form>
</h:body>
</ui:define>
</ui:composition>
UploadBean.java
#RequestScoped
#ManagedBean(name = "uploadBean")
public class UploadBean implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String fileType = null;
#PostConstruct
public void init() {
}
public void handleFileUpload(FileUploadEvent event){
System.out.println("The file type selected is " + this.getFileType());
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public void changeFileType() {
System.out.println("File type changed to: " + this.getFileType());
}
}
As pointed out by #Kukeltje the issue was that my bean was not properly scoped. Switching from RequestScoped to ViewScoped fixed the issue I was having.
I would like to work with icefaces, but as soon as I import the libraries of icefaces, my webapplication crashes after clicking a button on the form which worked properly before the import.
What could go wrong?
The source Java:
package test,
import javax.faces.bean.ManagedBean;
#ManagedBean
public class Student {
private String firstName;
// create no-arg constructor
public Student() {
}
// define getter/setter methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
index.xhtml:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Student Registration Form</title>
</h:head>
<h:body>
<h:form>
First name: <h:inputText value="#{student.firstName}" />
<br/><br/>
<h:commandButton value="Submit" action="student_response" />
</h:form>
</h:body>
student_response.xhtml
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Student Confirmation</title>
</h:head>
<h:body>
The student is confirmed: #{student.firstName}
</h:body>
</html>
The error I get:
you have 2 javax.faces. jars in classpath according to your first screenshot.
Please remove ONE of the faces jars in your classpath.
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).
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>