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標簽 。 今天,我們將研究與可在結果頁中使用的動作類響應相關的兩個標記。
<s:actionerror/>
數據庫2003錯誤?We will look it’s usage with a simple project to explain how easy s:actionerror
is to use.
<s:actionerror/>
我們將通過一個簡單的項目來了解它的用法,以說明s:actionerror
的使用方法有多么容易。
<s:actionmessage/>
<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.
這兩個標記均生成無序的操作錯誤列表或在操作類中添加的消息。 讓我們創建一個簡單的項目來顯示其用法。 我們的最終項目將如下圖所示。
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? 一個簡單的屬性文件,將在標簽的結果頁中使用。
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屬性。
請簡述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規則。 從下面的鏈接下載項目,并進行嘗試以更好地理解。
翻譯自: https://www.journaldev.com/2274/struts-2-action-error-action-message
struts2數據庫操作
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态