This page contains FAQ on Struts, Struts-EL, JSTL and Ajax tags. The separation between these categories is rather fluid, because it is possible to mix and match tags, for example to use JSTL tags or scriptlets within Struts tags. Therefore, please look in all categories.

Struts tags

Using the <html:link> tag for multiple, dynamic parameters

Well, there seems to be a lot of frustration and discussion about this "standard" requirement in web applications. Most web applications want to use multiple, dynamic url parameters in their links, but currently it seems like the only official way breaks with anti-scriptlet rule.

For a discussion, please read:

http://www.jguru.com/faq/view.jsp?EID=877089

Here comes the solution with JSTL...

<c:url value="/myAction.do" var="url">
<c:param name="param1" value="${user.fullName}"/>
<c:param name="param2" value="${'parameter 2'}"/>
</c:url>
<a href="${url}">Click</a>

NPE when accessing value using scriptlet

Q: I use the following code in a jsp:

 <logic:present name="person">
   <html:text property="name" " value='<%= person.getName() %>'/>
 </logic:present>

If 'person' is not present <%= person.getName() %> is compiled and I get this error: membreLIFL cannot be resolved

If i use this code:

 <logic:present name="person">
   <input type="text" name="name" 
          value="<bean:write name="person" property="name"/>"
   />
 </logic:present>

No problem. How can i use the html:text tag?

A: JSP scriptlet is a regular Java code, which calls a method on "person" object. If "person" object is not defined, an NPE is generated.

Choice 1: If you use JSP 2.3 + Struts-EL or JSP 2.4, you can use the following syntax to access "name" property:

 <html-el:text property="name" value="${person.name}" />

JSTL does not throw NPE if you accessing a property of non-existing object.

Choice 2: If a Struts tag defines "value" attribute, it is possible to use either this attribute or text element of a tag. The following definitions are equivalent:

 <html:text property="name" value="someData"/>

and

 <html:text property="name">
   someData
 </html:text>

Struts-EL tags

JSTL tags

Comparison using nested tag vs JSTL for nested objects on form

Many think that you are required to display nested data using the Nested tag. I agree the Nested tag is the cleaner solution, but just so you are aware you can do this with JSTL as well. (Below assumes JSP2.0)

companyForm has a List of divisions and for each division in the list there is a list of department objects and we will display the 'name' of each department.

JSTL Solution

<c:forEach items="${companyForm.divisions}" var="division" varStatus="divstatus">
    Division: <html:text property="divisions[${divstatus.index}].name" value="${division.name}" /><br>
    <c:forEach items="${division.departments}" var="department" varStatus="depstatus">
    --- Department: <html:text property="divisions[${divstatus.index}].departments[${depstatus.index}].name" value="${department.name}" /><br>
    </c:forEach>
</c:forEach>

Nested Tag Solution

<nested:root name="companyForm">
  <nested:iterate property="divisions">
    Division: <nested:text property="name" /><br>
        <nested:iterate property="departments">
        --- Department: <nested:text property="name" /><br>
        </nested:iterate>
  </nested:iterate>
</nested:root>

JSP scriptlets

Ajax Tags

AjaxTags is an enhanced version of the standard Struts HTML taglibs that provides an easy way to add AJAX functionality to an existing struts app through a completely declarative approach (read: it's all an XML config file). This enhanced taglib provides 100% compatibility with the normal HTML taglib and in fact requires NO changes to existing pages UNTIL you want to add AJAX functions.

The original AjaxTags project, which is still available on the Struts Apps SourceForge page at http://struts.sourceforge.net is **DEPRECATED** in favor of the AjaxTags subcomponent of the Java Web Parts (JWP) project at http://javawebparts.sourceforge.net. AjaxTags in JWP is a much more powerful and flexible version of the original concept and IS NOT STRUTS-SPECIFIC like the original was. While the original version is still available, it is no longer being developed and will likely NOT work with future Struts versions.

It is HIGHLY recommended that the AjaxTags in JWP be used if this is something you are interested in as you will definitely find it more feature-rich and just as easy to use, and it IS being supported and further developed (I will still support the original version as much as I can, but at a lower priority than the version in JWP).

  • No labels