struts2几种result type探究 (二)

2014-11-24 02:38:25 · 作者: · 浏览: 3
ueStack();

if (status != -1) {
response.setStatus(status);
} else if (error != -1) {
if (errorMessage != null) {
String finalMessage = parse TextParseUtil.translateVariables(
errorMessage, stack) : errorMessage;
response.sendError(error, finalMessage);
} else
response.sendError(error);
}

if (headers != null) {
for (Iterator iterator = headers.entrySet().iterator();
iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
String value = (String) entry.getValue();
String finalValue = parse TextParseUtil.translateVariables(value, stack) : value;
response.addHeader((String) entry.getKey(), finalValue);
}
}
}4.redirect


重定向操作,与请求转发的区别看1的介绍

让客户端请求另外的网络资源,可以为action,也可以为视图资源

下面是apache的示例

view plaincopy to clipboardprint

foo.jsp
false


foo.jsp
false

5.redirectAction

重定向至Action,与redirect的区别找了好久,但是也没发现比较权威的说明

最明显的区别当然是redirectAction只能请求action,如果请求视图资源会报错

然后还有个小区别就是redirectAction会为url添加.action后缀而redirect不会


有说redirectAction无法通过url传参,但是我测试时完全可以通过request获取到,

此处区别如果有哪位知道的麻烦告知,Thank you!

下面是apache的传参的示例


view plaincopy to clipboardprint

<-- Pass parameters (reportType, width and height) -->



generateReport
/genReport
pie
100
100




<-- Pass parameters (reportType, width and height) -->



generateReport
/genReport
pie
100
100



6.stream

这个返回类型主要用作下载文件或者在浏览器上显示PDF等文档

此处给一个显示PDF文档示例

项目web.xml中


view plaincopy to clipboardprint

pdf
application/pdf


pdf
application/pdf

struts.xml中


view plaincopy to clipboardprint


application/pdf
inputStream
filename="a.pdf"




application/pdf
inputStream
filename="a.pdf"


TestAction.java中


view plaincopy to clipboardprint
public class TestAction extends ActionSupport
{

private InputStream inputStream;


public InputStream getInputStream()
{
return inputStream;
}

@Override
public String execute() throws Exception
{
inputStream = new FileInputStream("xxxxx/a.pdf");//要下载或显示的文件路径
return SUCCESS;
}
}
public class TestAction extends ActionSupport
{

private InputStream inputStream;


public InputStream getInputStream()
{
return inputStream;
}

@Override
public String execute() throws Exception
{
inputStream = new FileInputStream("xxxxx/a.pdf");//要下载或显示的文件路径
return SUCCESS;
}
}需要注意的地方是struts.xml中inputName的值要与TestAction中的属性名相同,在此处就是inputStream


7.plainText


响应以plain形式返回给客户端

截取源码的最重要的一部分


view plaincopy to clipboardprint
if (charset != null) {
response.setContentType("text/plain; charset="+charSet);
}
else {
response.setContentType("text/plain");
}
if (cha