Struts2(六)result
一、result简述
result:输出结果;第个Action返回一个字符串,Struts2根据这个值来决定响应结果
name属性:result的逻辑名。和Actin里的返回值匹配,默认"success"
值 :指定对应的实际资源位置
二、Action中返回其它值
如果Action中返回其它扯,result中的Name属性要与之对应才可以找到指定的资源
Action默认定义了一些常量,可以拿来使用
package com.opensymphony.xwork2;
public interface Action {
public static final String SUCCESS = "success";
public static final String NONE = "none";
public static final String ERROR = "error";
public static final String INPUT = "input";
public static final String LOGIN = "login";
}

三、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>
这个配置显示了几种常见的结果类型,关于这些以及其他结果类型,各自作用简介如下:
dispatcher: 将请求转发(forward)到本应用程序中指定的资源(JSP 或Servlet)
chain: Action 链式处理,将请求转发(forward)到指定的Action
redirect: 请求重定向到指定的 URL(页面或者Action)
redirectAction: 请求重定向到指定的Action
json:实现Ajax 时返回JSON 对象
freemarker:处理FreeMarker 模板
httpheader:控制特殊HTTP 行为的结果类型
stream:像浏览器发送InputStream 对象,通常用来处理文件下载,还可用于返回Ajax
数据
velocity:处理Velocity 模板
xslt:处理XML/XLST 模板
plainText:显示原始文件内容,例如文件源代码
package com.pb.web.action;
import com.opensymphony.xwork2.ActionSupport;
import com.pb.entity.User;
/*
* 登录响应action 以javaBean方式接收用户登录输入的用户名和密码
*/
public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L;
//实例化对象
private User user; //要有execute方法
public String execute(){
return SUCCESS;
}
public String login(){
if(user.getUserName().equals("accp") && user.getPassWord().equals("accp")){
return SUCCESS;
}else{
return INPUT;
}
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} }
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.pb.web.action.LoginAction" method="login">
<result name="success" type="redirect">
/loginSuccess.jsp
</result>
<result name="input" type="dispatcher">
/login.jsp
</result>
</action>
<package name="user" namespace="/user" extends="struts-default">
<action name="login" class="com.pb.web.action.UserAction">
<result type="chain">
<param name="actionName"> houseAction </param>
<param name="namespace"> / house </param>
</result>
</action>
</package>
<package name="house" namespace="/house" extends="struts-default">
<action name=" houseAction " class="com.pb.web.action.HouseAction">
<result>
/houseSuccess.jsp
</result>
</action>
</package>

四、全局结果

package com.pb.web.action;
import com.opensymphony.xwork2.ActionSupport;
public class HourseAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
public String add(){
System.out.println("执行添加操作!");
try{
if(1==1){
//调用service的方法
throw new Exception();
}
}catch (Exception e){
e.printStackTrace();
return ERROR;
}
return "success";
}
public String update(){
System.out.println("执行更新操作!");
try{
if(1==1){
//调用service的方法
throw new Exception();
}
}catch (Exception e){
e.printStackTrace();
return ERROR;
}
return "success";
}
public String delete(){
System.out.println("执行删除操作!");
try{
if(1==1){
//调用service的方法
throw new Exception();
}
}catch (Exception e){
e.printStackTrace();
return ERROR;
}
return "success";
}
}
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<!-- <action name="hourse_*" class="com.pb.web.action.HourseAction" method="{1}">
<result name="success" type="dispatcher">
{1}success.jsp
</result>
</action> -->
<action name="hourse_add" class="com.pb.web.action.HourseAction" method="add">
<result name="success" type="dispatcher">
/loginSuccess.jsp
</result>
</action>
<action name="hourse_update" class="com.pb.web.action.HourseAction" method="update">
<result name="success" type="dispatcher">
/loginSuccess.jsp
</result>
</action>
<action name="hourse_delete" class="com.pb.web.action.HourseAction" method="delete">
<result name="success" type="dispatcher">
/loginSuccess.jsp
</result>
<result name="error">/error1.jsp</result>
</action>
</package>


五、动态结果

<%@ 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>登录页面</title>
</head>
<body>
<form action="user/login.action" method="post">
<table>
<tr>
<td>用户名:</td>
<!--这里的name要和提交的地址中声明的实体类.属性来用 -->
<td><input type="text" name="user.username" /></td>
</tr>
<tr>
<td>密码:</td>
<!--这里的name要和提交的地址中声明的实体类.属性来用 -->
<td><input type="password" name="user.password" /></td>
</tr>
<tr>
<td><input type="submit" value="登录" /></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>管理员用户页面</title>
</head>
<body>
管理员用户页面 </body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>普通用户</title>
</head>
<body>
普通用户页面
</body>
</html>
User实体类
package com.pb.enity;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserAction
package com.pb.web.action; import com.opensymphony.xwork2.ActionSupport;
import com.pb.enity.User; public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L;
private User user;
//下一个中转的Action
private String nextDispose;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getNextDispose() {
return nextDispose;
}
public void setNextDispose(String nextDispose) {
this.nextDispose = nextDispose;
} public String login(){
//用户是admin就转到adminAction
if(user.getUsername().equals("admin") && user.getPassword().equals("admin")){
nextDispose="admin";
return SUCCESS;
//用户是admin就转到commonAction
}else if(user.getUsername().equals("common") && user.getPassword().equals("common")){
nextDispose="common";
return SUCCESS;
//其它就跳转到登录页面
}else{
return INPUT;
}
} }
CommonAction
package com.pb.web.action;
import com.opensymphony.xwork2.ActionSupport;
public class CommonAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
AdminAction
package com.pb.web.action;
import com.opensymphony.xwork2.ActionSupport;
public class AdminAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <!-- Add packages here -->
<package name="user" namespace="/user" extends="struts-default">
<action name="login" class="com.pb.web.action.UserAction" method="login">
<!-- 通过${}取出在UserAciton中定义的变量对应下面的action name -->
<result name="success" type="redirectAction">${nextDispose}</result>
<result name="input">/login.jsp</result>
</action>
<action name="admin" class="com.pb.web.action.AdminAction">
<result name="success">/admin.jsp</result>
</action>
<action name="common" class="com.pb.web.action.CommonAction">
<result name="success">/common.jsp</result>
</action>
</package> </struts>

Struts2(六)result的更多相关文章
- Struts2 配置文件result的name属性和type属性
Struts2 配置文件result的name属性和type属性:Name属性SUCCESS:Action正确的执行完成,返回相应的视图,success是 name属性的默认值: NONE:表示Act ...
- Struts2 中result type属性说明
Struts2 中result type属性说明 首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-t ...
- Struts2之Result详解
上一篇我们把Struts2中的Action接收参数的内容为大家介绍了,本篇我们就一起来简单学习一下Action的4种Result type类型,分为:dispatcher(服务端页面跳转):redir ...
- struts2 action result type类型
struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...
- struts2的@Result annotation 如何添加params,并且在页面取值
http://www.bubuko.com/infodetail-2492575.html .............................................. 标签:lai ...
- Struts2配置Result(Struts2_result)
一.概要 二.常用四种类型的配置 Struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!D ...
- 【struts2】Result和ResultType
简单的说,Result是Action执行完后返回的一个字符串,它指示了Action执行完成后,下一个页面在哪里.Result仅仅是个字符串,仅仅是用来指示下一个页面的,那么如何才能够到达下一个页面呢? ...
- Struts2(六):ResultType
本章节将继续学习struts2的返回类型的使用方法. 学习文档下载struts2 full包解压后会在doc下包含离线html文档. 点击运行后页面: 点击Guides向导终将会有向导列表 再点开后, ...
- struts2的result的type属性
一共有两个属性name和type name这里就不介绍了 type 返回结果的类型,值可以从default-struts.properties中看到看到 常用的值:dispatcher (默认) ...
随机推荐
- codecombat js
#1 // Move to the gem. // Don't touch the walls! // Type your code below. this.moveRight(); this.mov ...
- C#高级编程9-第10章 集合
集合 1.集合接口和类型 接口 说明 IEnumerable<T> 如果foreach语句用于集合,就需要IEnumerable接口.这个借口定义了方法GetEnumerator(),他返 ...
- Qt线程外使用Sleep
一:方法1 QTime t; t.start(); while(t.elapsed()<1000){ QCoreApplication::processEvents();} 二:方法2 ...
- oracle开发学习篇之集合运算符以及集合异常捕获
--取出集合;长度 declare type list_nested ) not null; v_all list_nested := list_nested('a','b','c','d','c', ...
- [MySql]默认密码的查找与修改
摘要 在安装成功后,怎么找到mysql的默认密码,折腾很长时间,最后发现在安装的过程中,产生了一个默认的随机密码. 密码 在mysql安装目录生成的data文件下,查找xxx.err的文件如图: 用记 ...
- linux 查找文件命令
find -name 文件名 在当前目录下查找 find -name nginx.conf
- msgpack和protobuf的对比
msgpack和protobuf的对比 msgpack的序列化速度比protobuf要快一些,但反序列化要比protobuf要慢一些,但总体都接近msgpack可以直接序列化类对象,但protobuf ...
- Android APP分享功能实现
[Android应用开发详解]第01期:第三方授权认证(一)实现第三方授权登录.分享以及获取用户资料 由于公司项目的需要,要实现在项目中使用第三方授权登录以及分享文字和图片等这样的效果,几经波折, ...
- mr
大数据技术 —— MapReduce 简介 本文为senlie原创,转载请保留此地址:http://www.cnblogs.com/senlie/ 1.概要很多计算在概念上很直观,但由于输入数据很大, ...
- .NET:如何实现 “热插拔”?
背景 如果某个“功能”需要动态更新?这种动态更新,可能是需求驱动的,也可能是为了修改 BUG,面对这种场景,如何实现“热插拔”呢?先解释一下“热插拔”:在系统运行过程动态替换某些功能,不用重启系统进程 ...