XML을 출력하자

Java/Spring 2011.05.20 16:45
1. 설정파일을 셋팅해보자. 혼자 삽질하면서 만들었기 때문에 틀린 부분이 있다면 제보바란다.

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
  <display-name>RESTful</display-name>

<!-- Spring Dispatcher Servlet //-->
<servlet>
<servlet-name>spring3</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/WasSetting/spring/spring3-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring3</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- //Spring Dispatcher Servlet -->

</web-app> 
설정파일을 따로 관리해보기 위해 경로를 바꾸었다.

spring3-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        ">


<!-- Configures Spring MVC -->

<import resource="spring3mvc-config.xml" />

<!-- Configures Spring MVC -->

<import resource="rest-servlet.xml" />


</beans>

설정을 따로 하기 위해 import를 이용했다.

 spring3mvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-3.0.xsd

        ">


<!-- 패키지의 하위 bean 자동 등록 -->

<context:component-scan base-package="test" />


<!-- 뷰어를 연결 -->

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

<property name="viewResolvers">

   <list>

       <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

       <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

<property name="prefix" value="/WEB-INF/jsp/"/>

<property name="suffix" value=".jsp"/>

</bean>

   </list>

</property>

</bean>

</beans>

 
패키지 자동 등록의 경우에는 패키지가 커지면 로딩시간도 느려지므로 필요한 패키지를 따로 등록하면 로딩속도가 개선된다.

rest-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<!-- To enable @RequestMapping process on type level and method level -->

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />


<!-- xml autodectAnnotation set -->

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

  <property name="marshaller">

     <bean class="org.springframework.oxm.xstream.XStreamMarshaller">

        <property name="autodetectAnnotations" value="true"/>

     </bean>

  </property>

</bean>


</beans> 

xml을 화면에 뿌려주기 위한 설정이다.


2. 이제 클래스를 만들어보자. 테스트를 하기위해 DO와 Controller를 만들어보았다. test페키지에 만들자.

TestDO.java

@XStreamAlias("testdo")
public class TestDO {
private String name;
private int value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
} 

 

TestXmlController.java

@Controller
public class TestXmlController{

@RequestMapping(method=RequestMethod.GET,value = "/test/xml")
public ModelAndView newListXml(HttpServletRequest request) {
TestDO testdo = new TestDO();
testdo.setName("테스트 점수");
testdo.setValue(100);
List<TestDO> test = new ArrayList<TestDO>();
test.add(testdo);
return new ModelAndView("xmlClass", "testdo", test);
}

}


3. 출력된 결과를 보자

<list>
<testdo>
<name>테스트 점수</name>
<value>100</value>
</testdo>
</list> 


'Java > Spring' 카테고리의 다른 글

form으로 파일 전송  (0) 2012.06.28
spring + freemarker  (0) 2012.06.22
Spring 3.0 프로그래밍 챕터3  (0) 2011.05.31
Spring 3.0 프로그래밍 챕터2  (0) 2011.05.31
Spring 3.0 프로그래밍 챕터1  (0) 2011.05.23
XML을 출력하자  (0) 2011.05.20
Trackback 0 : Comment 0