I am creating now web application using following technologies: JSP, Servlet, Hibernate, MySQL. What I am doing now is making a kind of "dictionary" for all of the labels in my application, so that in the JSP/HTML pages, it is not needed to type the labels manually, instead its values will be loaded from this dictionary, then we will have a seamless application, avoid the case that forgetting the comma, wrong spelling...or we can say this dictionary acts like a template for all of the labels.
My idea now is storing the dictionary in the database:
package com.jwt.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Dictionary_TABLE")
public class Dictionary implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String default_key;
private String value;
public Dictionary() {
}
public Dictionary(String default_key, String value) {
super();
this.default_key = default_key;
this.value = value;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDefault_key() {
return default_key;
}
public void setDefault_key(String default_key) {
this.default_key = default_key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Dictionary [id=" + id + ", default_key=" + default_key + ", value=" + value + "]";
}
}
And when the request comes to the Servlet, I will retrieve all of the labels from the database, then I will have List of label objects:
public List<Dictionary> getListOfDictionary(){
List<Dictionary> list = new ArrayList<Dictionary>();
Session session = HibernateUtil.openSession();
Transaction tx = null;
try {
tx = session.getTransaction();
tx.begin();
list = session.createQuery("from Dictionary").list();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return list;
}
But I am stucking now at the step to bind these labels to the JSP page. I have read in the internet that one way I can do is setting the attribute and send it to the JSP page, and in the JSP page I can call something like: <div>${label1}</div>
. But right now I have a list of label objects, not single key-pair value like in this example. So can anybody give me a hints to solve this problem? Thank you!