Velocity string function

后端 未结 4 817
臣服心动
臣服心动 2020-12-30 19:52

I just start using Java Velocity. Now I want to create a java class template.

package $app.package_namespace
public class ${app.name}Station

{
    #foreac         


        
相关标签:
4条回答
  • 2020-12-30 20:21

    There is capitalize() method in DisplayTool:

    get${display.capitalize($s_attribute.name)}()
    
    0 讨论(0)
  • 2020-12-30 20:36

    You can invoke standard java methods on these objects. If s_attribute.name is type String you can directly use $s_attribute.name.toUpperCase() or for your specific case use $s_attribute.name.substring(0,1).toUpperCase() and $s_attribute.name.substring(1).toLowerCase()

    0 讨论(0)
  • 2020-12-30 20:36

    You could just create 2 methods getName() and getname() then when you use ${s_attribute.name} velocity will use getname() and when you use ${s_attribute.Name} velocity will use the getName() method.

    From the Velocity guide:

    Property Lookup Rules

    As was mentioned earlier, properties often refer to methods of the parent object. Velocity is quite clever when figuring out which method corresponds to a requested property. It tries out different alternatives based on several established naming conventions. The exact lookup sequence depends on whether or not the property name starts with an upper-case letter. For lower-case names, such as $customer.address, the sequence is

    getaddress()
    getAddress()
    get("address")
    isAddress()
    

    For upper-case property names like $customer.Address, it is slightly different:

    getAddress()
    getaddress()
    get("Address")
    isAddress()
    

    What i'm suggesting is that you handle it in your object on the backend.

    0 讨论(0)
  • 2020-12-30 20:37

    If you are using commons-lang you can use the StringUtils class:

    context.put("StringUtils", org.apache.commons.lang3.StringUtils.class);
    

    Then in your template:

    ...
    return  get$StringUtils.capitalize(s_attribute.name)();
    ...
    
    0 讨论(0)
提交回复
热议问题