2009-08-09 38 views
5

Tôi muốn gửi chuỗi như là một phản ứng với phương pháp AJAX xhrPOST. Tôi đang sử dụng Struts2 để thực hiện xử lý phía máy chủ. Nhưng, tôi không nhận được làm thế nào để gửi kết quả "loại" như chuỗi và lập bản đồ cần được thực hiện để gửi chuỗi từ lớp hành động struts2 đến phản ứng AJAX.Kiểu kết quả chuỗi trả về từ Struts2

Trả lời

2

Bạn có thể tạo một StringResult đơn giản khá dễ dàng bằng cách mở rộng StrutsResultSupport, nhưng không có gì tồn tại được xây dựng trong khuôn khổ theo như tôi biết.

Dưới đây là một thực hiện mà tôi đã sử dụng trong quá khứ của một StringResult đơn giản:

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

}

Tôi đã sử dụng json plugin để làm những điều tương tự. Nếu bạn sử dụng điều đó, bạn có thể sử dụng thông tin sau để hiển thị thuộc tính Chuỗi duy nhất trong hành động của mình:

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

Bạn có thể có phương thức hành động không trả về kết quả Chuỗi, mà là kết quả của loại StreamResult.

Nói cách khác:

class MyAction { 

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

Bạn không nhất thiết phải trả về một String từ một phương pháp hành động struts2. Bạn luôn có thể trả về việc triển khai giao diện Kết quả từ xwork.

+2

Có lẽ bạn có nghĩa là * trở StreamResult mới (new ByteArrayInputStream ("mystring" .getBytes())); * Điều này đã giúp, cảm ơn –

3

bản sao này trong lớp hành động

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> 

này hoạt động khi chúng ta muốn gửi một dữ liệu duy nhất để đáp ứng

+0

Chúng tôi làm gì để nhận được giá trị chuỗi này trong trợ giúp jsp page.plz. –

+0

StringBufferInputStream không được chấp nhận. thay vào đó hãy thử: ByteArrayInputStream ("some ... response" .getBytes()); – fishjd

Các vấn đề liên quan