正如前面提到的,struts提供了两种形式的配置。传统的方式是使用对所有配置struts.xml文件。到目前为止,我们已经看到了这样的例子很多。 struts配置的另一种方法是使用java5注释功能。使用struts 注解,我们可以实现零配置。
要开始在你的项目中使用注释,确保webcontent/web-inf/lib文件夹中的jar文件包括以下:
struts2-convention-plugin-x.y.z.jar
asm-x.y.jar
antlr-x.y.z.jar
commons-fileupload-x.y.z.jar
commons-io-x.y.z.jar
commons-lang-x.y.jar
commons-logging-x.y.z.jar
commons-logging-api-x.y.jar
freemarker-x.y.z.jar
javassist-.xy.z.ga
ognl-x.y.z.jar
struts2-core-x.y.z.jar
xwork-core.x.y.z.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.h3.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 应用程序可以使用java5注释作为替代xml和java属性配置。可以检查最重要的注解涉及不同类别的列表: