2013-05-22 3 views
1

struts2-jaxb-plugin과 같은 플러그인이 버전 2.0.x보다 높은 struts2 버전에서 작동합니까?struts2 버전 2.0.x 이상을위한 struts2 및 jaxb

최신 버전의 struts2에는 com.opensymphony.xwork2.ActionContext 클래스의 get (Object o)이 더 이상 없습니다.

struts2를 사용하여 xml 결과를 얻는 더 좋은 방법이 있다면 올바른 방향으로 나를 가리켜 주십시오.

그렇지 않으면 struts2-jaxb-plugins에서 발생한 것과 같이 자체적 인 마샬링 인터셉터와 jaxb 결과 유형을 작성하는 방법을 생각하고 있습니다.

현재 버전 :

  • struts2 : 2.3.14
  • JAXB-API :

답변

1

2.2.9 그냥 내 자신의 JAXB 결과 형을 썼다. 내가 생각했던 것보다 쉬웠다. 비슷한 뭔가를 찾고 그 아래를 떠나

:

<result-types> 
    <result-type name="jaxb" class="JaxbResult" /> 
</result-types> 

<action name="testaction" class="TestAction"> 
    <result name="success" type="jaxb" >jaxbObject</result> 
</action> 
:

import java.io.IOException; 
import java.io.Writer; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import org.apache.struts2.ServletActionContext; 

import com.opensymphony.xwork2.ActionInvocation; 
import com.opensymphony.xwork2.Result; 
import com.opensymphony.xwork2.util.ValueStack; 

public class JaxbResult implements Result { 
    private static final long serialVersionUID = -5195778806711911088L; 
    public static final String DEFAULT_PARAM = "jaxbObjectName"; 

    private String jaxbObjectName; 

    public void execute(ActionInvocation invocation) throws Exception { 
     Object jaxbObject = getJaxbObject(invocation); 
     Marshaller jaxbMarshaller = getJaxbMarshaller(jaxbObject); 
     Writer responseWriter = getWriter(); 

     setContentType(); 
     jaxbMarshaller.marshal(jaxbObject, responseWriter); 
    } 

    private Writer getWriter() throws IOException { 
     return ServletActionContext.getResponse().getWriter(); 
    } 

    private Marshaller getJaxbMarshaller(Object jaxbObject) throws JAXBException { 
     JAXBContext jaxbContext = JAXBContext.newInstance(jaxbObject.getClass()); 
     Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
     jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

     return jaxbMarshaller; 
    } 

    private Object getJaxbObject(ActionInvocation invocation) { 
     ValueStack valueStack = invocation.getStack(); 

     return valueStack.findValue(getJaxbObjectName()); 
    } 

    private void setContentType() { 
     ServletActionContext.getResponse().setContentType("text/xml"); 
    } 

    public String getJaxbObjectName() { 
     return jaxbObjectName; 
    } 

    public void setJaxbObjectName(String jaxbObjectName) { 
     this.jaxbObjectName = jaxbObjectName; 
    } 
} 

스트럿 - XML의 구성은 다음과 같이 보입니다