一、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. Codeforces 505A Mr. Kitayuta's Gift 暴力

    A. Mr. Kitayuta's Gift time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. Codechef December Challenge 2014 Chef and Apple Trees 水题

    Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...

  3. antd 父组件获取子组件中form表单的值

    还是拿代码来讲吧,详情见注释 子组件 import React, { Component } from 'react'; import { Form, Input } from 'antd'; con ...

  4. PHP实现文件下载的核心代码

    PHP实现文件下载的核心代码:

  5. composer安装Workerman报错:Installation failed, reverting ./composer.json to its original content.

    今天想在TP5上安装workerman,实现一个后台消息提醒功能. 第一步就卡住了,根据手册里说的首先通过composer安装 $ composer require topthink/think-wo ...

  6. 【机器学习算法-python实现】决策树-Decision tree(2) 决策树的实现

    (转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景      接着上一节说,没看到请先看一下上一节关于数据集的划分数据集划分.如今我们得到了每一个特征值得 ...

  7. ICD2 VPP limiter for new PIC microcontrollers.

    http://www.circuitsathome.com/mcu/pic_vpp_limiter VOUT = 2.5V * ( 1 + 24/10 ) = 2.5 * 3.4 = 8.5V New ...

  8. Android native层动态库注射

    1.简单介绍 本文解说在Android native层.root权限下.注射动态库到目标进程,从而hook目标进程中动态库的函数的实现方式. 文中的源代码所有来源于网络.我仅仅是略微加以整理. 环境: ...

  9. 各种语言的注释总结--from wiki

    Comments can be classified by: style (inline/block) parse rules (ignored/interpolated/stored in memo ...

  10. 使用开源库 EasyTimeline 操作定时器 NSTimer

    EasyTimeline https://github.com/mmislam101/EasyTimeline Sometimes you need things to happen at speci ...