struts2 문자열 직접 반환
1893 단어 struts2 반환 문자열
/**
* Struts2 , String
*
* @author Carmack Created on 2009-3-24 03:36:32
*/
public class StringResult extends ServletRedirectResult {
/**
* @author Carmack Created on 2009-3-24 03:36:24
*/
private static final long serialVersionUID = -2800270132418148253L;
private static final Logger LOG = LoggerFactory.getLogger(StringResult.class);
public StringResult(){
super();
}
public StringResult(String location){
super(location);
}
public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);
HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(HTTP_REQUEST);
response.setContentType("text/plain; charset=UTF-8");
response.setHeader("Content-Disposition","inline");
PrintWriter writer = null;
try {
writer = response.getWriter();
writer.write(request.getAttribute(finalLocation).toString());
} catch(NullPointerException e) {
if(finalLocation.equals("")){
LOG.warn(" value",e);
}else{
LOG.error(" ",e);
}
} finally {
if (writer != null) {
writer.flush();
writer.close();
}
}
}
}
사용도 쉬워요.Action에 적어주세요.
@Results({ @Result(name ="strResult", type=StringResult.class, value="testStr")})
방법에서teststr에 먼저 값을 부여하고 sttrResult로 되돌아오면 OK.
testStr =" 123abc";
return"strResult";
우리의 프로젝트는 모두 UTF-8로 인코딩되었기 때문에 문자 인코딩 처리를 하지 않았습니다.관심 있는 친구는 알아서 하세요.
org를 볼 수 있습니다.apache.struts2.dispatcher.PlainTextResult