• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

jsp tag file question

im creating a single tag file (not a tag library)

if i define an attribute in my tag file as required=true, what happens if someone uses my tag and completely leaves out that attribute?

also how can i make it so that if one attribute is specified as "false" when someone uses my tag it then requires another attribute to be defined but if its true then the attribute is not required.
0
wrynn
Asked:
wrynn
  • 2
  • 2
1 Solution
 
fargoCommented:
--> if i define an attribute in my tag file as required=true, what happens if someone uses my tag and completely leaves out that attribute?
required=true means the attribute must be defined (you can not leave it out)

For dependent attribute or attribute validation, you may use TEI tag extending the TagExtraInfo class. Have a look at the following link for "Attribute Validation"
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPTags5.html
0
 
rrzCommented:
>if i define an attribute in my tag file as required=true, what happens if someone uses my tag and completely leaves out that attribute?  
The page will not compile and send a mesage like this  
org.apache.jasper.JasperException: /test.jsp(2,0) According to the TLD or the tag file, attribute message is mandatory for tag testTag  

>also how can i make it so that if one attribute is specified as "false" when someone uses my tag it then requires another attribute to be defined but if its true then the attribute is not required.    
I came up with something close to your requirement.  Let the following code be  testTag.tag  
----------------------------------------------------------------------------------------------------------------------------
<%@ tag body-content="empty" import="javax.servlet.jsp.JspException"%>
<%@ attribute name="message1" required="false" %>
<%@ attribute name="message2" required="false" %>
Message1 is ${message1}!
Message2 is ${message2}!
<%
  if(message1 == null && message2 == null)
     throw new JspException("if the attribute message1 is not specified then attribute message2 is required in testTag");
%>
--------------------------------------------------------------------------------------------
and if  on a JSP  we have  the following  
<%@ taglib prefix="my" tagdir="/WEB-INF/tags/myTags" %>
<my:testTag/>  
then the compiler will compain and the page writer will be forced to give the attribute message2 if he did not give the attribute message1.      rrz
0
 
wrynnAuthor Commented:
thanks for the very nice idea rrz and thanks for the info fargo!!!
0
 
wrynnAuthor Commented:
oops i was gona try to split the points but i messed up sorry fargo
0
 
fargoCommented:
No problem. The explanation from rrz is better.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now