Struts2 result类型

1.dispatcher:服务器跳转到页面,通常来处理JSP,默认类型。

2.redirect:重定向到页面。

Action:

1 public String redirect() {
2 message = "message中有值";
3 return "redirect";
4 }

struts.xml

1 <package name="chapter3" namespace="/chapter3" extends="struts-default">
2 <action name="redirectAction" class="chapter3.action.Chapter3Action" method="redirect">
3 <result name="redirect" type="redirect">/redirect.jsp</result>
4 </action>
5 </package>

注意的地方:外部中转不能带值过去,并且页面不能受保护

传参数:

1 <action name="redirectAction" class="chapter3.action.Chapter3Action" method="redirect">
2 <result name="redirect" type="redirect">/redirect.jsp?message=${message}</result>
3 </action>

页面:

${param.message}<br>

3.chain:服务端跳转到Action;

action:

1 public String action2() {
2 message = "我是action2中设置的值";
3 return "action2";
4 }

struts.xml

1 <package name="chapter32" namespace="/chapter32" extends="struts-default">
2 <action name="action2" class="chapter3.action.Chapter3Action" method="action2">
3 <result name="action2" type="chain">
4 <param name="actionName">redirectAction</param>
5 <param name="namespace">/chapter3</param>
6 </result>
7 </action>
8 </package>

4.redirectAction:外部跳转到Action;

action:

1 public String action3() {
2 message = "我是action3中设置的值";
3 return "action3";
4 }

struts.xml

1 <action name="action3" class="chapter3.action.Chapter3Action" method="action3">
2 <result name="action3" type="redirectAction">redirectAction</result>
3 </action>

跨命名空间的外部跳转:

1 public String action4() {
2 message = "我是action4中设置的值";
3 return "action4";
4 }

struts.xml

1 <action name="action4" class="chapter3.action.Chapter3Action" method="action4">
2 <result name="action4" type="redirectAction">
3 <param name="actionName">redirectAction</param>
4 <param name="namespace">/chapter3</param>
5 </result>
6 </action>
5,json
Struts2中返回JSON的格式有两种,一种是使用Servelt输出流,另一种就是使用struts2对JSON的扩展。这里笔者介绍第一种,
编写Action代码:
public void write() throws IOException{
HttpServletResponse response=ServletActionContext.getResponse();
/*
* 在调用getWriter之前未设置编码(既调用setContentType或者setCharacterEncoding方法设置编码),
* HttpServletResponse则会返回一个用默认的编码(既ISO-8859-1)编码的PrintWriter实例。这样就会
* 造成中文乱码。而且设置编码时必须在调用getWriter之前设置,不然是无效的。
* */
response.setContentType("text/html;charset=utf-8");
//response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//JSON在传递过程中是普通字符串形式传递的,这里简单拼接一个做测试
String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"张三\",\"say\":\"Hello , i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}";
out.println(jsonString);
out.flush();
out.close();
}

配置Action:

从以下的配置中可以明显的看到配置与普通的action配置没有任何区别,只是没有返回的视图而已。

<action name="write" class="json.JsonAction" method="write" />

返回值:

{"user":{"id":"123","name":"张三","say":"Hello , i am a action to print a json!","password":"JSON"},"success":true}
6,stream
就是把数据以字节流的方式输出,

1:图片验证码:

Action类,action主要要提供一个获取InputStrem的方法:

public class CheckCodeAction extends ActionSupport implements SessionAware {
private Logger log = LoggerFactory.getLogger(this.getClass());
private InputStream imageStream;
private Map session;

public String getCheckCodeImage(String str, int show, ByteArrayOutputStream output) {
Random random = new Random();
BufferedImage image = new BufferedImage(80, 30, BufferedImage.TYPE_3BYTE_BGR);
Font font = new Font("Arial", Font.PLAIN, 24);
int distance = 18;
Graphics d = image.getGraphics();
d.setColor(Color.WHITE);
d.fillRect(0, 0, image.getWidth(), image.getHeight());
d.setColor(new Color(random.nextInt(100) + 100, random.nextInt(100) + 100, random.nextInt(100) + 100));
for (int i = 0; i < 10; i++) {
d.drawLine(random.nextInt(image.getWidth()), random.nextInt(image.getHeight()), random.nextInt(image.getWidth()),
random.nextInt(image.getHeight()));
}
d.setColor(Color.BLACK);
d.setFont(font);
String checkCode = "";
char tmp;
int x = -distance;
for (int i = 0; i < show; i++) {
tmp = str.charAt(random.nextInt(str.length() - 1));
checkCode = checkCode + tmp;
x = x + distance;
d.setColor(new Color(random.nextInt(100) + 50, random.nextInt(100) + 50, random.nextInt(100) + 50));
d.drawString(tmp + "", x, random.nextInt(image.getHeight() - (font.getSize())) + (font.getSize()));
}
d.dispose();
try {
ImageIO.write(image, "jpg", output);
} catch (IOException e) {
log.warn("生成验证码错误.", e);
}
return checkCode;
}

public String execute() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
String checkCode = getCheckCodeImage("ABCDEFGHJKLMNPQRSTUVWXYZ123456789", 4, output);
this.session.put(Constants.CHECK_CODE_KEY, checkCode);
//这里将output stream转化为 inputstream
this.imageStream = new ByteArrayInputStream(output.toByteArray());
output.close();
return SUCCESS;
}

public InputStream getImageStream() {
return imageStream;
}

public void setSession(Map session) {
this.session = session;
}

struts配置文件:

<action name="checkCode" class="CheckCodeAction">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<!-- 指定提供InputStream的filed name -->
<param name="inputName">imageStream</param>
<param name="bufferSize">1024</param>
</result>
<interceptor-ref name="defaultStack"/>
</action>

原文链接:http://www.cnblogs.com/elleniou/archive/2012/10/17/2728237.html

【Struts2】result类型的更多相关文章

  1. Struts2 Result 类型和对应的用法详解 2

  2. Struts2 Result 类型和对应的用法详解

  3. struts2学习笔记之七:Result类型

    一:关于Struts2的type类型,也就是Result类型,他们都实现了共同的接口Result,都实现了execute方法 他们体现了策略模式,具体Result类型参见:struts-default ...

  4. Struts2中 Result类型配置详解

    一个result代表了一个可能的输出.当Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出.在com.opensymphony.xwor ...

  5. java struts2入门学习---中文验证、对错误消息的分离、结果(result)类型细节配置

    一.需求 利用struts2实现中文验证并对错误消息的抽离. 详细需求:用户登录-->不填写用户名-->页面跳转到用户登录页面,提示用户名必填(以英文和中文两种方式提示)-->填写英 ...

  6. struts2常用类型的Result

    2.2.1. dispatcher dispatcher类型是用于转发的Result,可以将请求转发给JSP.这种类型的Result对应的类为 ServletDispatcherResult,它是St ...

  7. struts2学习笔记之十一:struts2的类型转换器

    Struts2的类型转换器   如何实现Struts2的类型转换器? * 继承StrutsTypeConverter * 覆盖convertFromString和convertToString   注 ...

  8. struts2结果类型

    struts2结果类型: 结果类型 描述 前request域属性是否丢失 1 dispatcher 用于与jsp整合的结果类型.默认结果类型. 2 chain Action链式处理结果类型.前一个Ac ...

  9. struts2自定义类型转换器

    首先,何为struts2的类型转换器? 类型转换器的作用是将请求中的字符串或字符串数组参数与action中的对象进行相互转换. 一.大部分时候,使用struts2提供的类型转换器以及OGNL类型转换机 ...

随机推荐

  1. OCR 基本知识

    OCR,optical character recognition 的简称,也就是光学识别系统,属于图形识别的一个分支,OCR是针对印刷体字符,採用光学的方式将文档资料转换成原始资料黑白点阵的图像文件 ...

  2. Android 演示 Android ListView 和 github XListView(1-3)

    本文内容 环境 项目结构 演示 1:ListView 演示 2:简单 XListView 演示 3:音乐列表 XListView 演示 4:另一个音乐列表 XListView 本文四个演示,循序渐进. ...

  3. AS-->创建项目(慢)和打开项目(慢)等需要注意的问题

    Android Studio 创建新项目的时候,会在进度条的界面滞留很久. 有时候一天都卡在这. 打开其它项目有些时候,也会出现这种情况. 主要的原因就是, 此过程正在 下载对应版本的 gradle. ...

  4. 关于ngModelOptions用法总结 让校验不过的验证绑定ngModel

    updataOn 指定ng-model以什么绑定事件触发 default 就是默认的大家都知道blur 失去焦点的时候更新mouseover 鼠标滑过....... <input type=&q ...

  5. TOJ 3365 ZOJ 3232 It's not Floyd Algorithm / 强连通分量

    It's not Floyd Algorithm 时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte   描述 When a directed grap ...

  6. 树莓派中找不到/dev/video0的解决方案及RaspberryCam的使用

    一.原因 当使用CSI连接的方式将摄像头模块连接树莓派后,在/dev/中找不到video0,因此使用一些第三方库(如Opencv或RaspberryCam)去调用摄像头时,无法调用成功. 二.解决方法 ...

  7. SpringBoot2中配置文件的调整,升级SpringBoot2时候注意的坑

    原来使用SpringBoot1.5最近写个demo后发现原来的配置文件不能用了. 最后上网查询了一下资料,springboot2.0和spring1.x还是存在不少问题的. 1.问题一:Java版本要 ...

  8. Java 基础【15】 压缩与解压缩

    Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...

  9. 〖Linux〗svn log 每个日志记录只显示一行的方法

    vi ~/.bashrc,增加一个function svnlog(){ svn log "$@" | awk -f <(cat <<EOF #!/usr/bin/ ...

  10. sql如何通过当前日期获取上周,上上周,上上上周的起始日期(周一_周七)

    当前时间周的起始日期(以周一为例)select DATEADD(week,DATEDIFF(week,0,getdate()),0)上周起始:select dateadd(week,-1,DATEAD ...