2

Before I get started with my question, here are my unfortunate limitations:

  1. I'm using JSF 1.2, not 2; so no composite component.
  2. I'm using JSP for rendering instead of facelets; so none of those composite components either.
  3. I'm not allowed to use any 3rd-party tag libraries (richFaces, iceFaces, etc.)

These limitations are set in stone.

Now moving onto my question. Currently, we have a JSP subview which handles creating an address. There is a lot of javascript that goes along with this, along with a backing bean. This page is never used directly. Instead, it's included using a <jsp:include />.

However, there are several attributes that I want to be able to change. For instance, is county required, are we currently doing address scrubbing, etc. In order to do this, it would make sense to use a custom component (I think?). However, I'm not really sure the best way to do this.

If I could, I would simply turn this JSP into a composite component and be done with it. However, that's not really an option based on my limitations.

What are my options? This wouldn't be so difficult if it weren't for the amount of javascript involved. I know my explanation was vague; however, I'm looking more for guidance than a direct answer. I've googled for things such as custom JSF 1.x components with javascript, etc. I haven't found many good articles, however.

Thanks in advance.

Zack Marrapese
  • 12,072
  • 9
  • 51
  • 69
  • 2
    Sounds like they expect you to pick fly poop out of pepper while wearing boxing gloves. Been there. It sounds like you want to add custom validations on form fields. The real question is, why does a `jsp:include` forgo this? What will a custom component bring to the table other than your ability to learn how to write a custom component under the JSF1.2 spec which IMHO is broken? – maple_shaft Feb 24 '12 at 18:18
  • If it were only so easy! No validation is done using JSF validators. Not even defining the required attribute. They don't want to change that. There are many other customizations that need done beyond validation. For example, each address can be attached to a different contact whose contactId (contact table's PK) needs defined up front. There's also display logic: can this address be a legal address -- one defined using lat/long -- instead of a mailing address. – Zack Marrapese Feb 24 '12 at 18:31
  • @Zack... I just noticed that you are a fellow Yinzer, meet me in chat if you get a chance, I will create a room for us. I would like to talk through this. – maple_shaft Feb 24 '12 at 18:54
  • I created a room called Zack and Maple_shaft room. I will hang out there for the next couple hours and check in to see if you show up. – maple_shaft Feb 24 '12 at 19:00

1 Answers1

2

Create a JSP tag file.

/WEB-INF/tags/foo.tag

<%@ tag body-content="empty" %>
<%@ attribute name="countryRequired" required="false" type="java.lang.Boolean" %>
<%@ attribute name="showAddress" required="false" type="java.lang.Boolean" %>

<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<h:panelGrid columns="2">
    <h:outputLabel for="country" value="Country" />
    <h:inputText id="country" value="#{bean.country}" required="${countryRequired}" />

    <c:if test="${showAddress}">
        <h:outputLabel for="address" value="Address" />
        <h:inputText id="address" value="#{bean.address}" />
    </c:if>
</h:panelGrid>

Declare and use it as follows (no additional XML configuration necessary):

<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
...
<my:foo showAddress="true" />

Note that JSTL is also here a "view build time" tag like as in Facelets. Also note that you can't use #{} to reference JSP tag attributes.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555