블로그 이미지

Rurony's Training Gym

Rurony의 트레이닝 도장! by Rurony


'OXM'에 해당되는 글 1건

  1. 2012.06.09 Spring JAXB를 활용한 Tree 구현

Spring JAXB를 활용한 Tree 구현

Spring OXM모듈의 JAXB를 활용한 Tree 구현 예제.

1. spring-oxm 라이브러리 추가 (maven dependency 사용)
jar download url : http://mvnrepository.com/artifact/org.springframework/spring-oxm/3.0.5.RELEASE


2. Spring Bean 설정

XML 응답을 위한 MarshallingView 등록하고 XML 출력을 위한 바운드 객체를 property로 주입.

<bean id="xmlViewer" class="org.springframework.web.servlet.view.xml.MarshallingView">

<constructor-arg>

<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">

<property name="classesToBeBound">

<list>

<value>com.rurony.format.common.tree.TreeNode</value>

</list>

</property>

</bean>

</constructor-arg>

</bean>


3. XML 출력을 위한 바운드 객체

XML 출력을 위해 JAXB annotation을 설정하고 Tree 구현을 위해 객체 내에 하위 객체를 포함하도록 구현.

@XmlRootElement(name="Tree")

@XmlAccessorType(XmlAccessType.FIELD)

public class TreeNode {

@XmlAttribute

private String option;

@XmlAttribute

private String status;

@XmlAttribute

private String name;

@XmlAttribute

private int depth;

@XmlAttribute

private int seq;

@XmlAttribute

private String type;

@XmlAttribute

private String parentId;

@XmlAttribute

private String id;

@XmlElement(name="Node")

protected List<TreeNode> nodes = new ArrayList<TreeNode>();

 

public TreeNode() {

}


public TreeNode(String id, String parentId, String type, int seq, String name, String status, String option) {

this.id = id;

this.parentId = parentId;

this.type = type;

this.seq = seq;

this.depth = 0;

this.name = name;

this.status = status;

this.option = option;

}


public void add(TreeNode node) {

if (id.equals(node.parentId)) {

node.depth = depth + 1;

nodes.add(node);

return;

}

Iterator<TreeNode> iter = nodes.iterator();

while (iter.hasNext()) {

iter.next().add(node);

}

}  


[... Setter/Getter ...]

public List<TreeNode> getNodes() {

return nodes;

}


public void setNodes(List<TreeNode> nodes) {

this.nodes = nodes;

}

}


4. MakeTreeNode Class

XML 바운드 객체을 사용하여 Tree XML을 구현하는 객체.

public class MakeTreeNode {

TreeNode treeNode;

public MakeTreeNode(String id, String parentId, String type, int seq, String name, String status, String option) {

treeNode = new TreeNode(id, parentId, type, seq, name, status, option);

}

public MakeTreeNode() {

treeNode = new TreeNode("0", "-1", "1", 1, "ROOT", "1", "");

}


public void add(TreeNode node) {

treeNode.add(node);

}


public void addAll(List<TreeNode> nodes) {

Iterator<TreeNode> iter = nodes.iterator();

while (iter.hasNext()) {

add(iter.next());

}

}

public TreeNode getTreeXmlNode() {

return treeNode;

}

} 


5. Test Tree Util

Domo Tree 추가

public static TreeNode getTestTreeXml() {

List<TreeNode> treeList = new ArrayList<TreeNode>();

treeList.add(new TreeNode("1", "0", "type", 1, "Node 1", "status", "option"));

treeList.add(new TreeNode("2", "1", "type", 1, "Node 1-1", "status", "option"));

treeList.add(new TreeNode("3", "1", "type", 2, "Node 1-2", "status", "option"));

treeList.add(new TreeNode("4", "0", "type", 2, "Node 2", "status", "option"));

treeList.add(new TreeNode("5", "4", "type", 1, "Node 2-1", "status", "option"));

treeList.add(new TreeNode("6", "4", "type", 2, "Node 2-2", "status", "option"));

treeList.add(new TreeNode("7", "5", "type", 1, "Node 2-1-1", "status", "option"));

treeList.add(new TreeNode("8", "5", "type", 2, "Node 2-1-2", "status", "option"));

treeList.add(new TreeNode("9", "0", "type", 3, "Node 3", "status", "option"));

treeList.add(new TreeNode("10", "9", "type", 1, "Node 3-1", "status", "option"));

MakeTreeNode tree = new MakeTreeNode("0", "-1", "1", 1, "Tree Root", "status", "option");

tree.addAll(treeList);

return tree.getTreeXmlNode();

}


6. Controller

@ResponseBody annotation을 사용하여 Url Mapping.

@RequestMapping("/testTreeXML")

public @ResponseBody TreeNode treeXML() {

return MakeTreeUtil.getTestTreeXml();

}


7. Domo Page

Top

prev 1 next