简体   繁体   中英

Create reusable GUI components in Spring

I have a project using Spring MVC and I recently added the use of Apache Tiles on my pages. But I a model Book I have to display on a lot of different pages. Can I define a Book GUI Component somehow in Spring . I would like to have something like this:

A GUI Component I can define as such: book.viewcomponent

<h1>${book.title}</h1>
<span>${book.summary}</span>

A first page using this component by passing a model object booklist.jsp

<!--some import to let me insert with prefix:"insert"-->
<html>
<head>...</head>
<body>
<insert page="book.jsp" model="${book}">
</body>
</html>

A second a page using this component by passing a model object morebooks.jsp

<!--some import to let me insert with prefix:"insert"-->
<html>
<head>...</head>
<body>
<insert page="book.jsp" model="${book}">
</body>
</html>

I think you could do this with includes or tags. With includes:

<!-- There's a book in the request -->
<jsp:include page="book.jsp" />

In this case, if you have things like ${book.title} inside your book.jsp file, then there must be a Book object called "book" in the request prior to including your fragment.

With tags , it's a little more formal. You need to write your tag in a file, along with its parameters definition. The following would be your book.tagx tag inside, for example, WEB-INF/tags/mytags :

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
  <jsp:directive.attribute name="book" type="your.package.Book" required="true" />
  <h1>${book.title}</h1>
  <span>${book.summary}</span>
</jsp:root>

You can then import this tag adding the namespace referring to the location where you saved it:

xmlns:mytags="urn:jsptagdir:/WEB-INF/tags/mytags"

and finally use it:

<mytags:book book="${book}" />

As you can see, here the parameter is passed to the tag, instead of being passed implicitly in the request.

More info on creating tags: https://docs.oracle.com/cd/E19316-01/819-3669/6n5sg7b5m/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM