struts2的框架解析第5篇action中result的各种转发类型
2017-12-05 01:16
519 查看
result配置类似于struts1中的forward,但struts2中提供了多种结果类型,常用的类型有:dispatcher(默认值,内部转发)、redirect(重定向到某个路径)、redirectAction(重定向到某个action)、plainText(输出页面的源代码)。
在result中可以用${属性名}表达式【ognl表达式】访问action中的属性,表达式中的属性名对应action中的属性,如下:
下面是redirectAction结果类型的例子,如果重定向的action中同一个包下:
plaintText:显示原始文件内容,例如:当我们需要原样显示jsp文件源代码的时候,就可以使用此类型。
代码如下:
adds.jsp的代码如下:
struts.xml文件的代码版本一:
struts.xml文件的代码版本二:
在result中可以用${属性名}表达式【ognl表达式】访问action中的属性,表达式中的属性名对应action中的属性,如下:
<result type = "redirect">/view.jsp?id=${id}</result>
下面是redirectAction结果类型的例子,如果重定向的action中同一个包下:
<result type = "redirectAction"> <param name = "actionName">hello world</param> <param name = "namespace">/test</param> </result>
plaintText:显示原始文件内容,例如:当我们需要原样显示jsp文件源代码的时候,就可以使用此类型。
<result name = "source" type = "plainText"> <param name = "localtion">/xxx.jsp</param> <param name = "charSet">UTF-8</param><!--指定读取文件的编码--> </result>
代码如下:
package com.gz.action; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class HelloWorldAction { private String mes; private String userName; public String getMessage() { return mes; } public String execute() throws UnsupportedEncodingException { mes = "hello world!"; this.userName = URLEncoder.encode("你好,世界!","UTF-8");//进行编码 return "success"; } public String addGlob() { return "message"; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
adds.jsp的代码如下:
<%@page import="java.util.*,java.net.URLDecoder"%> <%@ 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>Insert title here</title> </head> <body> ${param.userName} <%-- <%= URLDecoder.decode(new String(request.getParameter("userName").getBytes("ISO8859-1"),"UTF-8"),"UTF-8") %> --%> </body> </html>
struts.xml文件的代码版本一:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <!-- START SNIPPET: xworkSample --> <struts> <!-- http://localhost:8080/struts/test/helloworld --> <package name="hello" extends="struts-default" namespace="/test"> <action name="helloworld" class="com.gz.action.HelloWorldAction" method="execute"> <!-- <result name = "success">/WEB-INF/test/hello.jsp</result> --> <!-- 重定向到WEB-INF外面到jsp页面 --> <result name = "success" type = "redirect">/adds.jsp?userName=${userName}</result> </action> <action name = "add"> <result>/WEB-INF/test/add.jsp</result> </action> <!-- http://localhost:8080/struts/test/redirect.action --> <action name="redirect"> <result type="redirect">/adds.jsp</result> </action> <!-- 重定向到其他action --> <!-- http://localhost:8080/struts/test/redirectAction.action --> <action name="redirectAction"> <result type = "redirectAction">helloworld</result> </action> <!-- http://localhost:8080/struts/test/plainText.action --> <action name="plainText"> <result type="plainText"> <param name="location">/adds.jsp</param> <param name="charset">UTF-8</param><!-- 指定读取文件的编码 --> </result> </action> <!-- 重定向到其他包到action --> <!-- http://localhost:8080/struts/test/redirectOtherAtion.action --> <action name="redirectOtherAtion"> <result type = "redirectAction"> <param name="actionName">xx</param> <param name="namespace">/other</param> </result> </action> </package> <package name="other" namespace="/other" extends="struts-default"> <!-- 包内的全局视图 --> <!-- http://localhost:8080/struts/other/mange.action --> <global-results> <result name = "message">/WEB-INF/test/message.jsp</result> </global-results> <action name="mange" class = "com.gz.action.HelloWorldAction" method="addGlob"></action> <action name="xx"> <result>/WEB-INF/test/hello.jsp</result> </action> </package> </struts> <!-- END SNIPPET: xworkSample -->
struts.xml文件的代码版本二:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <!-- START SNIPPET: xworkSample --> <struts> <package name="base" extends="struts-default"> <global-results> <result name = "message">/WEB-INF/test/message.jsp</result> </global-results> </package> <!-- http://localhost:8080/struts/test/helloworld --> <package name="hello" extends="base" namespace="/test"> <action name="helloworld" class="com.gz.action.HelloWorldAction" method="execute"> <!-- <result name = "success">/WEB-INF/test/hello.jsp</result> --> <!-- 重定向到WEB-INF外面到jsp页面 --> <result name = "success" type = "redirect">/adds.jsp?userName=${userName}</result> </action> <action name = "add"> <result>/WEB-INF/test/add.jsp</result> </action> <!-- http://localhost:8080/struts/test/redirect.action --> <action name="redirect"> <result type="redirect">/adds.jsp</result> </action> <!-- 重定向到其他action --> <!-- http://localhost:8080/struts/test/redirectAction.action --> <action name="redirectAction"> <result type = "redirectAction">helloworld</result> </action> <!-- http://localhost:8080/struts/test/plainText.action --> <action name="plainText"> <result type="plainText"> <param name="location">/adds.jsp</param> <param name="charset">UTF-8</param><!-- 指定读取文件的编码 --> </result> </action> <!-- 重定向到其他包到action --> <!-- http://localhost:8080/struts/test/redirectOtherAtion.action --> <action name="redirectOtherAtion"> <result type = "redirectAction"> <param name="actionName">xx</param> <param name="namespace">/other</param> </result> </action> </package> <package name="other" namespace="/other" extends="base"> <!-- 内部转发到整个struts.xml的全局视图 --> <!-- http://localhost:8080/struts/other/mange.action --> <action name="mange" class = "com.gz.action.HelloWorldAction" method="addGlob"></action> <action name="xx"> <result>/WEB-INF/test/hello.jsp</result> </action> </package> </struts> <!-- END SNIPPET: xworkSample -->
相关文章推荐
- struts2之Action配置的各项默认值、result配置的各种试图转发类型及为应用指定多个struts配置文件
- struts2之Action配置的各项默认值、result配置的各种试图转发类型及为应用指定多个struts配置文件
- struts2之Action配置的各项默认值、result配置的各种试图转发类型及为应用指定多个struts配置文件
- struts2教程:5、Action中result的各种转发类型
- 传智Struts2笔记(四)Action中result的各种转发类型
- Struts2_Action中result的各种转发类型
- Struts2 中,action中的result的各种转发类型
- struts2之Action配置的各项默认值、result配置的各种试图转发类型及为应用指定多个struts配置文件
- struts2之Action配置的各项默认值、result配置的各种试图转发类型及为应用指定多个struts配置文件
- 【Struts2】④Action中result的各种转发类型
- 传智Struts2笔记(四)Action中result的各种转发类型
- struts2中Action配置中的各项默认值和result的各种转发类型
- Struts2:Action中result的各种转发类型
- struts2之Action配置的各项默认值、result配置的各种视图转发类型及为应用指定多个struts配置文件
- 2012/8/4Action中result的各种转发类型
- Action中result的各种转发类型
- struts.xml配置文件中action中的result的各种转发类型
- struts2学习------result配置的各种视图转发类型
- struts2 --result配置的各种视图转发类型
- Struts2中Action各种转发类型