Struts2.xml中result type属性说明

合集下载

struts2 strus.xml中result类型及含义

struts2 strus.xml中result类型及含义

struts2strus.xml中result类型及含义一个提交到服务器的处理通常可以分为两个阶段,第一个阶段查询服务器状态(查询或者更新数据库),第二个阶段选择一个合适的结果页面其返回给用户(这里要讲的Result的内容)。

Struts2提供了对不同种类返回结果的支持,常见的有JSP,FreeMarker,Velocity等。

Struts2支持的不同类型的返回结果为:名字说明Chain Result用来处理Action链Dispatcher Result用来转向页面,通常处理JSPFreeMarker Result处理FreeMarker模板HttpHeader Result用来控制特殊的Http行为Redirect Result重定向到一个URLRedirect Action Result重定向到一个ActionStream Result向浏览器发送InputSream对象,通常用来处理文件下载Velocity Result处理Velocity模板XLS Result处理XML/XLST模板PlainText Result显示原始文件内容,例如文件源代码结合Tile使用S2PLUGINS:TilesResult另外第三方的Result类型还包括JasperReports Plugin,专门用来处理JasperReport类型的报表输出。

在struts-default.xml文件中已经有了对于所有类型Result的定义:<result-types><result-type name="chain"class="com.opensymphony.xwork2.ActionChai nResult"/><result-type name="dispatcher"class="org.apache.struts2.dispatcher.Serv letDispatcherResult"default="true"/><result-type name="freemarker"class="org.apache.struts2.views.freemarke r.FreemarkerResult"/><result-type name="httpheader"class="org.apache.struts2.dispatcher.Http HeaderResult"/><result-type name="redirect"class="org.apache.struts2.dispatcher.Serv letRedirectResult"/><result-type name="redirectAction"class="org.apache.struts2.dispatcher.Serv letActionRedirectResult"/><result-type name="stream"class="org.apache.struts2.dispatcher.Stre amResult"/><result-type name="velocity"class="org.apache.struts2.dispatcher.Velo cityResult"/><result-type name="xslt"class="org.apache.struts2.views.xslt.XSLT Result"/><result-type name="plainText"class="org.apache.struts2.dispatcher.Plai nTextResult"/><!--Deprecated name form scheduled for removal in Struts 2.1.0.The camelCase versions are preferred.See ww-1707--><result-type name="redirect-action"class="org.apache.struts2.dispatcher.Serv letActionRedirectResult"/><result-type name="plaintext"class="org.apache.struts2.dispatcher.Plai nTextResult"/></result-types>从上述代码中可以看出在不指定Result类型的时候使用dispatcher类型。

基于Struts2 Result Type为chain 的Action之间数据传递

基于Struts2 Result Type为chain 的Action之间数据传递

chain:基本用途是构造成一条动作链。

前一个Action将控制权转交给后一个Action,而前一个Action的状态在后一个Action里仍然保持着。

我现在有一个场景,FirstAction 通过chain的方式,将控制权交给SecondAction。

FirstAction对应的页面代码为first.ftl,SecondAction对应的页面代码为second.ftl。

假设我们的FirstAction如下定义:public class SecondAction extends ActionSupport{private CustomUser user = null;public String execute() throws Exception {// 利用user做事情或显示在页面上}// getter setter}意思很明确了,通过first.ftl的输入,到DB中或其他,生成了我们的CustomUser对象,这个CustomUser对象将要在SecondAction使用。

于是我们想到了要配置FirstAction 的name为toSecond的Result type为chain,将生成的CustomUser对象传递到SecondAction中,我们也这样做了,但是经过调试,发现在SecondAction中没有得到FirstAction中的CustomUser对象。

SecondAction是这样实现的:public class SecondAction extends ActionSupport{private CustomUser user = null;public String execute() throws Exception {// 利用user做事情或显示在页面上}// getter setter}看一下ChainingInterceptor.java的实现,发现有这样的注释:An interceptor that copies all the properties of every object in the value stack to t he currently executing object.在FirstAction 中CustomUser user 并没有在value stack 中,所以没有拷贝到SecondAction中。

struts2_resultType

struts2_resultType

首先看一下在struts-default.xml中对于result-type的定义:<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>chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. dispatcher:用来转向页面,通常处理JSP.freemaker:处理FreeMarker模板.httpheader:控制特殊HTTP行为的结果类型.redirect:重定向到一个URL,被跳转的页面中丢失传递的信息,如request. redirectAction:重定向到一个Action,跳转的页面中丢失传递的信息.stream:向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据. velocity:处理Velocity模板.xslt:处理XML/XLST模板.plainText:显示原始文件内容,例如文件源代码.重点说一下redirect和redirectAction的区别:(1)使用redirect需要后缀名,使用redirect-action可以不需要后缀名.(2)type="redirect"的值可以转到其它命名空间下的action,而redirect-action只能转到同一命名空下的action,因此它可以省略.do的后缀直接写action的名称.1、dispatcher结果类型Struts2在后台使用Servlet API 的RequestDispatcher来转发请求,因此在用户的整个请求/响应过程中,目标Servlet/JSP接收到的request/response对象,与最初的Servlet/JSP相同。

struts2中使用注解配置Action方法详解

struts2中使用注解配置Action方法详解

struts2中使⽤注解配置Action⽅法详解使⽤注解来配置Action可以实现零配置,零配置将从基于纯XML的配置转化为基于注解的配置。

使⽤注解,可以在⼤多数情况下避免使⽤struts.xml⽂件来进⾏配置。

struts2框架提供了四个与Action相关的注解类型,分别为ParentPackage、Namespace、Result和Action。

ParentPackage:ParentPackage注解⽤于指定Action所在的包要继承的⽗包。

该注解只有⼀个value参数。

⽤于指定要继承的⽗包。

⽰例:使⽤ParentPackage注解,其value值为mypackage,表⽰所在的Action需要继承mypackage包,@ParentPackage(value="mypackage")public class UserAction extends ActionSupport{}如果注解中只有⼀个value参数值,或者其他参数值都使⽤默认值时,则可以对value参数设置进⾏简写,⽐如上述的代码:@ParentPackage("mypackage")public class UserAction extends ActionSupport{}把struts2-convention-pligin-2.x.x.jar包导⼊到web应⽤中,才能在Action类中使⽤注解。

Namespace:Namespace注解⽤于指定Action所在的包的命名空间。

该注解只有⼀个value参数,⽤于指定ACtion所属于的命名空间。

当使⽤Namespace注解时,在为命名空间取名需要使⽤斜杠(/)开头。

使⽤Namespace注解,指定其Action所在的包的命名空间为/user:@Namespace("/user")public class UserAction extends ActionSupport{}Result:Result注解⽤于定义⼀个Result映射,该注解包含四个参数,1)name:可选参数,⽤于指定Result的逻辑名,默认值为success2)location:必选参数,⽤于指定Result对应资源的URL3)type:可选参数,⽤于指定Result的类型,默认值为NullResult.class4)params:可选参数,⽤于为Result指定要传递的参数,格式为:{key1,value1,key2,value2,...}如果type参数的值为NullResult.class,那么struts2框架在解析Result配置时,会使⽤默认的结果类型(即ServletDispatcherResult)来替换NullResult。

Struts2配置Result

Struts2配置Result

配置Result一个result代表了一个可能的输出。

当一个Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出。

Results配置由两部分组成:一部分是result映射,另一部分是result类型。

1.结果映射在struts.xml文件中,使用result元素来配置result映射。

result元素有两个可选的属性:在Struts2中允许定义一个默认的Result类型,这是通过result-type元素来定义的。

在框架的默认配置文件struts-default.xml中,在struts-default.xml包中有如下的一个配置:<result-types><result-type name="dispatcher"class="org.apache.struts2.dispatcher.ServletDispatcherResult"default="true"/></result-types>result-type元素的default属性指定名为dispatcher的结果类型,dispatcher结果类型使用ServletAPI中的ResultDispatcher将请求导向到目标资源(通常是JSP页面)。

如果在使用result元素配置result映射时,没有使用type类型,那么框架就将使用默认的dispatcher类型。

由于Struts2中的包是可以继承的,所以我们定义的package只要继承了struts-default包,因此也继承了默认的结果类型,所以可以省略result元素的type属性。

如果没有指定result元素的name属性,那么框架将把它命名为”success”。

2.结果类型在框架调用Action对请求进行处理之后,就要向用户呈现一个结果视图,Struts2支持多种类型的视图,这些视图是由不同的结果类型来管理的。

result讲解

result讲解

Rsult是什么简单的说Result就是Action方法执行完毕之后返回的一串字符串,他指示出Action执行完之后的下一个页面在哪里,具体页面的位置是我们在struts.xml中配置的,就是<result>子元素,例如我们在前面UserAction中配置的Result:<action name="*User"class="erAction"method="{1}"><result name="input">/input.jsp</result><result name="success">success.jsp</result></action>在Struts2中内建了许多ResultType,他们都定义在strtus-default包中,我们可以在struts-default.xml中找到这些Result Type的定义虽然Strtus2内建支持这么多Result Type,但是一般情况下我们都用不到这么多,因此我们挑几个常用的学习一下,其他的可以在需要的时候再去查看文档。

Result的配置非常简单,<result>元素可以有name属性和type属性,但是两种属性都不是必须的。

1:配置name属性按照前面的讲述,name属性是用来跟Action的execute方法返回的字符串相对应的,用来指示Action运行后跳转到的下一个页面,因此name属性的值可以是任意字符串。

比如有如下的execute方法:java代码:public String execute() throws Exception {return "toWelcome";}那么,这里返回的“toWelcome”,在struts.xml里面就有如下的配置来对应:java代码:<action name="helloworldAction"class="cn.javass.action.action.HelloWorldAction"><result name="toWelcome">/s2impl/welcome.jsp</result></action>如果不设置的话,默认值为“success”,正好和Action中的“SUCCESS”这个常量相对应,那样的话,execute方法就应该返回SUCCESSDispatcher从是struts2.xml的配置中可以看出对于Dispatcher类型的Result Type被设置为默认使用的结果类型。

struts2中struts.xml文件详解

struts2中struts.xml文件详解

<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
</action>
</package>
</struts>
上面代码的两个动作的class属性都指向同一个类,name为这个类起了两个动作别名:test和my。在动作my中,使用了method属性指定要要运行的方法名为my。
在MyAction类中必须要有my方法,代码如下:
<struts>
<package name="demo" extends="struts-default"
>
<action name="test" class="action.MyAction">
</action>
<action name="my" class="action. MyAction" method="my">
<?xml version="1.0" encoding="UTF-8" ?>

Struts2的配置文件详解

Struts2的配置文件详解
Struts2的命名空间的作用等同于struts1里模块的作用。
1.3.包含配置:
在Struts2中可以将一个配置文件分解成多个配置文件,那么我们必须在struts.xml中包含其他配置文件。
<struts>
<includefile="struts-default.xml"/>
<includefile="struts-user.xml"/>
erName= userName;
}
/**
*@returnthepassword
*/
publicString getPassword() {
returnpassword;
}
/**
*@parampasswordthepasswordtoset
*/
publicvoidsetPassword(String password) {
</action>
</package>
</struts>
如上示例的配置,配置了一个名为default的包,该包下定义了一个Action。
1.2.命名空间配置:
考虑到同一个Web应用中需要同名的Action,Struts2以命名空间的方式来管理Action,同一个命名空间不能有同名的Action。
Struts2通过为包指定namespace属性来为包下面的所有Action指定共同的命名空间。
l public Map getSession():返回一个Map对象,该Map对象模拟了HttpSession实例。
l public void setSession(Map session):直接传入一个Map实例,将该Map实例里的key-value对转换成session的属性名-属性值对。

struts2中的result的type类型

struts2中的result的type类型
<param name="charSet">字符规范(如GBK)</param>
</result>
缺点:redirect把一个http返回码(SUCCESS)以及返回的页面位置一起重新发给web服务器,容纳后由web服务器产生一个新的HTTP请求,就会产生一个新的线程,保存在原来Action执行的线程中的数据就无法访问。
所以,result需要包含Action的数据,那么redirect不是一个可行的办法。因为新的HTTP请求时在Servlet容器的新的线程中处理的,ActionContext中的所有状态都不会存在。
struts2 跳转类型 result type=chain、dispatcher、redirect(redirect-action)
dispatcher 为默认跳转类型,用于返回一个视图资源(如:jsp)
Xml代码 :
<result name="success">/main.jsp</result>
<result name="success" type="chain">step2.action</result>
</action>
<action name="step2" class="test.Step2Action">
<result name="success">finish.jsp</result>
Xml代码:
<result name="err" type="redirect-action">

Struts2介绍

Struts2介绍

Struts2集成指南关于Struts2Struts是Apache软件基金会(ASF)赞助的一个开源项目。

它最初是Jakarta项目中的一个子项目,并在2004年3月成为ASF的顶级项目。

它通过采用Java Servlet/JSP技术,实现了基于Java EE Web应用的Model-View-Controller〔MVC〕设计模式的应用框架〔Web Framework〕,是MVC经典设计模式中的一个经典产品。

Struts,a history在Java EE的Web应用发展的初期,除了使用Servlet技术以外,普遍是在JavaServer Pages(JSP)的源代码中,采用HTML与Java代码混合的方式进行开发。

因为这两种方式不可避免的要把表现与业务逻辑代码混合在一起,都给前期开发与后期维护带来巨大的复杂度。

为了摆脱上述的约束与局限,把业务逻辑代码从表现层中清晰的分离出来,2000年,Craig McClanahan采用了MVC的设计模式开发Struts。

后来该框架产品一度被认为是最广泛、最流行JAVA的WEB应用框架。

Craig McClanahan2006年,WebWork与Struts这两个优秀的Java EE Web框架(Web Framework〕的团体,决定合作共同开发一个新的,整合了WebWork与Struts优点,并且更加优雅、扩展性更强的框架,命名为“Struts 2”,原Struts的1.x版本产品称为“Struts 1”。

至此,Struts项目并行提供与维护两个主要版本的框架产品——Struts 1与Struts 2。

Struts1 vs. Struts2侵入性Struts 1在编程方面是面向抽象类编程,而不是面向接口编程。

Struts 1要求自定义Action 类继承一个特定的抽象基类Action。

另一方面,Struts 1的 Action 依赖于 Servlet API,因为Struts 1 Action 的execute 方法中有HttpServletRequest 和HttpServletResponse 方法。

struts2配置文件详解

struts2配置文件详解
"/dtds/struts-2.0.dtd">
<struts>
<!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 标准的UI主题,默认的UI主题为xhtml,可以为simple,xhtml或ajax -->
<cosntant name="struts.ui.theme" value="xhtml" />
<!-- spring 托管 -->
<constant name="struts.objectFactory" value="spring" />
<!--
指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration
<!-- 设置默认的locale和字符编码 -->
<constant name="struts.locale" value="zh_CN" />
<constant name="struts.i18n.encoding" value="GBK" />
<!-- 该属性指定Struts 2文件上传中整个请求内容允许的最大字节数 -->

struts2

struts2
struts核心 包
加载struts2
要使用struts2,必须在web.xml中进行配置
以过滤器的形式加载 struts2
过滤器所在包: org.apache.struts2. dispatcher.FilterDi spatcher
加载struts2
以过滤器的形式加载struts2
struts2以过滤器的形式加载到工程中,在web.xml中配置: <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
struts2中的命名空间起到模块化的作用。这个包下 的所有Action(请求的处理类),都应该以: /[命名空间名]/[Action名]来访问 命名空间的名字都应该以“/”开头,如果不配置命名 空间,则默认的就是namespace=“/”。 一个包下不能存在名字相同的Action
struts2常用配置-Action配置
name属性配置Action的名字
class属性配置Action类的完整类路径,说明 用哪个类处理提交的请求。
struts2常用配置-Action配置
LoginAction.java public class LoginAction extends ActionSupport { private String userName; private String password; public void setUserName(String userName) { erName = userName; } public void setPassword(String password) { this.password = password; } public String execute() throws Exception { return SUCCESS; } } 每个请求处理类必须继 承ActionSupport 对应页面表单元素名 对应的表单元素名必须 给出set方法 处理请求的方法。方法 名默认为execute struts2中,所提交的请求 应以.action结尾

struts2注解总结----@Action和@Result

struts2注解总结----@Action和@Result

struts2注解总结----@Action和@Result除了使⽤配置⽂件配置之外,还能够使⽤注解来配置以下是⼀些经常使⽤的注解介绍:@Action/@Actions:@Action指定⼀个类为action,相应配置⽂件⾥的<action>....</action>标签,当中能够配置例如以下属性1. results:配置返回的结果集属性,相当于struts2中的<result>列表,能够在{}中配置属性,详细例如以下2. value:配置action的名字,相当于<action>中的name属性3. interceptorRefs:配置拦截器@Action能够定义在类上,也能够定义在⽅法上例如以下(@Result的作⽤后⾯讲,也能够和后⾯的配合着看)@Action(value = "testAction",results = {@Result(name="success",location="/success.jsp")})public class testAction extends ActionSupport {@Overridepublic String execute() throws Exception {return SUCCESS;}}这就相当于例如以下的xml配置<action name="testAction" class="struts2.action.testAction"><result name="success">/success.jsp</result></action>在xml配置中假设name不写,那么默认就是success,在注解中也是,假设results中的name不写。

Struts2 result返回类型

Struts2 result返回类型

Struts2 result返回类型(type)小结在struts2的返回结果配置中,我们大部分情况使用默认的或者chain或者redirect,其实struts2还有很多其他类型的,今天我们就来看一下都有哪些类型。

打开struts2的源码中struts-default.xml文件,我们能看到如下配置<package name="struts-default"abstract="true"><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"/><!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 --><result-type name="redirect-action"class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/><result-type name="plaintext"class="org.apache.struts2.dispatcher.PlainTextResult"/></result-types>下面我们就来一一介绍一下每个是做什么的。

struts2的配置文件

struts2的配置文件
t;
<!-- 定义默认的拦截器 每个Action都会自动引用,如果Action中引用了其它的拦截器,默认的拦截器将无效 -->
<default-interceptor-ref name="mystack"></default-interceptor-ref>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
当前路径为web-inf/classes
struts2的配置文件
类型
struts2共有4类配置文件,
struts.properties:定义框架自身的全局变量。
struts-default.xml:定义框架自身使用的action映射及result定义。
struts-plugin.xml:struts插件使用的配置文件,比如当使用struts和spring结合时就需要在web.xml中引用该配置文件。
-->
<package name="com.kay.struts2" extends="struts-default" namespace="/test">
<interceptors>
<!-- 定义拦截器-->
<interceptor name="timer" class="com.kay.timer"></interceptor>

xml文件中resultmap的用法

xml文件中resultmap的用法

文章标题:深度解析XML文件中ResultMap的用法在开发中,XML文件是一种常见的数据交换格式,而MyBatis框架中的ResultMap是对查询结果集的映射。

本文将深入探讨XML文件中ResultMap的用法,并结合具体实例进行讲解。

1. ResultMap的基本概念在MyBatis中,ResultMap是对象关系映射(ORM)中的重要组成部分。

它定义了如何将数据库查询结果映射到Java对象上。

ResultMap主要由id、type、result、association、collection等属性构成,通过这些属性的配置,可以实现复杂的结果映射。

2. ResultMap的用法在XML中定义ResultMap时,需要指定id和type属性,分别表示ResultMap的唯一标识和映射的Java类型。

而result、association、collection等属性则用于配置结果映射的细节。

在实际使用中,可以通过ResultMap的配置,实现一对一、一对多等复杂的映射关系。

3. 实例分析假设有一个学生表和课程表,学生表中包含学生的基本信息,课程表中包含学生选修的课程信息。

通过ResultMap的配置,可以将学生表和课程表的查询结果映射到Java对象上,实现对学生及其选修课程的全面查询和展示。

4. 结合实际案例在实际开发中,我们经常会遇到复杂的查询需求,通过ResultMap的配置,可以灵活实现对多表关联查询的结果映射。

这样既可以减少开发人员的工作量,又可以提高程序的性能和可维护性。

总结回顾通过对XML文件中ResultMap的深入探讨,我们了解了ResultMap 的基本概念、用法和实例分析。

我们也明白了ResultMap在实际开发中的重要性和应用场景。

通过合适的ResultMap配置,可以实现复杂查询结果的灵活映射,为程序的性能和可维护性提供了保障。

个人观点在实际开发中,我认为XML文件中ResultMap的用法是非常重要的。

result中类型

result中类型

struts 中的 result 有哪些常用的类型:一、 dispatcher (1)为缺省的 result 类型,一般情况下我们在 struts.xml 会这么写: <result name="success">/main.jsp</result> 以上写法使用了两个默认,其完整的写法为: # <result name="success" type="dispatcher"> # <param name="location">/maini.jsp</param> # </result> 第一个默认:type="dispatcher";第二个默认:设置的为 location 参数,location 只能是页面,不能是另一个 action(可用 type="chain"解决) 。

(2)实现方式 从 doExecute 方法看出,有三个出口(finalLocation 为要跳转的地址) : pageContext.include(finalLocation); dispatcher.forward(request, response); (dispatcher 是根据 finalLocation 创建的) dispatcher.include(request, response); 而我们知道,forward 与 include 都是转发到 context 内部的资源。

二、redirect (1)可以重定向到一个页面,另一个 action 或一个网址。

# <result name="success" type="redirect">aaa.jsp</result> # <result name="success" type="redirect">bbb.action</result> # <result name="success" type="redirect"></result> (2)实现方式: 查看 doExecute 方法,只有一个出口: response.sendRedirect(finalLocation); sendRedirect 是重定向,是重新产生一个 HTTP 请求到服务器,故重定向后其原 来所在的 action 上下文就不可用了。

mybatisMapper的xml文件中resultType值的使用说明

mybatisMapper的xml文件中resultType值的使用说明

mybatisMapper的xml⽂件中resultType值的使⽤说明⽬录Mapper的xml⽂件中resultType值①返回⼀般数据类型的值②当返回类型是javaBean③当返回是List类型④返回类型数Map结构⑤说⼀下关于mybatis⾥⾯mapper层中传⼊多个参数的⽅法mybatis学习之resultType解析总结1、对象类型2、List类型3、Map类型Mapper的xml⽂件中resultType值①返回⼀般数据类型的值⽐如根据id或者字段条件查询获取表中的某⼀个字段值User Sel(int id); //根据id查询SQL映射⽂件<select id="Sel" resultType="ng.String"> //注意这个写了类全名select username from user_test where id = #{id}</select>如果需要简写的话需要定义resultType的别名java 的基本类型不需要别名的:别名映射的类型_byte byte_long long_short short_int int_boolean booleaninteger Integerstring Stringdate Dateboolean Boolean②当返回类型是javaBean<select id="Sel" resultType="er">select * from user_test where id = #{id}</select><typeAliases><!-- 针对单个别名定义 type:类型的路径 alias:别名 --><typeAlias type="er" alias="user"/>③当返回是List类型有时候我们需要模糊查询或者是全表查询,返回的数据是多条的,那么可以把多条数据保存到list⾥⾯的。

Result结果类型详解

Result结果类型详解

Result结果类型详解1. 配置Result在 struts.xml ⽂件中,<result> 元素⽤于配置 Result 逻辑视图与物理视图之间的映射关系,它有两个可选属性 name 和 type。

其中,name 属性⽤于指定逻辑视图的名称,默认值为 success;type 属性⽤于指定返回的视图资源的类型,不同的类型代表不同的结果输出,它的默认值是 dispatcher。

<action name="loginAction" class="com.mengma.action.LoginAction"><result name="success" type="dispatcher"><param name="location">/success.jsp</param></result></action><!-- Action 配置了⼀个 name 为 success 的 Result 映射,该映射的值可以是 JSP 页⾯,也可以是⼀个 Action 的 name 值; 这⾥使⽤ param ⼦元素为其指定了 Result 映射对应的物理视图资源为 success.jsp。

--><param> ⼦元素的 name 属性有两个值:location:指定该逻辑视图所对应的实际视图资源。

parse:指定在逻辑视图资源名称中是否可以使⽤ OGNL(对象图导航语⾔)表达式。

默认值为 true,表⽰可以使⽤,如果设为false,则表⽰不⽀持。

简化上⾯的代码:<action name="loginAction" class="com.mengma.action.LoginAction"><result>/success.jsp</result></action>需要注意的是,在 Result 配置中指定实际资源位置时,可以使⽤绝对路径,也可以使⽤相对路径。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一.struts2.xml 中result type属性说明
1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息。

com.opensymphony.xwork2.ActionChainResult
2.dispatcher:用来转向页面,通常处理JSP
org.apache.struts2.dispatcher.ServletDispatcherResult
3.freemaker:处理FreeMarker模板
org.apache.struts2.views.freemarker.FreemarkerResult
4.httpheader:控制特殊HTTP行为的结果类型
org.apache.struts2.dispatcher.HttpHeaderResult
5.stream:向浏览器发送InputSream对象,用来处理文件下载,还可用于返回AJAX数据
org.apache.struts2.dispatcher.StreamResult
6.velocity :处理V elocity模板
org.apache.struts2.dispatcher.V elocityResult
7.xsl:
处理XML/XLST模板
org.apache.struts2.views.xslt.XSLTResult
8.plainText:显示原始文件内容,例如文件源代码
org.apache.struts2.dispatcher.PlainTextResult
9.plaintext:显示原始文件内容,例如文件源代码
org.apache.struts2.dispatcher.PlainTextResult
10.redirect:重定向到一个URL ,被跳转的页面中丢失传递的信息,如request
org.apache.struts2.dispatcher.ServletRedirectResult
11.redirectAction :重定向到一个Action ,跳转的页面中丢失传递的信息,如request
org.apache.struts2.dispatcher.ServletActionRedirectResult
12.redirect-action:重定向到一个Action ,跳转的页面中丢失传递的信息,如request
org.apache.struts2.dispatcher.ServletActionRedirectResult
二.redirect与redirect-action区别
一、使用redirect需要后缀名使用redirect-action不需要后缀名
二、type="redirect"的值可以转到其它命名空间下的action,而redirect-action只能转到同一命名空下的action,因此它可以省略.action的后缀直接写action的名称。

如:
<result name="success" type="redirect">viewTask.action</result>
<result name="success" type="redirect-action">viewTask</result>
附:redirect-action 传递参数
Xml代码
<action name ="enterpreinfo" class = "preinfoBusinessAction" method = "enterPreinfoSub" > < result name = "success" type = "redirect -action" >
showpreinfo?preinfo.order_number=${preinfo.order_number}&amp;pany_name =${pany_name}
</ result >
< result name = "error"type = "redirect " >
< param name = "location" > /error.jsp </ param >
</ result >
</ action >
因为使用了redirect -action,所以要注意不能将showpreinf?preinfo.order_number=${preinfo.order_number}写成showpreinf.action?preinfo.order_number=${preinfo.order_number}
其中${}为EL表达式,获取action:enterpreinfo中属性的值;在这个配置文件里,多个参数的连接符使用了"&amp;",但XML的语法规范,应该使用"&amp;"代替"&",原理和HTML 中的转义相同,开始没有注意,在struts 分析配置文件时,总是报出这样的错误:
The reference to entity "preinfo" must end with the ';' delimiter.
The reference to entity "preinfo" must end with the ';' delimiter.
进行上面说明的替换后,就正常了。

相关文档
最新文档