复习struts2之result配置的各种视图转发类型

一、struts1中提供的转发类型
在struts1中,forward提供两种转发类型:
1、内部转发:
<forward name="add">/index.jsp</forward>
2、采用浏览器转发:
<forward name="add" redirect="true">/index.jsp</forward>
二、struts2中提供的转发类型
struts2中的struts2.xml文件中的result配置类似于struts1中的forward,但struts2中提供了多种结果类型,常用的类型有: dispatcher(默认值,内部转发类型)、 redirect 
在struts2中实现服务器内部转发类型:
		<action name="addUI">
			<result>/WEB-INF/page/employeeAdd.jsp</result>
		</action>
浏览器重定向到某个路径(例子:重定向到jsp)
注意:从浏览器重定向到一个jsp页面时,jsp页面不能放在WEB-INF下
原因:浏览器重定向既引导用户的浏览器去访问某个路径
在struts.xml文件中进行如下配置:
		<action name="helloword" class="cn.itcast.action.HelloWordAction" method="execute">
			<result type="redirect" name="success">/employeeAdd.jsp?username=${username}</result>
		</action>
在action中定义一个变量username,并生成相应的set、get方法,在execute方法中直接给username添加一个默认值:this.username=" aaa ";
访问路径:localhost:8080/struts2/test/helloword
加载完毕显示的路径为
http://localhost:8080/struts2/employeeAdd.jsp?username=aaa

如果在action中传递中文的话,要先对中文进行编码
this.username=URLEncoder.encode("老王","UTF-8");
如果想在重定向的页面中打印出中文字符串的话也同样要进行编码
<%= URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8")  %>

重定向到一个action:
		<action name="redirectAction ">
			<result type="chain">helloword</result>
		</action>
旧版本中:
<result type="redirectAction">helloworld</result> 
旧版本中重定向到其他包下的action: 
<result type="redirectAction"> 
	<param name="actionName">helloworld</param> 
	<param name="namespace">/test</param> 
</result> 

要把视图的源代码原样的显示给用户看,用plainText
	<action name="plainText">
		<result type="plainText">/index.jsp</result>
	</action>
Jsp页面:
<%=new Date() %>
把整个视图的源代码都显示在用户的浏览器中
如果在jsp页面中存放中文的话,在显示时会有乱码,原因就是编码集不统一,解决办法如下:
	<action name="plainText">
		<result >
			<param name="location">/index.jsp</param>
			<!-- 指定读取文件的编码 -->
			<param name="charSet">UTF-8</param>	
		</result>
	</action>

定义全局视图(实现视图的重用):
在struts.xml文件中:
	<global-results>
		<result name="message">/WEB-INF/page/message.jsp</result>
	</global-results>
	<action name="manage"  class="cn.itcast.action.HelloWordAction" method="add"></action>

在action中:
	public String add(){
		return "message";
	}

如果想在其他包中使用本包中的全局视图的话,只需继承本包即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值