1

我的一个集成客户提供了一个带有属性名称“_1”、“_2”......等的XML。例如

<element _1="attr1" _2="attr2">

使用 JAXB 生成类,属性的 getter 方法将是 get1() 和 get2()

但是在我的 JSP 页面中,使用 JSTL 和 EL,确保我无法通过

${variable.1}

如何正确使用 EL 访问值?

4

2 回答 2

1

You could use an external binding file to rename the property generate by JAXB:

schema.xsd

Below is a sample XML schema based on your post:

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org"
    xmlns:tns="http://www.example.org" 
    elementFormDefault="qualified">
    <element name="element1">
        <complexType>
            <attribute name="_1" type="string" />
            <attribute name="_2" type="string" />
        </complexType>
    </element>
</schema>

binding.xml

An external binding file is used to customize how Java classes are generated from the XML schema. Below we'll use an external binding file to rename the generated properties.

<jaxb:bindings
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jaxb:bindings schemaLocation="schema.xsd">
        <jaxb:bindings node="//xsd:attribute[@name='_1']">
            <jaxb:property name="one"/>
        </jaxb:bindings>
        <jaxb:bindings node="//xsd:attribute[@name='_2']">
            <jaxb:property name="two"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

XJC Call

Below is an example of how you reference the binding file when using the XJC tool.

xjc -b binding.xml schema.xsd

Element1

Below is what the generated class will look like:

package forum12259754;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "element1")
public class Element1 {

    @XmlAttribute(name = "_1")
    protected String one;
    @XmlAttribute(name = "_2")
    protected String two;

    public String getOne() {
        return one;
    }

    public void setOne(String value) {
        this.one = value;
    }

    public String getTwo() {
        return two;
    }


    public void setTwo(String value) {
        this.two = value;
    }

}
于 2012-09-04T09:43:20.823 回答
0

使用这个符号:

${variable.["1"]}
于 2012-09-04T09:30:38.617 回答