springmvc

spring mvc 相关

spring mvc json使用

导入 参照一下连接注意,Jackson 需要用all的jar包,并且版本要在1.9.1以上

http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/arrow-up-right

@RequestMapping(value = "test/user/json", method = RequestMethod.GET)
public @ResponseBody User getJson() {
    return new User("name", "password", "login");
}
  1. Jackson library is existed in the project classpath

    2.The mvc:annotation-driven is enabled

  2. Return method annotated with @ResponseBody

spring mvc + freemarker中表单组件

定义一个form.ftl宏,restful风格的uri中需要添加_method这个hidden input,内容分别为put(修改),delete(删除),post(新加) 如果用@s.formXXX Controller中必须有绑定的对象,在这个例子里必须有User 对象,可以new,可以传参。

<#macro userform action method="post" button_content="注册新用户" >
<form action=${action} method="post">
<input type="hidden" name="_method" value="${method}">
<#if method!='delete'>
    <#import "/spring.ftl" as s />   
    <@s.formHiddenInput "user.uuser_id"/>
    <@s.showErrors "<div>" />
    登录名:
    <@s.formInput "user.login_name" "class='loginText'"/>
    <@s.showErrors "<div>" />
     用户名:
    <@s.formInput "user.user_name" />
    <@s.showErrors "<div>" />
    密码:
    <@s.formPasswordInput "user.password"/>
    <@s.showErrors "<div>" />
  <button type="submit">${button_content}</button>
<#else>
    <#nested>
</#if>
</form>
 </#macro> 

对应文件引用即可

</html>

spring mvc + junit单元测试

测试中导入spring中的配置文件,并且使用注解还是比较简单的,类上面加上两个注解即可,测试类内部就可以使用spring的IoC了。

spring 测试controller,测试页面再也不用启动tomcat了以及写一堆没用的页面了~

spring测试controller的时候使用的是Mock这个测试框架 ,Mock的request和response分别使用:MockHttpServletRequest request; MockHttpServletResponse response。注意测试的时候需要模拟真实的HTTP请求,即每次的request和response对象都是一个新new的。

直接上例子:

spring mvc ehcache缓存

spring mvc使用ehcache

需要用到的jar包

spring配置文件中需要添加如下内容

cache一般用在和数据库交互的地方service

非常详细的spring mvc和cache的使用博客

http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/arrow-up-right

官网文档

http://docs.spring.io/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.htmlarrow-up-right

ehcache介绍

http://my.oschina.net/coolfire368/blog/123377arrow-up-right

spring mvc整合 ehcache

http://blog.csdn.net/jadyer/article/details/12257865arrow-up-right

简单讲解

cache主要注解使用:@Cacheable,@CacheEvict,@CachePut

缓存是这样的,取值时在方法(A)调用前查一下缓存中是否有目标值,缓存存在的话直接从缓存中拿出不再去执行方法(A),这也是最基本的@Cacheable的概念;

缓存中有值需要更新怎么办?使用@CacheEvict来更新,这个注解的意思是删除掉缓存里面的某个值,从而达到更新缓存的效果。关于缓存更新,例如,取topN个对象,第一次取的时候比如是前1~10个,缓存中存这1~10的一个集合对象,第二次取的时候直接从缓存中拿,这没问题,现在是这样的,假设数据库中删除了1~10个元素中的任意一个值,这样数据库中的topN与缓存中的topN就不同步了,下次你在前台取topN的时候,因为缓存里面有这个对象,根据之前的介绍(取值时在方法(A)调用前查一下缓存中是否有目标值,缓存存在的话直接从缓存中拿出不再去执行方法(A)),方法A被略过,查的值不是真正的topN了,因此需要在add或者delete之后删除掉原来的缓存,保持数据一致。其他情景请自行考虑。

根据缓存的特性,如何做到既要保证方法被调用,又希望结果被缓存呢?直接使用@CachePut,他与@Cacheable的区别就在与方法是会被执行的。

注解里面属性解释,@Cacheable 与@CachePut一样, @CacheEvict还有和删除有关的两个属性:

  1. value:缓存的名称,在spring配置文件中定义,必须指定至少一个

    • 例如:@Cacheable(value=”mycache”) 或者

      @Cacheable(value={”cache1”,”cache2”}

  2. key:缓存的key,(缓存是键值对儿)可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

    • 例如:

      @Cacheable(value=”testcache”,key=”#userName”)

    • 使用字符串"'sss'"

    • 调用对象getName方法key=”#userName.getName()”

  3. condition:缓存的条件,可以为空,使用SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

    • 例如:

      @Cacheable(value=”testcache”,condition=”#userName.length()>2”)

  4. allEntries:是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存

    • 例如:

      @CachEvict(value=”testcache”,allEntries=true)

  5. beforeInvocation:是否在方法执行前就清空,缺省为 false,如果指定为 true,则在*方法还没有执行的时候就清空缓存,缺省情况下为false,这样如果方法执行抛出异常,则不会清空缓存

    • 例如:

      @CachEvict(value=”testcache”,beforeInvocation=true)

Last updated