struts2的resultType

struts2定义了多种resultType,包括dispatcher和redirectAction等

在struts-default.xml文件中,定义了所有的resultType类型
<result-types>
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
        </result-types>

有空可以跟进去看看源码

本文重点说的是默认的dispatcher和redirectAction的区别

struts2的resultType

这里点击“删除”,会执行一段javascript代码:
$(document).ready(function() {
	$(".delete_book").click(deleteBook);
});

function deleteBook() {
	var $deleteButton = $(this);
	var $idSpan = $deleteButton.parent().find(".hidden_book_id");
	var bookId = $idSpan.text();
	var url = "delete.action?id=" + bookId;
	window.location.href = url;
}

关键就是会跳转到delete.action?id=xxx这个URL,进入Action的delete()方法
@Autowired
	private IBookService bookService;

	private List<Book> books;// 书籍列表

	private String id;// 接收客户端传值

	public String list() {
		books = bookService.getAllBooks();
		return SUCCESS;
	}

	public String delete() {
		bookService.deleteBookById(id);
		books = bookService.getAllBooks();
		return SUCCESS;
	}

此时struts.xml的配置是这样的:
<package name="bookManage" extends="struts-default" namespace="/book">
		
		<action name="list" class="bookAction" method="list">
			<result name="success">../jsp/bookManage/bookList.jsp</result>
		</action>
		
		<action name="delete" class="bookAction" method="delete">
			<result name="success">../jsp/bookManage/bookList.jsp</result>
		</action>
		
	</package>

代码很简单,就不解释了。功能可以实现,不过有个问题,就是用这种配置方式,删除操作之后,URL是这样的:

struts2的resultType

这时用户如果用F5刷新浏览器,就会发生重复提交的问题。所以可以这样修改,将resultType改成redirectAction
<package name="bookManage" extends="struts-default" namespace="/book">
		
		<action name="list" class="bookAction" method="list">
			<result name="success">../jsp/bookManage/bookList.jsp</result>
		</action>
		
		<action name="delete" class="bookAction" method="delete">
			<result name="success" type="redirectAction">list.action</result>
		</action>
		
	</package>

这样执行完delete.action后,会跳转进list.action,所以delete()方法也可以删除一行代码
@Autowired
	private IBookService bookService;

	private List<Book> books;// 书籍列表

	private String id;// 接收客户端传值

	public String list() {
		books = bookService.getAllBooks();
		return SUCCESS;
	}

	public String delete() {
		bookService.deleteBookById(id);
		return SUCCESS;
	}

实际看点击“删除”按钮的前后URL对比

struts2的resultType

struts2的resultType

实现了我们的目的

总结来说,默认的resultType是dispatcher,一般用来处理jsp跳转。如果希望一个action执行之后进入另一个action,可以用redirectAction,如果要多个action共同响应一个请求,可以用chain

最后补充一点,通过redirectAction进入新的Action,会新创建一个Action的实例
public String list() {
		System.out.println("after redirectAction, currentBook is null or not");
		if (null == currentBook) {
			System.out.println("currentBook is null");
		} else {
			System.out.println("currentBook is not null");
		}
		books = bookService.getAllBooks();
		return SUCCESS;
	}

<action name="delete" class="bookAction" method="delete">
			<result name="success" type="redirectAction">list.action</result>
		</action>

输出结果:
after redirectAction, currentBook is null or not
currentBook is null

你可能感兴趣的:(struts2,redirectAction,dispatcher,resultType)