• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 17734
302
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 + "" %>' />
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic