Struts2--result配置的各种视图转发类型

Strust2中的几种视图转发类型:

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

 

2. redirect:客户端(浏览器)重定向到页面,下面举个例子说明一下redirect视图转发,需要注意的是,由于是浏览器进行重定向,所以要把重定向的页面放在WebContent下面,否则无法访问

下面是struts.xml配置文件:

<package name="struts" namespace="/test" extends="struts-default">
	<action name="redirect" class="com.struts.action.RedirectAction" method="execute">
		<result name="success" type="redirect">/redirect.jsp?message=${message }</result>
	</action>
</package>

 下面是RedirectAction:

package com.struts.action;

public class RedirectAction {
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String execute() {
		this.message = "This is a redirect test";
		return "success";
	}
}

 

下面是需要重定向的redirect.jsp:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>redirect视图转发</title>
</head>
<body>
	message : ${param.message }
</body>
</html>

 部署项目到服务器,在地址栏中输入http://localhost:8080/strutsdemo/test/redirect

 则redirect.jsp显示message : This is a redirect test

浏览器地址栏变化结果:http://localhost:8080/strutsdemo/redirect.jsp?message=This%20is%20a%20redirect%20test

 

注:可用el表达式获取action中的属性值

 

3. redirectAction:服务器端跳转到指定的action,下面举个例子说明一下redirectAction视图的转发

下面是struts.xml配置文件:

<package name="struts" namespace="/test" extends="struts-default">
	<action name="testAction" class="com.struts.action.RedirectActionAction" method="execute">
		<result name="success" type="dispatcher">/WEB-INF/pages/redirectAction.jsp?message=${message }</result>
	</action>
		
	<action name="redirectAction">
		<result name="success" type="redirectAction">testAction</result>
	</action>
</package>

 

下面是RedirectActionAction:

package com.struts.action;

public class RedirectActionAction {
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String execute() {
		this.message = "This is a redirectAction test";
		return "success";
	}
}

 

下面是需要重定向的redirectAction.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>redirectAction视图转发</title>
</head>
<body>
	message : ${param.message }
</body>
</html>

 部署项目到服务器,在地址栏中输入http://localhost:8080/strutsdemo/test/redirectAction

则redirect.jsp显示message : This is a redirectAction test

浏览器地址栏变化结果:http://localhost:8080/strutsdemo/test/testAction.action

 

以上是重定向到同一命名空间下的action的情况,如果要重定向到不同命名空间下的action则Struts.xml的配置为:

 

<struts>
	<package name="struts" namespace="/test" extends="struts-default">
		<action name="testAction" class="com.struts.action.RedirectActionAction" method="execute">
			<result name="success" type="dispatcher">/WEB-INF/pages/redirectAction.jsp?message=${message }</result>
		</action>
	</package>
	
	<package name="other" namespace="/test2" extends="struts-default">
		<action name="redirectAction">
			<result name="success" type="redirectAction">
				<param name="namespace">/test</param>
				<param name="actionName">testAction</param>
			</result>
		</action>
	</package>
</struts>

 

 演示实例步骤参考上面,测试结果是一样的 

 

4. plainText:用于显示原始文件内容

 配置文件:

<action name="plainText" class="com.struts.action.PlainTextAction" method="execute">
	<result name="success" type="plainText">/WEB-INF/pages/plainText.jsp</result>
</action>

 下面是PlainTextAction:

package com.struts.action;

public class PlainTextAction {
	public String execute() {
		return "success";
	}
}

 

 下面是plainText.jsp:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>plainText视图转发</title>
</head>
<body>
	<%= new Date(); %>
</body>
</html>

 测试结果:如plainText.jsp

 

 要解决中文乱码问题action如下配置:

<action name="plainText">
	<result type="plainText">
		<param name="location">/WEB-INF/pages/plainText.jsp</param>
		<param name="charSet">UTF-8</param> 
	</result>
</action>

  5. 配置公用视图:

配置文件:

<struts>
	<package name="global" namespace="/global" extends="struts-default">
		<global-results>
			<result name="success">/WEB-INF/pages/global.jsp</result>
		</global-results>
	</package>
	
	<package name="struts" namespace="/test" extends="global">
		<action name="globalResultAction" class="com.struts.action.GlobalResultAction" method="execute">
			<param name="namespace">/global</param>
		</action>
	</package>
</struts>

从配置可以看出,配置文件中的package也像我们java中的包一样,可以继承

 

 GlobalResultAction:

package com.struts.action;

public class GlobalResultAction {
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String execute() {
		this.message = "This is a globalResult test";
		return "success";
	}
}

 global.jsp:

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>globalResult视图</title>
</head>
<body>
	message : ${message }
</body>
</html>

 

在浏览器地址栏输入:http://localhost:8080/strutsdemo/test/globalResultAction

页面显示:message : This is a globalResult test

你可能感兴趣的:(jsp,xml,Web,struts,浏览器)