• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to set maxlength property dynamically in Struts html:text tag?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I have a problem using Struts <html:text> tag.

How do I set maxlength property dynamically?

For example:

<html:text name="myForm" property="firstName" size="25" maxlength="30" />

I tried the following three ways:
(1) use <%= %>, it won't work.

import MyClass;
....
<html:text name="myForm" property="firstName" size="25" maxlength="<%=MyClass.FIRSTNAME_MAX_LENGTH%>" />

(2) Use <bean:message> tags, it won't work.
<html:text name="myForm" property="firstName" size="25" maxlength="<bean:message key="myPage.maxlength.firstnameMaxlength"/>" />

(3) Use <bean:write> tag, it also won't work.
<html:text name="myForm" property="firstName" size="25" maxlength="<bean:write name=\"myForm\" property="firstnameMaxLength"/>"/>

Any ideas?

Thank you!

Kate
 
Sheriff
Posts: 17648
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Struts documentation shows that the maxlength attribute can be a runtime expression so the first method should work. Try using the fully qualified name of MyClass:

maxlength=<%=com.foo.MyClass.MAXLENGTH %>
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup. First method should work. I use it all the time.

I can't think of what might have gone wrong (other than the already stated). Maybe the constant is an int and it doesn't like it? If so, change it to a String or do the following:

<html:text name="myForm" property="firstName" size="25"
maxlength='<%= MyClass.FIRSTNAME_MAX_LENGTH + "" %>' />
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic