SpringMVC 注解

1、@value  ——  全局常量值

  controller中的注解:

  @value(value="${url}")

  private  String  url;

  ${url}在配置文件中赋值

  如果在applicationContext.xml(是Spring的主配置文件)中定义properties配置文件,则可在service中注解

  如果在xxx-servlet.xml(是Spring的主配置文件)中定义properties配置文件,则可在controller中注解

 

2、@InitBinder  ——  表单日期字符串和JavaBean的Date类型转换

  controller注解:

  @InitBinder

  public void initBinder(WebDataBinder binder) {

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    dateFormat.setLenient(false);

    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

  }

  在需要使用转换日期的controller中使用注解@InitBinder配合Spring自带的WebDataBinder类来操作

  WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set这个值的时候,如果set的这个属性是个对象,Spring就会去找到对应的CustomDateEditor进行转换,然后再set进去

  需要在SpringMVC的配置文件加上

    <!-- 解析器注册 -->  
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
      <property name="messageConverters">  
          <list>  
              <ref bean="stringHttpMessageConverter"/>  
          </list>  
      </property>  
  </bean>  
  <!-- String类型解析器,允许直接返回String类型的消息 -->  
  <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/> 

  换种写法

    <mvc:annotation-driven>
      <mvc:message-converters>
          <bean class="org.springframework.http.converter.StringHttpMessageConverter">
              <constructor-arg value="UTF-8"/>
          </bean>
      </mvc:message-converters>
  </mvc:annotation-driven>

  详解:SpringMVC在绑定表单之前都会注册这些编辑器,SpringMVC提供了一些实现类(如:CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor),上诉方法中调用WebDataBinder的registerCustomEditor方法。

 

3、@SessionAttributes  ——  将Model中的属性同步到session中

  controller注解:

    @SessionAttributes({"str1","str2"})
    public  class    A{
        public  void  index(String str1, String str2){
            model.addAttribute("str1", str1);
            model.addAttribute("str2", str2);
        }
    }

  上述代码中,当执行index方法之后存入model中的值会直接同步到session中(@SessionAttributes指定)供全局使用。

 

4、@ModelAttributes

  controller注解:

/** 
 * 设置这个注解之后可以直接在前端页面使用hb这个对象(List)集合 
 * @return 
 */  
@ModelAttribute("hb")  
public List<String> hobbiesList(){  
    List<String> hobbise = new LinkedList<String>();return hobbise;  
} 

  页面可以直接使用"hb"集合

<c:forEach items="${hb}" var="ho">   
</c:forEach> 

 

5、@PathVariable  ——  获取URL中的动态参数

@PathVariable("a") Integer a

  URL:/b/c/{d}/e/{a}

 

6、@ExceptionHandler  ——  异常捕获

/**
* 参数绑定异常
*/
@ExceptionHandler({BindException.class, ConstraintViolationException.class, ValidationException.class})
public String bindException() {  
   return "error/400";
}
    
/**
* 授权登录异常
*/
@ExceptionHandler({AuthenticationException.class})
public String authenticationException() {  
   return "error/403";
}

  捕获异常,出现对应的异常类时,跳转到指定页面

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

7、@SuppressWarnings注解

  该注解的作用就是让代码段(类或方法)中的警告"保持沉默",比如某个对象未使用,可以使用@SuppressWarnings("unchecked")来去掉警告,对应的关键字如下:

关键字 用途
all to suppress all warnings(抑制所有警告)
boxing  to suppress warnings relative to boxing/unboxing operations(抑制装箱、拆箱操作时候的警告)
cast to suppress warnings relative to cast operations(抑制映射相关的警告)
dep-ann to suppress warnings relative to deprecated annotation(抑制启用注释的警告)
deprecation to suppress warnings relative to deprecation(抑制过期方法警告)
fallthrough  to suppress warnings relative to missing breaks in switch statements(抑制确在switch中缺失breaks的警告)
finally  to suppress warnings relative to finally block that don’t return(抑制finally模块没有返回的警告)
hiding to suppress warnings relative to locals that hide variable
incomplete-switch  to suppress warnings relative to missing entries in a switch statement (enum case)(忽略没有完整的switch语句)
nls  to suppress warnings relative to non-nls string literals(忽略非nls格式的字符)
null to suppress warnings relative to null analysis(忽略对null的操作)
rawtypes to suppress warnings relative to un-specific types when using generics on class params(使用generics时忽略没有指定相应的类型)
restriction to suppress warnings relative to usage of discouraged or forbidden references
serial to suppress warnings relative to missing serialVersionUID field for a serializable class(忽略在serializable类中没有声明serialVersionUID变量)
static-access o suppress warnings relative to incorrect static access(抑制不正确的静态访问方式警告)
synthetic-access   to suppress warnings relative to unoptimized access from inner classes(抑制子类没有按最优方法访问内部类的警告)
unchecked  to suppress warnings relative to unchecked operations(抑制没有进行类型检查操作的警告)
unqualified-field-access to suppress warnings relative to field access unqualified(抑制没有权限访问的域的警告)
unused to suppress warnings relative to unused code(抑制没被使用过的代码的警告)
posted @ 2017-12-21 10:55  想名字头痛  阅读(209)  评论(0编辑  收藏  举报