2009-08-09 20 views
5

Desidero inviare String come risposta al metodo xhrPOST di AJAX. Sto usando Struts2 per implementare l'elaborazione lato server. Ma non capisco come inviare il risultato "type" come stringa e il mapping che dovrebbe essere fatto per inviare la stringa dalla classe action struts2 alla risposta AJAX.Tipo di risultato stringa di ritorno da Struts2

risposta

2

È possibile creare facilmente un semplice StringResult estendendo StrutsResultSupport, ma per quanto ne so non esiste nulla di integrato nel framework.

Ecco un'implementazione che ho usato in passato di un semplice stringResult:

public class StringResult extends StrutsResultSupport { 
private static final Log log = LogFactory.getLog(StringResult.class); 
private String charset = "utf-8"; 
private String property; 
private String value; 
private String contentType = "text/plain"; 


@Override 
protected void doExecute(String finalLocation, ActionInvocation invocation) 
     throws Exception { 
    if (value == null) { 
     value = (String)invocation.getStack().findValue(conditionalParse(property, invocation)); 
    } 
    if (value == null) { 
     throw new IllegalArgumentException("No string available in value stack named '" + property + "'"); 
    } 
    if (log.isTraceEnabled()) { 
     log.trace("string property '" + property + "'=" + value); 
    } 
    byte[] b = value.getBytes(charset); 

    HttpServletResponse res = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE); 

    res.setContentType(contentType + "; charset=" + charset); 
    res.setContentLength(b.length); 
    OutputStream out = res.getOutputStream(); 
    try { 
     out.write(b); 
     out.flush(); 
    } finally { 
     out.close();  
    } 
} 


public String getCharset() { 
    return charset; 
} 


public void setCharset(String charset) { 
    this.charset = charset; 
} 


public String getProperty() { 
    return property; 
} 


public void setProperty(String property) { 
    this.property = property; 
} 


public String getValue() { 
    return value; 
} 


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


public String getContentType() { 
    return contentType; 
} 


public void setContentType(String contentType) { 
    this.contentType = contentType; 
} 

}

Ho usato il json plugin a fare cose simili. Se si utilizza questo, si può utilizzare il seguente per esporre una singola proprietà stringa nella vostra azione:

<result name="success" type="json"> 
    <param name="root">propertyToExpose</param> 
</result> 
5

puoi avere il tuo metodo di azione di ritorno non un risultato stringa, ma un risultato di tipo StreamResult.

In altre parole:

class MyAction { 

public StreamResult method() { 
    return new StreamResult(new ByteArrayInputStream("mystring".getBytes())); 
} 
} 

Non devono necessariamente restituire una stringa da un metodo di azione Struts2. Puoi sempre restituire un'implementazione dell'interfaccia Result da xwork.

+2

Probabilmente volevi dire * return new StreamResult (nuovo ByteArrayInputStream ("mystring" .getBytes())); * Questo ha aiutato, grazie –

3

copia presente in classe azione

private InputStream inputStream; 
public InputStream getInputStream() { 
    return inputStream; 
} 

public String execute(){ 
    inputStream = new StringBufferInputStream("some data to send for ajax response"); 
    return SUCCESS; 
} 

struts.xml

<action name=....> 
<result type="stream"> 
       <param name="contentType">text/html</param> 
       <param name="inputName">inputStream</param> 
</result> 

Questo funziona quando si vuole inviare un unico dati in risposta

+0

Cosa facciamo per ottenere questo valore di stringa nella guida jsp page.plz. –

+0

StringBufferInputStream è obsoleto. prova invece: ByteArrayInputStream ("some ... response" .getBytes()); – fishjd