JSP tricks to make templating easier?

前端 未结 7 686
猫巷女王i
猫巷女王i 2020-11-22 08:41

At work I\'ve been tasked with turning a bunch of HTML files into a simple JSP project. It\'s really all static, no serverside logic to program. I

相关标签:
7条回答
  • 2020-11-22 09:18

    Add dependecies for use <%@tag description="User Page template" pageEncoding="UTF-8"%>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
    </dependencies>
    
    0 讨论(0)
  • 2020-11-22 09:20

    As skaffman suggested, JSP 2.0 Tag Files are the bee's knees.

    Let's take your simple example.

    Put the following in WEB-INF/tags/wrapper.tag

    <%@tag description="Simple Wrapper Tag" pageEncoding="UTF-8"%>
    <html><body>
      <jsp:doBody/>
    </body></html>
    

    Now in your example.jsp page:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
    
    <t:wrapper>
        <h1>Welcome</h1>
    </t:wrapper>
    

    That does exactly what you think it does.


    So, lets expand upon that to something a bit more general. WEB-INF/tags/genericpage.tag

    <%@tag description="Overall Page template" pageEncoding="UTF-8"%>
    <%@attribute name="header" fragment="true" %>
    <%@attribute name="footer" fragment="true" %>
    <html>
      <body>
        <div id="pageheader">
          <jsp:invoke fragment="header"/>
        </div>
        <div id="body">
          <jsp:doBody/>
        </div>
        <div id="pagefooter">
          <jsp:invoke fragment="footer"/>
        </div>
      </body>
    </html>
    

    To use this:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
    
    <t:genericpage>
        <jsp:attribute name="header">
          <h1>Welcome</h1>
        </jsp:attribute>
        <jsp:attribute name="footer">
          <p id="copyright">Copyright 1927, Future Bits When There Be Bits Inc.</p>
        </jsp:attribute>
        <jsp:body>
            <p>Hi I'm the heart of the message</p>
        </jsp:body>
    </t:genericpage>
    

    What does that buy you? A lot really, but it gets even better...


    WEB-INF/tags/userpage.tag

    <%@tag description="User Page template" pageEncoding="UTF-8"%>
    <%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
    <%@attribute name="userName" required="true"%>
    
    <t:genericpage>
        <jsp:attribute name="header">
          <h1>Welcome ${userName}</h1>
        </jsp:attribute>
        <jsp:attribute name="footer">
          <p id="copyright">Copyright 1927, Future Bits When There Be Bits Inc.</p>
        </jsp:attribute>
        <jsp:body>
            <jsp:doBody/>
        </jsp:body>
    </t:genericpage>
    

    To use this: (assume we have a user variable in the request)

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
    
    <t:userpage userName="${user.fullName}">
      <p>
        First Name: ${user.firstName} <br/>
        Last Name: ${user.lastName} <br/>
        Phone: ${user.phone}<br/>
      </p>
    </t:userpage>
    

    But it turns you like to use that user detail block in other places. So, we'll refactor it. WEB-INF/tags/userdetail.tag

    <%@tag description="User Page template" pageEncoding="UTF-8"%>
    <%@tag import="com.example.User" %>
    <%@attribute name="user" required="true" type="com.example.User"%>
    
    First Name: ${user.firstName} <br/>
    Last Name: ${user.lastName} <br/>
    Phone: ${user.phone}<br/>
    

    Now the previous example becomes:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
    
    <t:userpage userName="${user.fullName}">
      <p>
        <t:userdetail user="${user}"/>
      </p>
    </t:userpage>
    

    The beauty of JSP Tag files is that it lets you basically tag generic markup and then refactor it to your heart's content.

    JSP Tag Files have pretty much usurped things like Tiles etc., at least for me. I find them much easier to use as the only structure is what you give it, nothing preconceived. Plus you can use JSP tag files for other things (like the user detail fragment above).

    Here's an example that is similar to DisplayTag that I've done, but this is all done with Tag Files (and the Stripes framework, that's the s: tags..). This results in a table of rows, alternating colors, page navigation, etc:

    <t:table items="${actionBean.customerList}" var="obj" css_class="display">
      <t:col css_class="checkboxcol">
        <s:checkbox name="customerIds" value="${obj.customerId}"
                    onclick="handleCheckboxRangeSelection(this, event);"/>
      </t:col>
      <t:col name="customerId" title="ID"/>
      <t:col name="firstName" title="First Name"/>
      <t:col name="lastName" title="Last Name"/>
      <t:col>
        <s:link href="/Customer.action" event="preEdit">
          Edit
          <s:param name="customer.customerId" value="${obj.customerId}"/>
          <s:param name="page" value="${actionBean.page}"/>
        </s:link>
      </t:col>
    </t:table>
    

    Of course the tags work with the JSTL tags (like c:if, etc.). The only thing you can't do within the body of a tag file tag is add Java scriptlet code, but this isn't as much of a limitation as you might think. If I need scriptlet stuff, I just put the logic in to a tag and drop the tag in. Easy.

    So, tag files can be pretty much whatever you want them to be. At the most basic level, it's simple cut and paste refactoring. Grab a chunk of layout, cut it out, do some simple parameterization, and replace it with a tag invocation.

    At a higher level, you can do sophisticated things like this table tag I have here.

    0 讨论(0)
  • 2020-11-22 09:20

    This can also be achieved with jsp:include. Chad Darby explains well here in this video https://www.youtube.com/watch?v=EWbYj0qoNHo

    0 讨论(0)
  • 2020-11-22 09:23

    Use tiles. It saved my life.

    But if you can't, there's the include tag, making it similar to php.

    The body tag might not actually do what you need it to, unless you have super simple content. The body tag is used to define the body of a specified element. Take a look at this example:

    <jsp:element name="${content.headerName}"   
       xmlns:jsp="http://java.sun.com/JSP/Page">    
       <jsp:attribute name="lang">${content.lang}</jsp:attribute>   
       <jsp:body>${content.body}</jsp:body> 
    </jsp:element>
    

    You specify the element name, any attributes that element might have ("lang" in this case), and then the text that goes in it--the body. So if

    • content.headerName = h1,
    • content.lang = fr, and
    • content.body = Heading in French

    Then the output would be

    <h1 lang="fr">Heading in French</h1>
    
    0 讨论(0)
  • 2020-11-22 09:29

    I made quite easy, Django style JSP Template inheritance tag library. https://github.com/kwon37xi/jsp-template-inheritance

    I think it make easy to manage layouts without learning curve.

    example code :

    base.jsp : layout

    <%@page contentType="text/html; charset=UTF-8" %>
    <%@ taglib uri="http://kwonnam.pe.kr/jsp/template-inheritance" prefix="layout"%>
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>JSP Template Inheritance</title>
        </head>
    
    <h1>Head</h1>
    <div>
        <layout:block name="header">
            header
        </layout:block>
    </div>
    
    <h1>Contents</h1>
    <div>
        <p>
        <layout:block name="contents">
            <h2>Contents will be placed under this h2</h2>
        </layout:block>
        </p>
    </div>
    
    <div class="footer">
        <hr />
        <a href="https://github.com/kwon37xi/jsp-template-inheritance">jsp template inheritance example</a>
    </div>
    </html>
    

    view.jsp : contents

    <%@page contentType="text/html; charset=UTF-8" %>
    <%@ taglib uri="http://kwonnam.pe.kr/jsp/template-inheritance" prefix="layout"%>
    <layout:extends name="base.jsp">
        <layout:put name="header" type="REPLACE">
            <h2>This is an example about layout management with JSP Template Inheritance</h2>
        </layout:put>
        <layout:put name="contents">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin porta,
            augue ut ornare sagittis, diam libero facilisis augue, quis accumsan enim velit a mauris.
        </layout:put>
    </layout:extends>
    
    0 讨论(0)
  • 2020-11-22 09:29

    I know this answer is coming years after the fact and there is already a great JSP answer by Will Hartung, but there is Facelets, they are even mentioned in the answers from the linked question in the original question.

    Facelets SO tag description

    Facelets is an XML-based view technology for the JavaServer Faces framework. Designed specifically for JSF, Facelets is intended to be a simpler and more powerful alternative to JSP-based views. Initially a separate project, the technology was standardized as part of JSF 2.0 and Java-EE 6 and has deprecated JSP. Almost all JSF 2.0 targeted component libraries do not support JSP anymore, but only Facelets.

    Sadly the best plain tutorial description I found was on Wikipedia and not a tutorial site. In fact the section describing templates even does along the lines of what the original question was asking for.

    Due to the fact that Java-EE 6 has deprecated JSP I would recommend going with Facelets despite the fact that it looks like there might be more required for little to no gain over JSP.

    0 讨论(0)
提交回复
热议问题