0

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!

Ock
  • 1,262
  • 3
  • 18
  • 38

1 Answers1

0

We did it like this:

First, ressourcebundle backed by a .properties file was created, containing all the translations:

example, we're using eclipse IDE

Now, how you proceed from here depends on your infrastructure and what you do with your application. What we did was introduce a language service (One global, one session-based) which gets these translations from the generated ressourcebundle, like so:

public class GlobalLanguageServiceImpl implements LanguageService {

    @Autowired
    ResourceBundleMessageSource MessageSource;

    ...

    public String getMessage(String key)
    {
        return MessageSource.getMessage(key, null, getLocale());
    }
}

Finally, it was used in our xhtml files like this:

<p:panel header="#{lang['menu.administration.config']}">

Hope that helps you in the right direction. You can of course substitute my properties file with a database solution, thought it may help performance-wise to generate a ressource bundle (more info about ressource bundles here and here)

Wep0n
  • 392
  • 3
  • 15
  • Hi Wep0n, thanks for your hint. I see that you use Spring framework here, don't you? But I still didn't really get the connection between your LanguageService and your user interface xhtml file, if possible can you please explain me a little bit about the workflow of your application. Thank you so much – Ock Oct 05 '17 at 13:32
  • Yes, I am using Spring, registering a service (such as my languageservice) is something that is mostly handled by this framework as well. (Info about that here: https://spring.io/guides/gs/service-registration-and-discovery/ ) - it's available throughout the application (depending on the scope) at startup or through the @Autowired annotation. I'm not sure if JSP/Servlets have a similar feature. However, look at this: http://www.wideskills.com/jsp/jsp-internationalization - it contains sections about which tags for JSP you need to use to retrieve from a RessourceBundle (26.4.3 / ) – Wep0n Oct 06 '17 at 05:32
  • I haven't looked into it yet but I wouldn't imagine that generating a RessourceBundle from a database (perhaps at application startup?) is very difficult. I found this on the topic: https://stackoverflow.com/a/2213421/7591918 - if you just slap that into a loop over your database you should be all fine, also check out the related article :) – Wep0n Oct 06 '17 at 05:36