第八篇——Struts2的处理结果类型
Struts2处理结果类型
1、SUCCESS:表示Action正确的执行完成,返回相应的视图,success是name属性的默认值;
2、ERROR:表示Action执行失败,返回到错误处理视图;
3、NONE:表示Action正确的执行完成,但是不返回任何视图;
4、LOGIN:Action因为用户没有登录的原因没有正确执行,将返回登录视图,要求用户进行登录验证;
5、INPUT:Action执行,需要从前端页面获取参数,input就是代表这个参数输入的界面,一般应用中会对这些参数进行验证,如果验证没有通过,将自动返回该视图。
项目实例:
1、项目结构
2、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ray</groupId>
<artifactId>struts2Test</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>struts2Test Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.16</version>
</dependency>
</dependencies>
<build>
<finalName>struts2Test</finalName>
</build>
</project>
3、web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
<display-name>Archetype Created Web Application</display-name>
<!-- 过滤所有请求交给Struts2处理 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<!--action后缀(方法二)-->
<!--<init-param>-->
<!--<param-name>struts.action.extension</param-name>-->
<!--<param-value>ray</param-value>-->
<!--</init-param>-->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4、UserAction.java
package com.ray.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* Created by Ray on 2018/3/26 0026.
**/
public class UserAction extends ActionSupport {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String save(){
return SUCCESS;
}
@Override
public void validate() {
if(username == null || "".equals(username)){
this.addFieldError("username","用户名不能为空");
}
}
}
5、eighth-struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/">
<action name="user" class="com.ray.action.UserAction" method="save">
<result>/loginSuccess.jsp</result>
<result name="input">/user.jsp</result>
</action>
</package>
<constant name="struts.custom.i18n.resources" value="messageResource"/>
</struts>
6、struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- action后缀(方法一) -->
<!--<constant name="struts.action.extension" value="ra"/>-->
<package name="default" namespace="/" extends="struts-default">
<!-- 默认action -->
<default-action-ref name="404"/>
<action name="404">
<result>/404.jsp</result>
</action>
<action name="helloWorld" class="com.ray.action.HelloWorldAction">
<result name="success">/success.jsp</result>
</action>
</package>
<include file="second-struts.xml"/>
<include file="third-struts.xml"/>
<include file="fourth-struts.xml"/>
<include file="seventh-struts.xml"/>
<include file="eighth-struts.xml"/>
</struts>
7、messageResource.properties
# 方式一:统一指定类型转换失败的错误提示信息 {0}表示输入框的name值
#xwork.default.invalid.fieldvalue={0} failure
# 方式二:单独指定某个输入框类型转换失败的提示信息,中文需转换为对应的ASCII码,否则页面会乱码
invalid.fieldvalue.age = \u5e74\u9f84\u8f93\u5165\u683c\u5f0f\u6709\u8bef
8、user.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="user" method="POST">
<s:textfield label="姓名:" name="username"/>
<s:fielderror fieldName="username"/>
<br>
<s:textfield label="年龄:" name="age"/>
<s:fielderror fieldName="age"/>
<br>
<s:submit label="提交"/>
</s:form>
</body>
</html>
9、loginSuccess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title成功页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
保存成功!
</body>
</html>
10、页面效果
ok!
第八篇——Struts2的处理结果类型的更多相关文章
- Python之路【第十八篇】:Web框架们
Python之路[第十八篇]:Web框架们 Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Pytho ...
- 第八篇 :微信公众平台开发实战Java版之如何网页授权获取用户基本信息
第一部分:微信授权获取基本信息的介绍 我们首先来看看官方的文档怎么说: 如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑. 关于网页授权回调域 ...
- struts2支持的结果类型
在struts2-core.jar/struts-default.xml中,我们可以找到关于result-type的一些配置信息,从中可以看出struts2组件默认为我们提供了这 些result-ty ...
- 第八篇 Replication:合并复制-How it works
本篇文章是SQL Server Replication系列的第八篇,详细内容请参考原文. 在这一系列的前几篇你已经学习了如何在多服务器环境中配置合并复制.这一篇将介绍合并代理并解释它在复制过程中扮演的 ...
- 第八篇 Integration Services:高级工作流管理
本篇文章是Integration Services系列的第八篇,详细内容请参考原文. 简介在前面两篇文章,我们创建了一个新的SSIS包,学习了SSIS中的脚本任务和优先约束,并检查包的MaxConcu ...
- 第八篇 SQL Server安全数据加密
本篇文章是SQL Server安全系列的第八篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...
- 第八篇 SQL Server代理使用外部程序
本篇文章是SQL Server代理系列的第八篇,详细内容请参考原文 在这一系列的上一篇,学习了如何用SQL Server代理作业活动监视器监控作业活动和查看作业历史记录.在实时监控和管理SQL Ser ...
- 【强烈强烈推荐】《ORACLE PL/SQL编程详解》全原创(共八篇)--系列文章导航
原文:[强烈强烈推荐]<ORACLE PL/SQL编程详解>全原创(共八篇)--系列文章导航 <ORACLE PL/SQL编程详解> 系列文章目录导航 ——通过知识共享树立个人 ...
- 【译】第八篇 SQL Server安全数据加密
本篇文章是SQL Server安全系列的第八篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...
随机推荐
- http 请求 post get 长度限制
一.问题起因在某项目释放后Bug统计的附件<释放后问题>里有: 问题 原因 分析 备注 CSV处理时,如果处理的主题数过多,发生URL参数上限的错误: 可变长度的参数通过UR ...
- 我们正在招聘java工程师,想来美团工作吗?
我们希望你有? 1.3年以上Java服务器开发经验,精通Java及面向对象设计开发,熟悉主流web框架 2.熟悉网络编程,熟悉TCP/IP协议,熟悉互联网应用协议 3.有大规模分布式系统设计与开发经验 ...
- No Ads for Blogs
最近浏览器出问题了还是博客园登录的问题. 每次进入自己博客都要输入密码. 然后进入某一篇博文查看时,底部总会有些垃圾广告. 怎么办呢. 好吧,真抱歉,为了营造良好的阅读环境,只好给你屏蔽掉了. 其实也 ...
- [转]50个极好的bootstrap 后台框架主题下载
50个极好的bootstrap 后台框架主题下载 http://sudasuta.com/bootstrap-admin-templates.html 越来越多的设计师和前端工程师开始用bootstr ...
- 半深入理解CSS3 object-position/object-fit属性
半深入理解CSS3 object-position/object-fit属性 转载:https://www.zhangxinxu.com/wordpress/2015/03/css3-object-p ...
- python DBUtils 线程池 连接 Postgresql(多线程公用线程池,DB-API : psycopg2)
一.DBUtils DBUtils 是一套允许线程化 Python 程序可以安全和有效的访问数据库的模块,DBUtils提供两种外部接口: PersistentDB :提供线程专用的数据库连接,并自动 ...
- js 函数中的this
资料 function 函数 没有"this"的持久概念, 调用函数时,创建this function hello(thing) { console.log(this + &quo ...
- react-create-app
github地址 配置文档 环境变量 λ yarn add classnames lodash @material-ui/core react-router-dom mobx mobx-react r ...
- adb 常用命名
adb是Android Debug Bridge的简称, 就是起到调试桥的作用,用来操作android设备 adb help (显示帮助信息) adb devices (获取设备列表及设备状态) ad ...
- 【node】node连接mongodb操作数据库
1.下载第三方模块mongodb cnpm install mongodb --save 2.检测是否连接成功 1.引入第三方模块mongodb并创建一个客户端 const MongoClient = ...