詳解Java的Struts框架中正文的用法。本站提示廣大學習愛好者:(詳解Java的Struts框架中正文的用法)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解Java的Struts框架中正文的用法正文
要開端在你的項目中應用正文,確保WebContent/WEB-INF/lib文件夾中的jar文件包含以下:
如今,讓我們看看你若何能做到設置裝備擺設在struts.xml文件,取而代之的是注解。
Struts2正文的概念的說明,我們須要從新斟酌我們的驗證為例解釋在 Struts2的驗證 一章中。
在這裡,我們將采用一個例子,雇員Employee 將被捕捉的姓名和年紀應用一個簡略的頁面,我們將會把兩個驗證,以確保應用老是進入一個名字和年紀應當是在28和65之間。所以,讓我們先從主JSP頁面的例子。
創立主頁:
讓我們寫主JSP頁面文件index.jsp,這將被用來搜集上述員工的相干信息。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Employee Form</title> </head> <body> <s:form action="empinfo" method="post"> <s:textfield name="name" label="Name" size="20" /> <s:textfield name="age" label="Age" size="20" /> <s:submit name="submit" label="Submit" align="center" /> </s:form> </body> </html>
在index.jsp應用Struts的標簽,我們還沒有籠罩,但我們將研討這些標簽相干的章節。但如今,假定s:textfield 標簽打印一個輸出字段 s:submit 打印一個提交按鈕。我們曾經應用label屬性標簽,每一個標簽每一個標簽創立。
創立視圖:
我們將應用JSP文件的success.jsp將挪用的情形下界說的舉措前往SUCCESS。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Success</title> </head> <body> Employee Information is captured successfully. </body> </html>
創立舉措:
這是將用於正文的處所。讓我們從新界說行為Employee類的正文,然後添加一個辦法稱為validate() ,以下所示在Employee.java文件。請確保操作類擴大ActionSupport類,不然validate辦法將不會被履行。
package com.yiibai.struts2;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.validator.annotations.*;
@Results({
@Result(name="success", location="/success.jsp"),
@Result(name="input", location="/index.jsp")
})
public class Employee extends ActionSupport{
private String name;
private int age;
@Action(value="/empinfo")
public String execute()
{
return SUCCESS;
}
@RequiredFieldValidator( message = "The name is required" )
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@IntRangeFieldValidator(message = "Age must be in between 28 and 65",
min = "29", max = "65")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
在這個例子中,我們曾經應用了一些注解。讓我逐一解釋:
起首,我們曾經Result注解。成果注解的成果是一個聚集。成果注解下,我們有兩個成果正文。成果正文的稱號對應的履行辦法的成果。它們還含有一個視圖應擔負響應的execute() 前往值的地位。
下一個注解是行為注解。這是用來潤飾 execute()辦法。操作辦法也須要一個值,該URL上挪用操作。
最初,應用兩個驗證的注解。曾經設置裝備擺設了所需的字段驗證的年紀字段"name“字段和整數規模驗證。也指定了自界說驗證新聞。
設置裝備擺設文件:
我們不須要struts.xml 設置裝備擺設文件,讓我們刪除該文件,並讓我們檢討web.xml文件中的內容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>struts.devMode</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
如今,右鍵點擊項目稱號,並單擊 Export > WAR File創立一個WAR文件。然後安排此WAR在Tomcat的webapps目次下。最初,啟動Tomcat辦事器和測驗考試拜訪URL http://localhost:8080/HelloWorldStruts2/index.jsp。這會給出以下畫面:
如今不輸出任何所需信息,只需點擊“Submit ”按鈕。將看到以下成果:
輸出所需的信息,但輸出了毛病的“From ”字段,讓我們說“test”和年紀為30名,最初點擊“Submit ”按鈕。將看到以下成果:
Struts 2的正文類型
Struts 2 運用法式可使用Java5正文作為替換XML和Java屬性設置裝備擺設。可以檢討最主要的注解觸及分歧種別的列表:
Struts 2 運用法式可使用Java5正文作為替換XML和Java屬性設置裝備擺設。這裡是清單的分歧的種別有關的最主要的注解:
定名空間正文(舉措正文):
@ Namespace正文許可在Action類中,而不是基於零設置裝備擺設的商定舉措的定名空間的界說。
@Namespace("/content")
public class Employee extends ActionSupport{
...
}
成果正文 - (舉措譯注):
@ Result注解許可在Action類中,而不是一個XML文件中界說的舉措成果。
@Result(name="success", value="/success.jsp")
public class Employee extends ActionSupport{
...
}
成果正文 - (舉措譯注):
@ Results注解界說了一套舉措的成果。
@Results({
@Result(name="success", value="/success.jsp"),
@Result(name="error", value="/error.jsp")
})
public class Employee extends ActionSupport{
...
}
正文後(攔阻正文):
@After注解標記著一個須要挪用後的重要操作辦法和履行成果的操作辦法。前往值將被疏忽。
public class Employee extends ActionSupport{
@After
public void isValid() throws ValidationException {
// validate model object, throw exception if failed
}
public String execute() {
// perform secure action
return SUCCESS;
}
}
正文之前(攔阻正文):
@ Before正文標志須要一個操作辦法的重要操作辦法之前被挪用履行成果。前往值將被疏忽。
public class Employee extends ActionSupport{
@Before
public void isAuthorized() throws AuthenticationException {
// authorize request, throw exception if failed
}
public String execute() {
// perform secure action
return SUCCESS;
}
}
BeforeResult正文 - (攔阻正文):
@ BeforeResult注解標記著一個成果之前須要履行的操作辦法。前往值將被疏忽。
public class Employee extends ActionSupport{
@BeforeResult
public void isValid() throws ValidationException {
// validate model object, throw exception if failed
}
public String execute() {
// perform action
return SUCCESS;
}
}
ConversionErrorFieldValidator正文 - (驗證譯注):
此驗證注解假如有任何轉換毛病停止了實地檢討,並實用於他們,假如他們存在。
public class Employee extends ActionSupport{
@ConversionErrorFieldValidator(message = "Default message",
key = "i18n.key", shortCircuit = true)
public String getName() {
return name;
}
}
DateRangeFieldValidator正文 - (驗證譯注):
這驗證注解檢討日期字段的值在指定規模內。
public class Employee extends ActionSupport{
@DateRangeFieldValidator(message = "Default message",
key = "i18n.key", shortCircuit = true,
min = "2005/01/01", max = "2005/12/31")
public String getDOB() {
return dob;
}
}
DoubleRangeFieldValidator正文 - (驗證譯注):
此驗證注解檢討雙字段有一個值,該值在指定規模內。假如既不最小或最年夜,甚麼都不會做的。
public class Employee extends ActionSupport{
@DoubleRangeFieldValidator(message = "Default message",
key = "i18n.key", shortCircuit = true,
minInclusive = "0.123", maxInclusive = "99.987")
public String getIncome() {
return income;
}
}
EmailValidator正文 - (驗證譯注):
這驗證注解檢討一個字段是一個有用的E-mail地址,假如它包括一個非空的字符串。
public class Employee extends ActionSupport{
@EmailValidator(message = "Default message",
key = "i18n.key", shortCircuit = true)
public String getEmail() {
return email;
}
}
ExpressionValidator正文 - (驗證譯注):
這類非字段級驗證驗證所供給的正則表達式。
@ExpressionValidator(message = "Default message", key = "i18n.key",
shortCircuit = true, expression = "an OGNL expression" )
IntRangeFieldValidator正文 - (驗證譯注):
這驗證注解檢討一個數字字段的值在指定的規模內。假如既不最小或最年夜,甚麼都不會做的。
public class Employee extends ActionSupport{
@IntRangeFieldValidator(message = "Default message",
key = "i18n.key", shortCircuit = true,
min = "0", max = "42")
public String getAge() {
return age;
}
}
RegexFieldValidator 正文 - (驗證譯注):
這個注解驗證一個字符串字段,應用正則表達式。
@RegexFieldValidator( key = "regex.field", expression = "yourregexp")
RequiredFieldValidator 正文 - (驗證譯注):
這驗證注解檢討一個字段不為空。標注必需被運用在辦法層面。
public class Employee extends ActionSupport{
@RequiredFieldValidator(message = "Default message",
key = "i18n.key", shortCircuit = true)
public String getAge() {
return age;
}
}
RequiredStringValidator正文 - (驗證譯注):
這驗證注解檢討一個字符串字段不為空(即非空,長度> 0)。
public class Employee extends ActionSupport{
@RequiredStringValidator(message = "Default message",
key = "i18n.key", shortCircuit = true, trim = true)
public String getName() {
return name;
}
}
StringLengthFieldValidator正文 - (驗證譯注):
這個驗證檢討字符串字段是適合的長度。假定該字段是一個字符串。假如設置既不是minLength 也不是最年夜長度,甚麼都不會做。
public class Employee extends ActionSupport{
@StringLengthFieldValidator(message = "Default message",
key = "i18n.key", shortCircuit = true,
trim = true, minLength = "5", maxLength = "12")
public String getName() {
return name;
}
}
UrlValidator正文 - (驗證譯注):
這個驗證檢討一個字段是一個有用的URL。
public class Employee extends ActionSupport{
@UrlValidator(message = "Default message",
key = "i18n.key", shortCircuit = true)
public String getURL() {
return url;
}
}
驗證正文 - (驗證譯注):
假如想應用多個雷同類型的正文,這些正文必需嵌套在@Validations() 正文。
public class Employee extends ActionSupport{
@Validations(
requiredFields =
{@RequiredFieldValidator(type = ValidatorType.SIMPLE,
fieldName = "customfield",
message = "You must enter a value for field.")},
requiredStrings =
{@RequiredStringValidator(type = ValidatorType.SIMPLE,
fieldName = "stringisrequired",
message = "You must enter a value for string.")}
)
public String getName() {
return name;
}
}
CustomValidator正文 - (驗證譯注):
這個注解可以用於自界說驗證。應用ValidationParameter的正文,以供給額定的 params.
@CustomValidator(type ="customValidatorName", fieldName = "myField")
轉換正文 - (類型轉換正文):
這是一個標志正文類型轉換類型級別。轉換正文必需運用在類型級別。
@Conversion()
public class ConversionAction implements Action {
}
CreateIfNull正文 - (類型轉換正文):
這個注解設置類型轉換CreateIfNull。必需運用在域或辦法級CreateIfNull注解。
@CreateIfNull( value = true ) private List<User> users;
元素正文 - (類型轉換正文):
這個注解設置元素停止類型轉換。必需運用在字段域或辦法級元素的注解。
@Element( value = com.acme.User ) private List<User> userList;
症結正文 - (類型轉換正文):
這個注解設置停止類型轉換的症結。必需運用在域或辦法級的症結注解。
@Key( value = java.lang.Long.class ) private Map<Long, User> userMap;
KeyProperty正文 - (類型轉換正文):
這個注解設置類型轉換KeyProperty。必需運用在域或辦法級KeyProperty注解。
@KeyProperty( value = "userName" ) protected List<User> users = null;
TypeConversion正文 - (類型轉換正文):
這個注解的注解是用於類和運用法式的轉換規矩。注解可以運用於TypeConversion在屬性和辦法的級別。
@TypeConversion(rule = ConversionRule.COLLECTION,
converter = "java.util.String")
public void setUsers( List users ) {
this.users = users;
}