i wrote custom tag which makes it easy to localize strings;
in jsp it looks like this:
<ct:word key="${message}"/>
message passed from servlet.
This tag takes needed string from the ResourceBundle.
Everything works but there is a problem. If i didn’t pass message from servlet then my app throw Exception(ResourceBundle can not find the necessary string).
How to ensure that custom tag did not respond to null and skipped? like it does
<c:out />
code im my custom tag:
private String key;
private String value;
public void setKey(String key) {
this.key = key;
}
public String getKey() {
return this.key;
}
public int doStartTag() {
try {
this.checkLocale();//check locale and init resourceBundle
value = resourceBundle.getString(key);
pageContext.getOut().write(value);
} catch (IOException e) {
logger.error(e);
} catch (MyException e1) {
logger.error(e1);
}
return SKIP_BODY;
}
I don’t want to use
<c:if test="${not empty message}"/> or <c:when/>
it’s clutters the code on jsp
UPDATED:
i try
} catch (MyException e1) {
logger.error(e1);
return SKIP_BODY;
}
but i have Exception:
java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key
in my methods i catch
catch (MissingResourceException e) {
throw new MyException(Constants.ERROR_TRANSLATE_TAG,e);
}
my app issues this exception ONLY if i I did not pass this message from servlet. I want to display page even if i dont have this message, without error pages.
For the record:
resourceBundle.getString(key);will throw aMissingResourceExceptionif the key cannot be found, thus catch that exception as well in thedoStartTag()method.