result 是action节点的子节点

result 代表action方法执行后,可能去的一个目的地

一个action节点可以配置多个result子节点。

result的name属性值对应着action方法可能有的一个返回值。

result有两个属性,还有一个是type它表示结果的响应类型

result的type属性值在struts-default包的result-types节点的name属性中定义,

常用的有:

dispatcher(默认值):转发,同Servlet中的转发。

redirect:重定向

redirectAction重定向到一个Action,

注意:通过redirect的响应类型也可以便捷的实现redirectAction

chain:转发到一个Action

注意:不能通过redirect的响应类型也可以便捷的实现转发到一个Action

看代码:

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>
<!-- 配置Struts2可以受理的请求的扩展名 -->
<constant name="struts.action.extension" value="action,do"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="login-ui">
<result>/login.jsp</result>
</action> <action name="user-login" class="logan.struts.study.UserAction">
<result name="login-success">/login-success.jsp</result>
</action> <action name="logout" class="logan.struts.study.UserAction"
method="logout">
<result name="logout-success">/login.jsp</result>
</action> <action name="testResult" class="logan.struts.study.TestResultAction"
>
<result name="success">/success.jsp</result>
<!-- 重定向到一个Action -->
<result name="index" type="redirectAction">
<param name="actionName">testAction</param>
<param name="namespace">/logan</param>
</result> <!-- 通过redirect的响应类型也可以便捷的实现redirectAction
<result name="index" type="redirect">/logan/testAction.do</result>
-->
<result name="login" type="redirect">/login.jsp</result>
<result name="test" type="chain">
<param name="actionName">testAction</param>
<param name="namespace">/logan</param> </result>
</action> </package>
<package name="testPackage" namespace="/logan" extends="struts-default">
<action name="testAction" class="logan.struts.study.TestAction">
<result>/success.jsp</result>
</action>
</package> </struts>
package logan.struts.study;

public class TestResultAction {
private int number; public void setNumber(int number) {
this.number = number;
} public String execute(){ String result = null; if(number%4 == 0){
result = "success";
}else if(number%4 == 1){
result = "login";
}else if(number%4 == 2){
System.out.println("2");
result = "index";
}else if(number%4 == 3){
result = "test";
} return result;
} }
package logan.struts.study;

public class TestAction {

    public String execute(){
System.out.println("TestAction's execute...");
return "success";
} }
package logan.struts.study;

import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware; public class UserAction implements SessionAware,ApplicationAware{ private Map<String, Object> session = null;
private Map<String, Object> application = null; private String username; public void setUsername(String username) {
this.username = username;
} public String logout(){
//数量减一
Integer count = (Integer) application.get("count");
if(count != null && count >0){
count--;
application.put("count", count);
}
count--; //session失效
((SessionMap)session).invalidate(); return "logout-success";
} public String execute(){ //把用户信息存入Session域中 //1.获取session,通过实现RequestAware接口 //获取登录信息
session.put("username", username);
//把用户信息存入Session域中 //
Integer count = (Integer) application.get("count");
if(count == null){
count = 0;
}
//2.使当前在线人数 + 1
count++;
application.put("count", count); return "login-success";
} @Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session; } @Override
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
this.application = application; } }

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="login-ui.do">LoginUI</a>
<br><br>
<a href="testResult.do?number=4">Test ActionSupport</a>
</body>
</html>

login-success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome:${sessionScope.username }
<br><br>
Count on:${applicationScope.count }
<br><br>
<a href="logout.do">Logout</a>
</body>
</html>

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="user-login.do" method="post">
username:<input type="text" name="username">
<input type="submit" value="Login">
</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h4>Success Page</h4>
</body>
</html>

Struts2学习第七课 result的更多相关文章

  1. Struts2学习第七课 OGNL

    request变成了struts重写的StrutsRequestWrapper 关于值栈: helloWorld时,${productName}读取productName值,实际上该属性并不在requ ...

  2. Struts2学习第七课 ActionSupport

    com.opensymphony.xwork2.ActionSupport类是默认的Action类,如果某个Action节点没有配置class属性,则ActionSupport即为待执行的Action ...

  3. Struts2学习第七课 动态方法调用

    动态方法调用:通过url动态调用Action中的方法. action声明: <package name="struts-app2" namespace="/&quo ...

  4. Struts2学习第七课 通配符映射

    一个WEB应用可能有长百上千个action声明,可以利用struts提供的通配符映射机制吧多个彼此相识的映射关系简化为一个映射关系. 通配符映射规则: --若找到多个匹配,没有通配符的那个将胜出(精确 ...

  5. Python学习第七课

    Python学习第七课 'Alex' "Alex"print('hello'*5) #重复输出字符串 print('hellowold'[2:]) #类似于切片操作:会取出 llo ...

  6. Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer

    原文:Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处 ...

  7. Struts2学习笔记(七)——类型转换

    1.自动类型转换 Struts2内部提供大量类型转换器,用来完成数据类型转换问题: String和boolean.Boolean:完成字符串与布尔值之间的转换 String和char.Characte ...

  8. Struts2学习第八课 声明式异常处理

    异常处理:exception-mapping元素 exception-mapping元素:配置当前的action的声明式异常处理 exception-mapping元素有两个属性: --excepti ...

  9. Struts2学习第六课 实现登录登出功能

    关于Struts2请求的扩展名问题: 1).org.apache.struts2包下的default.properties中配置了struts2应用的一些常量 2).struts.action.ext ...

随机推荐

  1. 前端开发笔记--flex布局

    flex布局: 个人觉得flex布局比起传统布局要优先得多(主要是容易使用),缺点是IE10及以上版本才能使用,甚至某些属性只有在IE11才能使用(而且我发现凡是不兼容主要IE的坑来的多,不是说其他浏 ...

  2. apache下配置多域名多目录的应用

    引言:阿里云centos apache web服务器中配置不同域名访问不同的目录,达到类似增加虚拟主机的效果: 案例: 如有2个www.a.com ,www.b.com 域名, 访问www.a.com ...

  3. hadoop-2.0.0-cdh4.6.0、sqoop-1.4.3-cdh4.6.0、mahout-0.7-cdh4.6.0 安装笔记

    1. /etc/profile中环境变量配置: export HADOOP_HOME=/usr/local/hadoop/cdh4. export HADOOP_MAPRED_HOME=${HADOO ...

  4. 本地建立SVN服务器

    想在自己电脑上搭建SVN服务器,于是有以下步骤. 首先明确SVN服务包括服务器和客户端,平时听到的TortoiseSVN就是一个客户端. 首先下载两个软件,服务器端我使用的是VisualSVN,版本是 ...

  5. Qt中 QTableWidget用法总结

    转自--> http://edsionte.com/techblog/archives/3014 http://hi.baidu.com/fightiger/item/693aaa0f0f87d ...

  6. adb命令(一)

    针对移动端 Android 的测试, adb 命令是很重要的一个点,必须将常用的 adb 命令熟记于心, 将会为 Android 测试带来很大的方便,其中很多命令将会用于自动化测试的脚本当中. And ...

  7. mybatis学习第(二)天

    Mybatis第二天    高级映射   查询缓存 关于与spring的整合和反转工程我偷懒了,下次看. 使用的sql: CREATE TABLE USER( id INT PRIMARY KEY A ...

  8. python-管理MySQL之ConfigParser模块

    1.拷贝2.7版本的ConfigParser.py模块支持无值解析 cp /usr/local/src/Python-2.7.5/Lib/ConfigParser.py /usr/lib/python ...

  9. 实现两个窗口通信方法-postMessage

    此方案可解决跨域而且跨Iframe,而且http和https之间的交互 首先来看一下基本的语法 otherWindow.postMessage(message, targetOrigin, [tran ...

  10. 2017-2018-1 20179203 《Linux内核原理与分析》第八周作业

    攥写人:李鹏举 学号:20179203 ( 原创作品转载请注明出处) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...