博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Struts2】result类型
阅读量:4332 次
发布时间:2019-06-06

本文共 4409 字,大约阅读时间需要 14 分钟。

Struts2 result类型

 

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

 

2.redirect:重定向到页面。

Action:

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

struts.xml

1 
2
3
/redirect.jsp
4
5

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

传参数:

1 
2
/redirect.jsp?message=${message}
3

 页面:

${param.message}<br>

 

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

action:

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

 

struts.xml

1 
2
3
4
redirectAction 5
/chapter3 6
7
8

 

 

4.redirectAction:外部跳转到Action;

 

action:

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

struts.xml

1 
2
redirectAction
3

 跨命名空间的外部跳转:

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

struts.xml

1 
2
3
redirectAction 4
/chapter3 5
6
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配置没有任何区别,只是没有返回的视图而已。

返回值:

{"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>

 

原文链接:h

转载于:https://www.cnblogs.com/HDK2016/p/7342138.html

你可能感兴趣的文章
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-6.微信扫码登录回调本地域名映射工具Ngrock...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-8.用户模块开发之保存微信用户信息...
查看>>
代码片段收集
查看>>
vue-cli3创建项目时报错
查看>>
输入1-53周,输出1-53周的开始时间和结束时间
查看>>
实验二
查看>>
shell——按指定列排序
查看>>
crash 收集
查看>>
507 LOJ 「LibreOJ NOI Round #1」接竹竿
查看>>
UI基础--烟花动画
查看>>
2018. 2.4 Java中集合嵌套集合的练习
查看>>
精通ASP.NET Web程序测试
查看>>
vue 根据不同属性 设置背景
查看>>