Kleber Mota
Kleber Mota

Reputation: 9095

Templates for JSP pages

I am looking for a way for create a template for jsp pages with a similar behavior of the genereic class from Java?

What I have now: in my folder of views (WEB-INF/view/jsp), I have a list of directories with the same fours files, with receive names of his functons (like, new item, change item, remove item, list).

In each one of this groups, the jsp pages save ou read data from one specific Entity class from my project.

For my Dao classes, for example, I have something like that:

Generic class

public class Dao<E> {

    private final Class<E> entity;

    @Autowired
    protected SessionFactory sessionFactory;

    protected Session getCurrentSession(){
        return sessionFactory.getCurrentSession();
    }

    public Dao(Class<E> classe) {
        this.entity = classe;
    }

    public boolean remove(E transientInstance) {
        ...
    }

    ...
}

and this way, each Dao class have a structure similar to this:

@Repository
public class FornecedorDao extends Dao<Fornecedor> {

    public FornecedorDao() {
        super(Fornecedor.class);
    }

}

Is there any to acomplish something similar to that for jsp pages? (I prefer a solution which don't need add to my project other framework, besides Spring).

UPDATE

Explaining better each one of the pages from each folder:

new_item.jsp -> it's just a form from where I submit the data to server. each field of the form is a atribute from my Entity class.

change_item.jsp -> same as new_item.jsp, plus a hidden field with the atribute id (primary key) of the class.

remove_item.jsp -> have a form with only one field: a hidden one with the id of the element to be removed.

list.jsp -> receives a list of items and generate a table where each column is a atribute from my entity class.

what I wish acomplish, by example, it's something like this:

in the template, I will have the html code for the form or the table. this pages have no style: a jquery code read them, strip the title and body content, and add this data to a div in my jsp page dashboard.jsp. in each one of the page, i like to tell only the name of the entity class I will use and perharps the atributes, and based on the template, will be created the page to be delivered to browser.

Upvotes: 2

Views: 1480

Answers (3)

norbitheeviljester
norbitheeviljester

Reputation: 962

The closest thing you can get is the JSP include tag:

<jsp:include page="new_item.jsp">
    <jsp:param name="formaction" value="newItemServlet"/>
    <jsp:param name="entityparams" value="{params:[param1,param2,param3]}"/>
</jsp:include>

Which will allow you to pass parameters to JSP templates. These parameters can be then parsed using JSP and javascript which will render custom form fields.

Upvotes: 1

geoand
geoand

Reputation: 64099

If you want to stick with JSP, you should check out Apache Tiles for a reasonable templating solution.

If however you are willing to move on to something else, you should check out Velocity, Freemarker or Thymeleaf.

I have personally worked with Thymeleaf and can say that you can create layouts very easily which can then be "implemented" by specific pages or "extended" by more specialized layouts

All three of the aforementioned templating engines can easily be configured in Spring as the view resolvers

Upvotes: 1

Dima
Dima

Reputation: 1374

I assume you mean to benefit from reuse of templates in multiple pages, right? Many technologies provide a solution to this or that extend. The analogy with class inheritance implies a top-down development with next pages being more specialized. Effectively in the world of templates Tiles and alike technologies provide support to what is called "layout" - a sub-template that defines positions for insertion of more markup.

This feature is only one of features of modern markup languages. In particular it may be found in JSF. See also HybridJava.

Upvotes: 1

Related Questions