struts2[2.1]结果处理-4种结果处理方式

1.学习路线

接下来咱们一起来学一学struts2的几种结果处理方式(关于struts2搭建、配置我会在之后慢慢发的),let`s go!

                                                                                           图1.学习路线

                                                                                           图2.类和配置文件

(在hello.jsp中,body标签中写入<h1>hello isleiyi</h1>)

2.结果处理方式

2.1结果处理方式-dispatcher

    新建一个Demo1Action类,继承ActionSupport,在创建一个execute()方法,return SUCCESS。(这个方法可以到源码中查看)

package cn.aisino.a_result;

import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action extends ActionSupport{

	public String execute() throws Exception {

		System.out.println("Demo1Action1");
		return SUCCESS;
	}
}

    在主配置文件struts.xml中配置结果处理方式:

    启动服务器,在地址栏中访问Demo1Action:

    便直接转发到hello.jsp了!(地址没变)

2.2结果处理方式-redirect

新建一个Demo1Action2类,继承ActionSupport,在创建一个execute()方法,return SUCCESS。

package cn.aisino.a_result;

import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action2 extends ActionSupport{

	public String execute() throws Exception {

		return SUCCESS;
	}
}

    在主配置文件struts.xml中配置结果处理方式:

    启动服务器,在地址栏中访问Demo1Action2:

    此时是重定向到hello.jsp(访问地址发生了变化!)

2.3结果处理方式-chain

新建一个Demo1Action3类,继承ActionSupport,在创建一个execute()方法,return SUCCESS。

package cn.aisino.a_result;

import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action3 extends ActionSupport{

	public String execute() throws Exception {

		System.out.println("Demo1Action3");
		return SUCCESS;
	}
}

    在主配置文件struts.xml中配置结果处理方式:

    启动服务器,在地址栏中访问Demo1Action3:

    然后看一下控制台:

    转发到Action1了。

2.4结果处理方式-redirectAction

新建一个Demo1Action4类,继承ActionSupport,在创建一个execute()方法,return SUCCESS。

package cn.aisino.a_result;

import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action4 extends ActionSupport{

	public String execute() throws Exception {

		System.out.println("Demo1Action4");
		return SUCCESS;
	}
}

    在主配置文件struts.xml中配置结果处理方式:

    启动服务器,在地址栏中访问Demo1Action4:

    然后看一下控制台:

    看地址栏,重定向到Action1了。

猜你喜欢

转载自blog.csdn.net/a_cherry_blossoms/article/details/84523057