使用AnnotationFormatterFactory<A extends Annotation>格式化数据

表单数据格式化实践

 代码:

testForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试AnnotationFormatterFactory	接口</title>
</head>
<body>
<h3>测试表单数据格式化</h3>
<form action="test" method="post">
     <table>
         <tr>
         	<td><label>日期类型: </label></td>
             <td><input type="text" id="birthday" name="birthday" ></td>
         </tr>
         <tr>
         	<td><label>整数类型: </label></td>
             <td><input type="text" id="total" name="total" ></td>
         </tr>
         <tr>
         	<td><label>百分数类型: </label></td>
             <td><input type="text" id="discount" name="discount" ></td>
         </tr>
         <tr>
         	<td><label>货币类型: </label></td>
             <td><input type="text" id="money" name="money" ></td>
         </tr>
         <tr>
             <td><input id="submit" type="submit" value="提交"></td>
         </tr>
     </table>
</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试AnnotationFormatterFactory</title>
</head>
<body>
<h3>测试表单数据格式化</h3>
<form:form modelAttribute="user" method="post" action="" >
<table>
	<tr>
		<td>日期类型:</td>
		<td><form:input path="birthday"/></td>
	</tr>
	<tr>
		<td>整数类型:</td>
		<td><form:input path="total"/></td>
	</tr>
	<tr>
		<td>百分数类型:</td>
		<td><form:input path="discount"/></td>
	</tr>
	<tr>
		<td>货币类型:</td>
		<td><form:input path="money"/></td>
	</tr>
</table>
</form:form>
</body>
</html>

User.java

package com.bean;

import java.io.Serializable;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;

public class User implements Serializable {
	// 日期类型
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birthday;
	// 正常数字类型
	@NumberFormat(style=Style.NUMBER, pattern="#,###")  
	private int total;  
    // 百分数类型
	@NumberFormat(style=Style.PERCENT)  
    private double discount;  
	// 货币类型
	@NumberFormat(style=Style.CURRENCY)  
	private double money;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public double getDiscount() {
		return discount;
	}
	public void setDiscount(double discount) {
		this.discount = discount;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "User [birthday=" + birthday + ", total=" + total + ", discount=" + discount + ", money=" + money + "]";
	}
	
}

FormatterController.java

package com.control;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.bean.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class FormatterController{
	private static final Log logger = LogFactory.getLog(FormatterController.class);
	 
	@RequestMapping(value="/{formName}")
	 public String loginForm(@PathVariable String formName){
		// 动态跳转页面
		return formName;
	}
	
	 @RequestMapping(value="/test",method=RequestMethod.POST)
	 public String test(@ModelAttribute User user, Model model) {
		 logger.info(user);
		 model.addAttribute("user", user);
	     return "success";
	 }
}

截图:




<think>我们正在解释Java中的`isAnnotationPresent`方法,其声明为: public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 我们将逐步分解这个方法的每个部分: 1. **访问修饰符 `public`**: - 表示该方法可以在任何其他类中被访问。 2. **返回类型 `boolean`**: - 该方法返回一个布尔值(`true`或`false`),指示当前元素(类、方法、字段等)是否被指定的注解所标注。 3. **方法名 `isAnnotationPresent`**: - 方法名直观地表达了其功能:检查注解是否存在(即是否出现在当前元素上)。 4. **参数类型 `Class<? extends Annotation> annotationClass`**: - 这是一个泛型参数,表示传入的必须是一个代表注解类型的`Class`对象。 - `Class<? extends Annotation>`:这里使用了有界通配符,表示`Class`对象代表的类型必须是`Annotation`类型或其子类型(即任何注解类型)。 - 参数`annotationClass`:这是传入的注解类的`Class`对象,例如,如果我们想检查`@Override`注解,就传入`Override.class`。 **方法的作用**: - 该方法用于检查某个元素(如类、方法、字段等)是否被指定的注解类型所标注。如果该元素被指定的注解标注,则返回`true`;否则返回`false`。 **使用场景**: - 在反射中,我们经常需要检查某个类、方法或字段上是否有特定的注解,以便根据注解的存在执行不同的逻辑。例如,在框架开发中,通过注解来配置行为,然后通过反射检查注解并做出相应的处理。 **示例**: 假设我们有一个自定义注解`@MyAnnotation`,并且有一个类`MyClass`,我们想检查`MyClass`上是否有`@MyAnnotation`注解。 ```java // 定义注解 @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { } // 使用注解 @MyAnnotation public class MyClass { } // 检查注解 public class AnnotationCheck { public static void main(String[] args) { Class<MyClass> clazz = MyClass.class; // 检查MyClass类上是否有MyAnnotation注解 boolean isPresent = clazz.isAnnotationPresent(MyAnnotation.class); System.out.println(isPresent); // 输出:true } } ``` **注意**: - 要使`isAnnotationPresent`方法能够检测到注解,该注解的保留策略(`Retention`)必须设置为`RetentionPolicy.RUNTIME`,因为反射是在运行时进行的。如果注解的保留策略是`SOURCE`或`CLASS`,则在运行时无法获取。 **引用说明**: 在Java反射中,`Class`类提供了许多方法来处理注解,其中`isAnnotationPresent`是常用方法之一[^1][^2][^3][^4]。 §§相关问题§§ 1. Java中注解的保留策略(RetentionPolicy)有哪几种?它们分别有什么作用? 2. 除了`isAnnotationPresent`方法,还有哪些常用的反射方法用于处理注解? 3. 如何通过反射获取注解实例并读取其属性值? 4. 自定义注解时,`@Target`注解的作用是什么?</think>### Java中`isAnnotationPresent`方法详解 `public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)` 是Java反射API中的关键方法,用于检查某个程序元素(类、方法、字段等)是否被特定注解标注。以下是逐部分解析: 1. **`public`** - 访问修饰符:表示该方法可在任何类中被访问 2. **`boolean`** - 返回类型:方法返回布尔值 - `true`:目标元素存在指定注解 - `false`:目标元素不存在指定注解 3. **`isAnnotationPresent`** - 方法名:直译为"注解是否存在",明确表达功能意图 4. **`Class<? extends Annotation> annotationClass`** - 参数类型:使用泛型限定必须传入`Annotation`子类的Class对象 - `? extends Annotation`:通配符表示接受任何注解类型 - 示例:检查`@Override`注解需传入`Override.class` **方法作用**: 在运行时检测目标元素(如类、方法或字段)是否被特定注解标记。这是实现注解驱动编程的核心机制,广泛应用于框架开发(如Spring的`@Autowired`、JUnit的`@Test`)。 **使用示例**: ```java // 检查方法是否被@Test注解标记 Method testMethod = MyTestClass.class.getMethod("testCase"); if (testMethod.isAnnotationPresent(Test.class)) { System.out.println("这是测试方法"); } ``` **关键特性**: - 依赖`@Retention(RetentionPolicy.RUNTIME)`:只有保留策略为`RUNTIME`的注解才能在运行时被检测到[^1][^4] - 反射操作基础:通过`Class`对象获取方法/字段后再调用此方法[^2][^3] - 类型安全:泛型参数确保只能传入注解类型Class对象 **典型应用场景**: 1. 单元测试框架识别测试方法 2. DI容器识别需要依赖注入的字段 3. ORM框架识别实体类映射关系 4. Web框架识别控制器路由
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇潇雨歇_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值