backing bean in jsf scope - jsf

I am displaying a ProductList, which is made up of Product objects. The Product has attributes of type int, string, string, and int. The data is being pulled from the database and I build a ProductList. The data is in the database, I can see it, but when I display the table, the table shows 0's for the 2 int columns, and blanks in the String columns. Here is the ProductList:
#ManagedBean
public class ProductList {
private ArrayList<Product> allProducts;
public ProductList(){
allProducts = DatabaseConnector.getAllProducts();
}
public ArrayList<Product> getAllProducts(){
return allProducts;
}
public void setAllProducts(ArrayList<Product> allProducts){
this.allProducts = allProducts;
}
}
And here is the Product bean:
#ManagedBean
public class Product {
private int id;
private String productName;
private String description;
private int quantity;
public Product() {
}
public void setId(int id) {
this.id = id;
}
public void setProductName(String productName) {
this.productName = productName;
}
public void setDescription(String description) {
this.description = description;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getId() {
return id;
}
public String getProductName() {
return productName;
}
public String getDescription() {
return description;
}
public int getQuantity() {
return quantity;
}
}
Should I change the scope of the beans?

Add scope annotation to your bean , for example #RequestScoped or #ViewScoped
otherwise it will be the default scope which is #NoneScoped

Related

Field attribute is coming as null while unmarshalling an xml file

Hi I am trying to convert an xml file into Java Objects using JAXB and I am very new to java. I have created the pojo classes and added some annotations but I am not sure whether they are right? I have spent hours in google but couldn't find what is wrong.
This is my xml :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<question id="1">
<answers>
<answername>java is a programming language</answername>
<id>101</id>
<postedby>ravi</postedby>
</answers>
<answers>
<answername>java is a platform</answername>
<id>102</id>
<postedby>john</postedby>
</answers>
<questionname>What is java?</questionname>
<marks set=50>
<longAnswer set=45/>
<shortAnswer set=30/>
</marks>
</question>
Pojo classes:
#XmlRootElement(name="question")
public class Question {
private int id;
private String questionname;
private List<Answer> answers;
private List<Marks> marks;
public Question() {}
public Question(int id, String questionname, List<Answer> answers, List<Marks> marks) {
super();
this.id = id;
this.questionname = questionname;
this.answers = answers;
this.marks = marks;
}
#XmlElement(name="marks")
public List<Marks> getMarks() {
return marks;
}
public void setMarks(List<Marks> marks) {
this.marks = marks;
}
#XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#XmlElement
public String getQuestionname() {
return questionname;
}
public void setQuestionname(String questionname) {
this.questionname = questionname;
}
#XmlElement
public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
}
public class Answer {
private int id;
private String answername;
private String postedby;
public Answer() {}
public Answer(int id, String answername, String postedby) {
super();
this.id = id;
this.answername = answername;
this.postedby = postedby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAnswername() {
return answername;
}
public void setAnswername(String answername) {
this.answername = answername;
}
public String getPostedby() {
return postedby;
}
public void setPostedby(String postedby) {
this.postedby = postedby;
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class Marks {
private LongAnswer longAnswer ;
private ShortAnswer shortAnswer;
private String set;
#XmlAttribute
public String getSet() {
return set;
}
public void setSet(String set) {
this.set = set;
}
#XmlElement(name="longAnswer")
public LongAnswer getLongAnswer() {
return longAnswer;
}
public void setLongAnswer(LongAnswer longAnswer) {
this.longAnswer = longAnswer;
}
#XmlElement(name="shortAnswer")
public ShortAnswer getShortAnswer() {
return shortAnswer;
}
public void setShortAnswer(ShortAnswer shortAnswer) {
this.shortAnswer = shortAnswer;
}
}
public class LongAnswer {
private String set;
public String getSet() {
return set;
}
public void setSet(String set) {
this.set = set;
}
public class ShortAnswer {
private String set;
public String getSet() {
return set;
}
public void setSet(String set) {
this.set = set;
}
}
Can anyone tell me how to annotate the 'marks' model class and how to set 'longAnswer' and 'shortAnswer' field. Because i am getting null values for them.
You should annotate your set properties with #XmlAttribute. Otherwise it looks quite fine.
What you could also do is create an XML Schema for you XML and compile it.

Send Object between #ViewScoped beans

I managed to create very simple example of sending Object between JSF pages:
First page:
#Named
#ViewScoped
public class Pricing
{
public Pricing()
{
int ww = 3;
PricingFormData obj = new PricingFormData(334, "Lalalala");
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("yourKey", obj);
}
Second page:
#Named
#ViewScoped
public class PricingCalculator implements Serializable
{
PricingFormData get;
public PricingCalculator()
{
get = (PricingFormData) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("yourKey");
}
}
Custom Object:
public class PricingFormData
{
private int id;
private String name;
public PricingFormData(int id, String name)
{
this.id = id;
this.name = name;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
This code works but I have several questions which I want to ask:
The code is working in View scope. What will happen if multiple users are clicking on the pages? Are these Objects are going to be mixed? Do I need to use some unique ID for Object key for example session ID. But here I don't have session.
What will happen if the Objects are too many(multiple users are working on the web site)? When the objects will be disposed?

spring-data-cassandra component/complex type denormalized mapping

I'm trying to use spring-data-cassandra (1.1.2.RELEASE) and I'm running into an issue with the lack of embeddable type mapping in JPA parlance.
I have an entity class like this:
#Table
public class Customer {
private UUID id;
private String name;
private Address address;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
and the embeddable Address class:
public class Address {
private String address;
private String city;
private String state;
private String zip;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
My cassandra table:
create table customer (
id uuid primary key,
name text,
address text,
city text,
state text,
zip text
);
I want the properties of Address to be mapped into the containing entity, I don't want a separate table for addresses. In JPA, I believe I'd use an #Embeddable annotation. Is there some similar construct in spring-data-cassandra?
Embeddable types are not yet supported by spring-data-cassandra. A feature request is available at DATACASS-167.
The only possible part of an entity to embed is the primary key. If your primary key consists of multiple fields, you can externalize that fields into a separate class and use it afterwards with the #PrimaryKey annotation.
Comment.java
#Table("comments")
public class Comment {
#PrimaryKey
private CommentKey pk;
private String text;
}
CommentKey.java
#PrimaryKeyClass
public class CommentKey implements Serializable {
private static final long serialVersionUID = -7871651389236401141L;
#PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String author;
#PrimaryKeyColumn(ordinal = 1)
private String company;
}
HTH, Mark

h:selectOneMenu along with f:selectItems always returns 0

Here's my XHTML code:
<h:selectOneMenu id="combo" value="#{TeamsHinzufuegenBean.selectedLeagueId}">
<f:selectItems value="#{TeamsHinzufuegenBean.leagues}"
var="league" itemValue="#{league.id}"
itemLabel="#{league.name}"/>
</h:selectOneMenu>
And my bean:
#ManagedBean(name = "TeamsHinzufuegenBean")
#ViewScoped
public class TeamsHinzufügenBean implements Serializable{
private static final long serialVersionUID = 1L;
private List<League> leagues;
private ArrayList<Team> teams = new ArrayList<Team>();
private String teamname;
private int selectedLeagueId=1;
#PostConstruct
public void init() {
leagues = Database.getInstance().getAllLeagues();
for(League l : leagues)
System.out.println(l);
}
public List<League> getLeagues() {
return leagues;
}
public void setLeagues(List<League> leagues) {
this.leagues = leagues;
}
public int getSelectedLeagueId() {
return selectedLeagueId;
}
public void setSelectedLeagueId(int selectedLeagueId) {
this.selectedLeagueId = selectedLeagueId;
}
public ArrayList<Team> getTeams() {
return teams;
}
public void setTeams(ArrayList<Team> teams) {
this.teams = teams;
}
public String getTeamname() {
return teamname;
}
public void setTeamname(String teamname) {
this.teamname = teamname;
}
}
The league-class has an attribute id but if I output the value of selectedLeagueId, it is always 0.
Check if getAllLeagues() contains objects that have an id and that it is correctly set

jaxb XmlAdapter unmarshall an object from the same xml

take a look at this code :
#XmlRootElement
class Course {
private int id;
private String name;
public Course() {}
public Course(int id, String name) {
super();
this.id = id;
this.name = name;
}
#XmlAttribute(name = "id")
public int getId() {
return id;
}
#XmlAttribute(name = "name")
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
class CourseAdapter extends XmlAdapter<Integer, Course>{
#Override
public Course unmarshal(Integer v) throws Exception {
// what to do hereeeeeeeeeeeeeeee????!!!!
// I want the Course with the id = v unmarshalled from
// the same xml I am unmarshalling at the moment
}
#Override
public Integer marshal(Course v) throws Exception {
return v.getId();
}
}
#XmlRootElement
class Offering {
private int id;
private Course course;
private int capacity;
public Offering() {}
public Offering(int id, Course course, int capacity) {
super();
this.id = id;
this.course = course;
this.capacity = capacity;
}
#XmlAttribute(name = "id")
public int getId() {
return id;
}
#XmlJavaTypeAdapter(CourseAdapter.class)
#XmlAttribute(name = "course")
public Course getCourse() {
return course;
}
#XmlAttribute(name = "capacity")
public int getCapacity() {
return capacity;
}
public void setId(int id) {
this.id = id;
}
public void setCourse(Course course) {
this.course = course;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
}
#XmlRootElement
class Department {
private String name;
private ArrayList<Course> courses;
private ArrayList<Offering> offerings;
public Department(){}
public Department(String name, ArrayList<Course> courses, ArrayList<Offering> offerings) {
super();
this.name = name;
this.courses = courses;
this.offerings = offerings;
}
#XmlAttribute(name = "name")
public String getName() {
return name;
}
#XmlElement
public ArrayList<Course> getCourses() {
return courses;
}
#XmlElement
public ArrayList<Offering> getOfferings() {
return offerings;
}
public void setName(String name) {
this.name = name;
}
public void setCourses(ArrayList<Course> courses) {
this.courses = courses;
}
public void setOfferings(ArrayList<Offering> offerings) {
this.offerings = offerings;
}
}
I have marshalled my Department and this is the result:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<department name="ece">
<courses id="1" name="farsi"/>
<courses id="2" name="dini"/>
<courses id="2" name="riazi"/>
<offerings capacity="10" course="1" id="1"/>
<offerings capacity="20" course="2" id="2"/>
</department>
the problem is I do not know how to unmarshal course with the id = v from this very xml which is being unmarshalled via "unmarshal" function of CourseAdapter .
You can use #XmlID/#XmlIDREF for this use case without requiring an XmlAdapter.
http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html

Resources