數據庫2003錯誤,struts2數據庫操作_Struts 2操作錯誤和操作消息

 2023-11-19 阅读 23 评论 0

摘要: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. Str

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/>

    數據庫2003錯誤?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/>

mysql連接不上數據庫。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 ,我們在其中為全局消息提供屬性文件名。 我們將使用此文件作為結果頁面中的標簽進行國際化。

sql isnull,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.

java執行sql? 一個簡單的屬性文件,將在標簽的結果頁中使用。

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)

請簡述Struts2開發環境的搭建步驟。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.

java struts? 請注意,我們正在使用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數據庫操作

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/182894.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息