struts2的结果类型
1、从struts-default.xml入手,得到结果类型列表以及对应的处理类:
<result-types>
<!-- 转发到action -->
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<!-- 转发到jsp -->
<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"/>
<!-- 重定向到jsp -->
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<!-- 重定向到action -->
<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-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
</result-types>
然后我们知道一些信息:
1、常用的跳转类型:
转发
chain:转发到action
dispatcher:转发到jsp
重定向:
redirect:重定向到jsp
redirectAction:重定向到action
流:
stream:在文件下载中使用
2、默认的跳转方式是转发到jsp
2、几种跳转方式的使用例子:
<package name="resulttype" namespace="/resulttype" extends="struts-default">
<action name="resultTypeAction" class="cn.itcast.resulttype.ResultTypeAction">
<!-- 方法一
默认为转发
type:指定结果类型,默认为转发“dispatcher”
-->
<!-- <result name="success" type="dispatcher">/resulttype/success.jsp</result>-->
<!-- 方法二:标准写法
-->
<!--<result name="success" type="dispatcher">
param:参数
name:参数的名称为“location”
实际上是struts2框架底层StrutsResultSupport类的setLocation()提供的
所谓的参数,应该是set方法或者get方法后面跟的名称才是参数名
<param name="location">/resulttype/success.jsp</param>
</result>-->
<!-- 重定向到jsp -->
<!--<result name="success" type="redirect">
<param name="location">/resulttype/success.jsp</param>
</result>-->
<!--
重定向到action
param:
actionName:指定“目的地”动作的名称。指定action标签name属性的值
namespace:用来指定目的地的命名空间。指定的是struts的配置文件action对应的package的namespace的值
-->
<!--<result name="success" type="redirectAction">
<param name="actionName">userAction</param>
<param name="namespace">/prima</param>
</result>
-->
<!--
跳转到action
param:
和重定向到action的一样
-->
<result name="success" type="chain">
<param name="actionName">userAction</param>
<param name="namespace">/prima</param>
</result>
</action>
</package>
注意:
我们以后写结果类型的时候尽量写标准写法。
重定向与转发的最大的区别在域request域信息的是否重置,所以我们使用request中添加属性,判断属性值是否可以显示验证转发与重定向。
如何在Action中获取request等web中常用的对象呢?
使用struts2提供的工具类ServletActionContext的指定方法,原来是struts2将request等web对象封装成了一个map集合,需要我们使用该工具类调用!
public String execute() throws Exception {
System.out.println("ResultTypeAction ****** execute() ");
//struts2框架将request封装成一个map集合,通过struts2框架提供的工具类ServletActionContext的getRequest()方法来获取request
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("username", "username_request");
return "success";
}
测试使用的代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'form.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="${pageContext.request.contextPath }/resulttype/resultTypeAction.do" name="form1" method="post">
<input type="submit" value="提交">
</form>
</body>
</html>
resulttype/form.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
resulttype: ${requestScope.username }
</body>
</html>
resulttype/success.jsp
前面的prima/success.jsp部分修改:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
this is success.jsp! <br>
${requestScope.username } </body>
</html>
resulttype/success.jsp
struts_resulttype.xml主要代码列出来就不写了,导入xml的也不写了
package cn.itcast.resulttype;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ResultTypeAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("ResultTypeAction ****** execute() ");
//struts2框架将request封装成一个map集合,通过struts2框架提供的工具类ServletActionContext的getRequest()方法来获取request
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("username", "username_request");
return "success";
}
}
resultTypeAction.java
struts2的结果类型的更多相关文章
- struts2自己定义类型转换器
1.1. struts2自己定义类型转换器 1) 自定类型转换类,继承DefaultTypeConverter类 package com.morris.ticket.conversio ...
- struts2基础---->自定义类型转换器
这一章,我们开始struts2中自定义类型转换器的学习. 自定义类型转换器
- Struts2 第六讲 -- Struts2的结果类型
7.struts2的结果类型 l 每个 action 方法都将返回一个 String 类型的值, Struts 将根据这个值来决定响应什么结果. l 每个 Action 声明都必须包含有数量足够多的 ...
- Struts2中 Result类型配置详解
一个result代表了一个可能的输出.当Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出.在com.opensymphony.xwor ...
- Struts2之自定义类型转换器
Struts2自定义类型转换器分为局部类型转换器和全局类型转换器 (1)局部类型转换器 如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用d ...
- struts2中Double类型的转换
今天做项目,ssh + Extjs,页面js中定义了几个NumberField,对应的value都是double类型的,其中有个NumberField的name为 name,结果执行的时候报错了,说找 ...
- struts2自定义日期类型转换器
在java web表单中提交的数据难免会有日期类型,struts2支持的日期类型是yyyy-MM-dd,如果是其他格式,就需要自己进行转换.比如yy-MM-dd 要完成自己定义的转换需要完成. 主要的 ...
- 【Struts2】result类型
Struts2 result类型 1.dispatcher:服务器跳转到页面,通常来处理JSP,默认类型. 2.redirect:重定向到页面. Action: 1 public String red ...
- struts2 result type类型
result标签中type的类型 类型 说明 chain 用于Action链式处理 dispatcher 用于整合JSP,是<result>元素默认的类型 freemarket 用来整合F ...
- struts2逻辑视图类型汇总与解释(转)
在struts2框架中,当action处理完之后,就应该向用户返回结果信息,该任务被分为两部分:结果类型和结果本身. 结果类型提供了返回给用户信息类型的实现细节.结果类型通常在Struts2中就已预定 ...
随机推荐
- 文件I/O操作为什么叫输入/出流
参考以下文档: http://blog.csdn.net/hguisu/article/details/7418161 我们关注的焦点是错误的,重点不在文件,我们关注的核心是数据流. 这种流可以是文本 ...
- js执行顺序总结
参考博文:http://www.2cto.com/kf/201401/273825.html http://www.jb51.net/article/44123.htm http://zhidao.b ...
- 云数据库 RDS 版怎么创建数据库和账号MySQL 5.7版
若要使用云数据库RDS,您需要在实例中创建数据库和账号.对于MySQL 5.7版本的实例,您需要通过RDS控制台创建一个初始账号,然后可以通过数据管理(DMS)控制台创建和管理数据库.本文将主要介绍在 ...
- 【转载】教你使用 Reflexil 反编译.NET
简介 反编译的方式有很多种,其实最靠谱的还是IL反编译. 如果不懂IL可以尝试我这边文章入门:http://www.wxzzz.com/278.html 不过我下面要说的不是IL这种底层的代码反编译, ...
- 利用php调用so库文件中的代码
某个功能被编译到so文件中,那么如何通过php来调用它?一个方法是写一个php模块(php extension),在php中调用该模块内的函数,再通过该模块来调用so中的函数.下面做一个简单的例子,使 ...
- Sping Boot入门到实战之实战篇(二):一些常用功能的Spring Boot Starters
包含功能 阿里云消息服务MNS 阿里云消息队列服务(即时消息.延迟消息.事务消息) AOP日志 基于MyBatis通用Mapper及DRUID的数据库访问 dubbo支持 错误处理 七牛图片服务 re ...
- 现在有一个城市销售经理,需要从公司出发,去拜访市内的商家,已知他的位置以及商家的位置,但是由于城市道路交通的原因,他只能在左右中选择一个方向,在上下中选择一个方向,现在问他有多少种方案到达商家地址。给定一个地图map及它的长宽n和m,其中1代表经理位置,2代表商家位置,-1代表不能经过的地区,0代表可以经过的地区,请返回方案数,保证一定存在合法路径。保证矩阵的长宽都小于等于10。
include "stdafx.h" #include<iostream> #include<vector> #include<algorithm&g ...
- CGI FASTCGI php-fpm
CGI(Common Gateway Interface) CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工 ...
- Erlang节点重启导致的incarnation问题(转)
转自霸爷的博客: 转载自系统技术非业余研究 本文链接地址: Erlang节点重启导致的incarnation问题 遇到个问题, =ERROR REPORT==== 10-Mar-2016::09:44 ...
- 深入理解JVM:JVM执行时数据区域分类
JVM在运行java程序的过程中会把他所管理的内存划分为若干个不同的数据区域. 这些区域都有各自的用途和创建.销毁时间.有些区域随着虚拟机的启动而存在.有些区域则依赖用户线程的启动和结束而建立和销毁. ...