struts2数据库操作

Struts 2 provide a lot of custom tags for development and we have already looked into Data Tags, Control Tags and UI Tags. Today we will look into two tags that are related to action class response that we can use in the result pages.

Struts 2提供了许多用于开发的自定义标签,我们已经研究了数据标签 , 控制标签和UI标签 。 今天,我们将研究与可在结果页中使用的动作类响应相关的两个标记。

Struts 2操作错误和操作消息 (Struts 2 Action Error and Action Message)

  1. actionerror tag: This tag is used in conjunction with Action class validation for form fields. If validation fails for any form fields, we can add action errors and then Struts 2 API forwards the request to “input” result page where we can use this tag to show the error messages. This tag is helpful in server side validation of form fields and then returning the input page with error message. Syntax of this tag is:

    <s:actionerror/>

    We will look it’s usage with a simple project to explain how easy s:actionerror is to use.

    actionerror标记 :此标记与Action类验证一起用于表单字段。 如果对任何表单字段的验证失败,我们可以添加操作错误,然后Struts 2 API将请求转发到“输入”结果页面,在这里我们可以使用此标记显示错误消息。 此标记有助于服务器端验证表单字段,然后返回带有错误消息的输入页面。 该标签的语法是:

    <s:actionerror/>

    我们将通过一个简单的项目来了解它的用法,以说明s:actionerror的使用方法有多么容易。

  2. actionmessage tag: This tag is used to show some custom message added by Action classes in the result page. For example, we can use this tag to welcome a user and show them last login time at the top of the page. Syntax of this tag is:

    <s:actionmessage/>

    actionmessage标记 :此标记用于显示由结果类中的Action类添加的一些自定义消息。 例如,我们可以使用此标记来欢迎用户,并在页面顶部显示他们的上次登录时间。 该标签的语法是:

    <s:actionmessage/>

Both these tags generated an unordered list of action errors or messages added in the action class. Let’s create a simple project to show their usage. Our final project will look like below image.

这两个标记均生成无序的操作错误列表或在操作类中添加的消息。 让我们创建一个简单的项目来显示其用法。 我们的最终项目将如下图所示。

Struts 2配置文件 (Struts 2 Configuration Files)

web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>Struts2ActionErrorMessages</display-name><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

pom.xml

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>Struts2ActionErrorMessages</groupId><artifactId>Struts2ActionErrorMessages</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.15.1</version></dependency></dependencies><build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>2.3</version><configuration><warSourceDirectory>WebContent</warSourceDirectory><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin></plugins><finalName>${project.artifactId}</finalName></build>
</project>

struts.xml

struts.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""https://struts.apache.org/dtds/struts-2.3.dtd">
<struts><!-- constant to define result path locations to project root directory --><constant name="struts.convention.result.path" value="/"></constant><!-- constant to define global properties file for i18n messages --><constant name="struts.custom.i18n.resources" value="global"></constant><package name="user" namespace="/" extends="struts-default"><action name="login"><result>/login.jsp</result></action><action name="Welcome" class="com.journaldev.struts2.actions.WelcomeAction"><result name="success">/welcome.jsp</result><result name="input">/login.jsp</result></action></package></struts>

Configuration files are self understood and used to configure maven web application project to use Struts2 framework. The important point to note is the struts.custom.i18n.resources where we are providing property file name for global messages. We will use this file for labels in result pages for internationalization.

配置文件是易于理解的,并用于配置Maven Web应用程序项目以使用Struts2框架。 需要注意的重要点是struts.custom.i18n.resources ,我们在其中为全局消息提供属性文件名。 我们将使用此文件作为结果页面中的标签进行国际化。

Another point to notice is the “input” result page for Welcome action, it’s used incase of any form field validation failure.

需要注意的另一点是“欢迎”操作的“输入”结果页面,用于任何表单字段验证失败的情况。

global.properties

global.properties

#global messages
msg.welcome=Hi
label.username=User Name
label.password=Password
label.submit.login=Login#error messages
error.username.required=User Name is required field
error.password.required=Password is required field

A simple property file that will be used in result pages for labels.

一个简单的属性文件,将在标签的结果页中使用。

Struts 2动作类 (Struts 2 Action Class)

WelcomeAction.java

WelcomeAction.java

package com.journaldev.struts2.actions;import com.opensymphony.xwork2.ActionSupport;public class WelcomeAction extends ActionSupport {@Overridepublic String execute() {return SUCCESS;}@Overridepublic void validate() {if("pankaj".equalsIgnoreCase(getUsername()) && "admin".equalsIgnoreCase(getPassword())){addActionMessage("Welcome Admin, do some work.");}else{if(!"pankaj".equalsIgnoreCase(getUsername())){addActionError("User name is not valid");}if(!"admin".equalsIgnoreCase(getPassword())){addActionError("Password is wrong");}}}// java bean propertiesprivate 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;}}

We are extending ActionSupport class and overriding validate method where we are adding action errors and action messages to ValueStack. Other part of action classes include execute() method and java bean properties with getter setter methods.

我们正在扩展ActionSupport类并覆盖validate方法,在其中向ValueStack添加操作错误和操作消息。 动作类的其他部分包括execute()方法和带有getter setter方法的java bean属性。

Struts 2结果页面 (Struts 2 Result Pages)

login.jsp

login.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
<!-- Adding CSS for Styling of error messages -->
<style type="text/css">
.errorDiv{background-color:gray;border:1px solid black;width:400px;margin-bottom:8px;
}
</style>
</head>
<body>
<h3>Struts2 ActionError Example</h3><%-- hasActionErrors() method is defined in ActionSupport --%>
<s:if test="hasActionErrors()"><div class="errorDiv"><s:actionerror/></div>
</s:if><s:form action="Welcome">
<s:textfield name="username" key="label.username"></s:textfield>
<s:password name="password" key="label.password"></s:password>
<s:submit key="label.submit.login" align="center" name="submit"></s:submit>
</s:form>
</body>
</html>

We are using Struts2 if conditional tag to check if there are any action error messages are present or not. hasActionErrors() method is defined in ActionSupport class that returns true if any action errors exists in the ValueStack.

如果条件标签用于检查是否存在任何操作错误消息,我们将使用Struts2。 在ActionSupport类中定义了hasActionErrors()方法,如果ValueStack中存在任何操作错误,则该方法返回true。

Notice that we are using CSS for styling of error messages and using key attributes of UI tags to generate the label from property file configured in struts configuration file. We are not using actionmessage tag here because if validation doesn’t fail, we are not returning to this page.

请注意,我们正在使用CSS设置错误消息的样式,并使用UI标记的关键属性从struts配置文件中配置的属性文件生成标签。 我们此处未使用actionmessage标签,因为如果验证没有失败,我们将不会返回此页面。

welcome.jsp

welcome.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Welcome Page</title>
<style type="text/css">
.welcome {background-color:green;border:1px solid black;width:400px;
}
//ul.actionMessage is added by Struts2 API
ul.actionMessage li {color:yellow;
}
</style></head>
<body>
<h3>Struts2 ActionMessage Example</h3>
<s:if test="hasActionMessages()"><div class="welcome"><s:actionmessage/></div>
</s:if>
<br><br>
<s:property value="getText('msg.welcome')" /> <s:property value="username"/>
</body>
</html>

A simple result page where we are utilizing actionmessage tag. Since this tag generates list with class name as actionMessage, we can use it for styling purpose. hasActionMessages() method is defined in ActionSupport class and returns true if there are any action messages present.

一个简单的结果页面,我们在其中使用actionmessage标签。 由于此标记生成的列表的类名称为actionMessage ,因此我们可以将其用于样式目的。 hasActionMessages()方法在ActionSupport类中定义,如果存在任何操作消息,则返回true。

When we run above application, we get following response pages.

当我们运行上面的应用程序时,我们得到以下响应页面。

Generated HTML snippet of actionerror tag is:

生成的actionerror标记HTML代码段是:

<ul class="errorMessage"><li><span>User name is not valid</span></li><li><span>Password is wrong</span></li>
</ul>

Generated HTML snippet of actionmessage tag is:

生成的actionmessage标签HTML代码段是:

<ul class="actionMessage"><li><span>Welcome Admin, do some work.</span></li>
</ul>

As you can see it’s very easy to use action errors and action messages tags in result pages and since generated html contains class for tags, we can easily add CSS rules for specific styling purposes. Download project from below link and play around with it for better understanding.

如您所见,在结果页面中使用动作错误和动作消息标记非常容易,并且由于生成的html包含标记的类,因此我们可以轻松地为特定样式目的添加CSS规则。 从下面的链接下载项目,并进行尝试以更好地理解。

Download Struts2 Action Errors Messages Example Project下载Struts2操作错误消息示例项目

翻译自: https://www.journaldev.com/2274/struts-2-action-error-action-message

struts2数据库操作

struts2数据库操作_Struts 2操作错误和操作消息相关推荐

  1. 怎么用php操作mysql删除数据库代码_如何使用php操作mysql的增删改查?

    php操作mysql的增删改查方法:1.插入语句[insert into 数据表名(字段1,字段2,....) values("值1","值2",..)]:2. ...

  2. 【DBMS 数据库管理系统】数据仓库 ( 数据仓库简介 | 操作型数据与分析性数据对比 | 数据仓库特征 | 特征一 : 面向主题组织数据 | 面向应用 | )

    文章目录 一.数据仓库简介 二.操作型数据与分析型数据对比 三.数据仓库 特征 与 定义 四.特征一 : 面向主题 数据组织方式 五.面向应用 数据组织方式 六.面向主题 组织数据 七.数据 从 面向 ...

  3. java调用oracle删除,使用IDEA对Oracle数据库进行简单增删改查操作

    1.1 java中的数据存储技术 在java中,数据库存取技术可分为如下几类: 1.jdbc直接访问数据库 2.jdo(java data object)是java对象持久化的新的规范,也是一个用于存 ...

  4. DBController心得之一:利用DMO对象对SQL2005数据库进行Backup和restore的操作

    这个礼拜没有甚么事情,所以使用C#写了一个对数据库进行backup,restore的工具DBController,学到和温习了不少东西: 1.对数据库利用sqlDMO进行宏观的操作. 2.如何利用no ...

  5. sqlite数据库的基本增删改查操作

    2019独角兽企业重金招聘Python工程师标准>>> 效果图示例 1.在清单里添加相应的权限 <uses-permission android:name="andr ...

  6. python数据库教程_Python连接mysql数据库及简单增删改查操作示例代码

    1.安装pymysql 进入cmd,输入 pip install pymysql: 2.数据库建表 在数据库中,建立一个简单的表,如图: 3.简单操作 3.1查询操作 #coding=utf-8 #连 ...

  7. qt mysql 注册码_QT连接Oracle数据库并实现登录验证的操作步骤

    目的: 本文实现QT登录界面,输入账号和密码后,系统连接Oracle数据进行判断账号和密码(MD5加密)是否和数据库一致,如果一致则提示登录成功. 开发环境:Windows10+QT5.14.2+Or ...

  8. [Android] SQLite数据库之增删改查基础操作

        在编程中经常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,能够支持Windows/Linux/Un ...

  9. SQL2K数据库开发三十之存储过程操作删除存储过程

            1.在要删除的存储过程上右击鼠标,在弹出的菜单中选择"删除"命令.         2.在"除去对象"对话框中显示了即将删除的对象,点击&quo ...

最新文章

  1. laravel5.5首次使用php artisan migrate注意问题:
  2. DataTable的Merge\COPY\AcceptChange使用说明
  3. python递归迭代_Python入门基础知识点(python迭代器和递归)
  4. android studio button位置_免费的Android开发环境
  5. 【期望】守卫挑战(金牌导航 期望-9)
  6. jackson - @JsonProperty的使用
  7. 什么是QName【转】
  8. 蓝桥杯2017年第八届C/C++省赛C组第二题-兴趣小组
  9. Linux下Python3.6安装实践与相关问题解决记录
  10. 关于断点续传的那些事
  11. java(jeecg框架) 调用CXF WebService接口的两种方式
  12. Python学习笔记-基础篇
  13. ORA-20011 ORA-29913 KUP-11024问题处理
  14. python 生成带有alpha 通道的视频
  15. 数据分析新人如何面对繁杂且突然的数据需求
  16. gitgerrit配置
  17. Office 365组命名策略 - 补充
  18. 同时使用scanf()函数和getchar()函数无法输入字符串的问题
  19. Python pyecharts地理数据可视化 绘制地理图表
  20. 小明对数位中含有 2、0、1、9 的数字很感兴趣,在 1 到 40 中这样的数包 括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是 574。 请问,在 1 到 2019 中,所

热门文章

  1. jQuery 判断div是否shown
  2. javascript 网页运行代码效果
  3. ajax data参数
  4. VisualSVN https 钩子失效 关闭服务器信任
  5. [转载] python 中NumPy和Pandas工具包中的函数使用笔记(方便自己查找)
  6. [转载] Java8新特性-003-Java8接口中的default修饰符
  7. Cannot set property 'onclick' of null报错
  8. 007 使用SpringMVC开发restful API五--异常处理
  9. UE4删除C++Classes下的类
  10. html中script标签的使用方法