Struts 2 Annotations
来源:互联网 发布:javascript实现倒计时 编辑:程序博客网 时间:2024/05/16 04:55
Struts2 提供两种形式的配置,一种是传统的struts.xml文件配置方式,另外一种就是Annotations 方式,使用Annotations 方式就不需要在写struts.xml文件,相当于零配置。至于如何使用Annotations 方式,下面我以例子形式讲述:
首先导入下面的Jar文件到你的项目中WebRoot/WEB-INF/lib目录
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
创建主页 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>创建视图 success.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>Success</title></head><body> Employee Information is captured successfully.</body></html>创建Action Employee.java
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; }}配置文件 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.ng.filter.StrutsPrepareAndExecuteFilter </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>
- Struts 2 Annotations Types
- Struts 2 Annotations
- Struts 2 Annotations
- struts-annotations-plugin 介绍
- struts2的主题(Struts 2 Themes)和注解(struts 2 annotations)
- Annotations in Tiger, Part 2: Custom annotations
- Annotations
- Annotations
- Android Annotations框架第2篇-实例代码1
- Android studio2.2以上版本配置Android annotations
- hibernate annotations
- 什么是annotations
- hibernate annotations
- Creating Annotations
- hibernate annotations
- Spring Annotations
- Hibernate Annotations
- Java Annotations
- 使用Oracle建库的一般流程
- C++07:使用OTL操作MySQL数据库
- html5 笔记7
- 在DIV中图片垂直、水平居中(最简单方法)
- Linq使用Group By分析 Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include
- Struts 2 Annotations
- Webdriver_RubyGems 镜像 - 淘宝网
- 零点起飞学SQL Server
- 让div居中对齐的css
- A sentence every day
- Socket between Java and Perl
- Android网络编程之Http
- c++
- tftp客户端设计小结