十、Struts2结果集

 

十、Struts2结果集

 

 

 

 

 

 

 

 

 

 

 

 

1、Struts.xml配置文件

result元素:指定动作类的动作方法执行完后的结果视图.

属性:

name:字符串,与动作方法返回的值一致。默认是success

type:指定处理结果的结果类型的别名。(struts-default.xml有定义)。默认值是dispatcher

2、局部和全局结果视图

public class UserAction extends ActionSupport {

    @Override

    public String execute() {

        // 输入你好到页面上面去

 

        // 把显示内容的代码写在这里

 

        //配置文件根据返回的字符串,转发到对应的jsp页面

        return SUCCESS;

    }

 

    public String execute2() {

        return ERROR;

 

    }

}

 

<package name="p1" extends="struts-default">

        <!--全局结果视图:包中所有的action都能使用同一个视图 -->

        <global-results>

            <result name="error">/error.jsp</result>

        </global-results>

        <action name="action1" class="com.itheima.action.Demo1Action"

            method="execute">

            <!--局部结果视图:配置在了action的内部 -->

            <result name="success" type="dispatcher">/success.jsp</result>

        </action>

 

        <action name="action2" class="com.itheima.action.Demo1Action"

            method="execute2" />

    </package>

 

3、补充知识:动作类的生命周期

Struts2每次请求,都会实例化动作类的对象,因此它是线程安全的。

4、Struts2提供的结果视图

  • chain:转发到另外一个动作

转发到包内的动作:

<action name="a1">

            <!-- 转发到另外一个动作:同一个包中的 -->

            <!-- 位于result标签元素内部的值会调用chain处理类的setActionName("a2") -->

            <result type="chain">a2</result>

            

            <!-- <result type="chain">

                <param name="actionName">a2</param>

            </result> -->

        </action>

 

转发到另外一个带有名称空间的包中的动作

<package name="p1" extends="struts-default">

        <action name="a3">

            <!--转发到另外一个动作:不同包且另外还有命名空间 -->

            <!-- 以下的写法是不对的 -->

            <!-- <result type="chain">/hello/a33</result> -->

            

            <result type="chain">

            <!--肯定是注入:给chain这个类型的处理类注入参数。chain对应的处理类有以下2个方法:

            setNamespace(String str) setActionName(String name) -->

                <param name="namespace">/hello</param>

                <param name="actionName">a33</param>

            </result>

        </action>

    </package>

    

    <package name="p3" extends="struts-dafault" namespace="/hello">

        <action name="a33">

            <result type="dispatcher">/3.jsp</result>

        </action>

    </package>

 

 

  • dispatcher:转发到一个页面,jsp

<action name="a2">

            <!--

            <result type="dispatcher">/3.jsp</result>

            -->

            <result>

                <param name="location">/3.jsp</param>

            </result>

        </action>

 

 

  • redirect:重定向到一个页面

<result type="redirect">

                <param name="location">/3.jsp</param>

            </result>

 

  • redirectAction:重定向到一个动作

<action name="a4">

            <!--重定向到另外一个包中的动作 -->

            <result type="redirectAction">

                <param name="namespace">/hello</param>

                <param name="actionName">a44</param>

            </result>

        </action>

 

 

<package name="p3" extends="struts-dafault" namespace="/hello">

        <action name="a33">

            <result type="dispatcher">/3.jsp</result>

        </action>

        <action name="a44">

            <result type="dispatcher">/3.jsp</result>

        </action>

    </package>

 

 

  • stream:文件下载(此处不讲,文件上传和下载再讲)
  • plainText:以纯文本的形式显示结果

<action name="a5">

            <!--以纯文本的形式显示目标页面:直接显示源码 -->

            <result type="plainText">/4.jsp</result>

        </action>

 

5、自定义结果视图

目标:用一个叫做image的视图,输入随机验证码图片

 

步骤:

  1. 定义一个类,实现com.opensymphony.xwork2.Result接口

    /*

    * 该类实现Rusult接口,实现自定义结果视图

    */

    public class CaptchaResult implements Result {

        /**

         *

         */

        private static final long serialVersionUID = 1L;

        private int width = 110;

        private int height = 25;

            

        public void setWidth(int width) {

            this.width = width;

        }

     

        public void setHeight(int height) {

            this.height = height;

        }

     

        @Override

        public void execute(ActionInvocation arg0) throws Exception {

        //验证码的代码

            // BufferedImage:代码内存图片

            BufferedImage image = new BufferedImage(width, height,

                    BufferedImage.TYPE_INT_RGB);

     

            // Graphics:画笔

            Graphics g = image.getGraphics();

            

            // 画边线

            g.setColor(Color.GREEN);

            g.drawRect(0, 0, width, height);

            

            // 填充背景色

            g.setColor(Color.YELLOW);

            g.fillRect(1, 1, width - 2, height - 2);

     

            // 干扰线

            Random r = new Random();

            g.setColor(Color.GRAY);

            for (int i = 0; i < 15; i++) {

                g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width),

                        r.nextInt(height));

            }

            // 验证码

            g.setColor(Color.RED);

            g.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 18));

     

            int x = 19;

            for (int i = 0; i < 5; i++) {

                g.drawString(r.nextInt(10) + "", x, 20);

                x += 20;

            }

     

            // ImageIO:输出图片给指定的流

            OutputStream output = ServletActionContext.getResponse()

                    .getOutputStream();

            ImageIO.write(image, ".jpg", output);

     

        }

    }

     

  2. 在配置文件中定义结果类型

    <package name="p3" extends="struts-dafault">

            <result-types>

        <result-type name="image" class="com.itheima.results.CaptchaResult"></result-type>

            </result-types>

        </package>

     

  3. 现在就可以在动作中使用该结果类型

    <!-- 使用自定义结果视图 -->

            <action name="imageAction">

                <result name="success" type="image">

                    <param name="width">400</param>

                    <param name="height">300</param>

                </result>

            </action>

     

    小技巧:很多包中都要使用该结果视图怎么办?

    <package name="mydefault" extends="struts-dafault">

            <!--自定义结果视图 -->

            <result-types>

                <result-type name="image"

    class="com.itheima.results.CaptchaResult">

    </result-type>

            </result-types>

        </package>

     

    <!--继承配置了结果视图的包 -->

        <package name="p4" extends="mydefault">

            <!-- 使用自定义结果视图 -->

            <result name="success" type="image">

                <param name="width">400</param>

                <param name="height">300</param>

            </result>

        </package>

     

原文地址:https://www.cnblogs.com/Prozhu/p/5886387.html