2012-09-04 41 views
1

我的一個集成客戶端提供具有屬性名稱「_1」,「_2」...等的XML 使用JAXB生成類使用getter方法訪問數字屬性的表達式

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

,屬性的getter方法將GET1()和get2()

但是在我的JSP頁面,使用JSTL和EL,確保我無法訪問值通過

${variable.1} 

如何正確使用EL訪問值?

+0

你試圖訪問通過'variable.1'價值? – sp00m

+0

當然,JSP不會編譯錯誤$ {variable.1}包含無效表達式: – Ivan

回答

0

使用這個符號:

${variable.["1"]} 
1

你可以使用一個外部綁定文件重命名的屬性生成的JAXB:

schema.xsd

下面是基於一個示例XML模式對你的帖子:

<?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

外部綁定文件用於定製如何從XML模式生成Java類。下面我們將使用外部綁定文件來重命名生成的屬性。

<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呼叫

下面是使用XJC工具時,您如何引用綁定文件的例子。

xjc -b binding.xml schema.xsd 

部件1

下面是生成的類將是什麼樣子:

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; 
    } 

}