一、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的更多相关文章

  1. Struts2 配置文件result的name属性和type属性

    Struts2 配置文件result的name属性和type属性:Name属性SUCCESS:Action正确的执行完成,返回相应的视图,success是 name属性的默认值: NONE:表示Act ...

  2. Struts2 中result type属性说明

    Struts2 中result type属性说明 首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-t ...

  3. Struts2之Result详解

    上一篇我们把Struts2中的Action接收参数的内容为大家介绍了,本篇我们就一起来简单学习一下Action的4种Result type类型,分为:dispatcher(服务端页面跳转):redir ...

  4. struts2 action result type类型

    struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...

  5. struts2的@Result annotation 如何添加params,并且在页面取值

    http://www.bubuko.com/infodetail-2492575.html .............................................. 标签:lai  ...

  6. Struts2配置Result(Struts2_result)

    一.概要 二.常用四种类型的配置 Struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!D ...

  7. 【struts2】Result和ResultType

    简单的说,Result是Action执行完后返回的一个字符串,它指示了Action执行完成后,下一个页面在哪里.Result仅仅是个字符串,仅仅是用来指示下一个页面的,那么如何才能够到达下一个页面呢? ...

  8. Struts2(六):ResultType

    本章节将继续学习struts2的返回类型的使用方法. 学习文档下载struts2 full包解压后会在doc下包含离线html文档. 点击运行后页面: 点击Guides向导终将会有向导列表 再点开后, ...

  9. struts2的result的type属性

    一共有两个属性name和type name这里就不介绍了 type    返回结果的类型,值可以从default-struts.properties中看到看到 常用的值:dispatcher (默认) ...

随机推荐

  1. spring @Transactional注解无效

    <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jd ...

  2. bio、nio、aio及select、poll、epoll

    BIO与NIO.AIO的区别http://blog.csdn.net/skiof007/article/details/52873421 select.poll.epoll之间的区别总结http:// ...

  3. linux下面mmap和setsignal函数用法

    #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <fcntl.h& ...

  4. Error : The specified component was not reported by the VSS writer (Error 517) in Windows Server 2012 Backup

    Error : The specified component was not reported by the VSS writer (Error 517) in Windows Server 201 ...

  5. delphi win64 DEBUG不能进预设断点的问题

    delphi win64 DEBUG不能进预设断点的问题  delphi win64,debug模式下运行,如果含有中文路径,不能进断点,音频跟踪.而同样的代码,DELPHI WIN32却没有这个问题 ...

  6. MyEclipse Web项目调试

    1.发布项目 2.启动服务 服务有两种启动方式,Run Server和Debug Server Run Server是运行模式,Debug Server是调试模式 使用Debug Server模式启动 ...

  7. 转 dockerfile 介绍 及 编写

    Docker简介 Docker项目提供了构建在Linux内核功能之上,协同在一起的的高级工具.其目标是帮助开发和运维人员更容易地跨系统跨主机交付应用程序和他们的依赖.Docker通过Docker容器, ...

  8. linux下so获得自己文件位置的路径

    打开这个设备/proc/self/maps 返回的就是这个进程当前使用的so列表 cat /proc/self/maps00400000-0040b000 r-xp 00000000 08:01 14 ...

  9. JSON.parse()和jQuery.parseJSON()的区别

    jQuery.parseJSON(jsonString) : 将格式完好的JSON字符串转为与之对应的JavaScript对象   (jquery 方法) 1 2 3 var str = '[{&qu ...

  10. Gunicorn+Flask中重复启动后台线程问题

    假设程序如下: if __name__ == '__main__': t = Thread(target=test) t.start() app.run(host='0.0.0.0',port=808 ...