原创内容,转载请注明来自http://boweihe.me/?p=2026
近期在努力学习SpringMVC,因为之前对JSP也是一知半解的,干脆拿了本《Spring in Action》(4th edition)啃,发现使用注释的方式比用xml来的有意思一些。网站中要用到验证码,目前能找到的文档都是用xml配置的,感觉有点不爽,决定学我党“摸着石头过河”一次,希望不要naive了..
Maven库
Google自家的库估计是没了,大概是时间太久远了吧。我用的Maven库是这个
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
Bean配置文件
需要把原有的applicationContext.xml用注释的方式实现,其实就是让Spring找到个可以用的Bean并加载相关配置。
我构造了一个类来搞定这个事情,里面的配置参数会从application.properties文件中读取,我仅仅实现了我需要的几个参数,反正如果要加的话就在kaptchaProperties方法里面写就可以,然后Autowire一个Environment用来读取文件配置参数。主要是@Configuration 注释,这个是告诉Spring我是个配置类,这个还是从Hibernate配置中学过来的,哈哈。
声明出来的这个@Bean(name = “captchaProducer”) 就是后面Controller里面要用的了
package edu.inlab.config; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import java.util.Properties; /** * Created by inlab-dell on 2016/5/17. */ @Configuration @PropertySource(value = {"classpath:application.properties"}) public class KaptchaConfig { @Autowired private Environment environment; private DefaultKaptcha kaptcha; @Bean(name = "captchaProducer") public DefaultKaptcha getKaptchaProducer(){ if(null == kaptcha) { kaptcha = new DefaultKaptcha(); kaptcha.setConfig(getKaptchaConfig()); } return kaptcha; } @Bean public Config getKaptchaConfig(){ return new Config(kaptchaProperties()); } private Properties kaptchaProperties(){ Properties properties = new Properties(); properties.put("kaptcha.image.width", environment.getRequiredProperty("kaptcha.image.width")); properties.put("kaptcha.image.height", environment.getRequiredProperty("kaptcha.image.height")); properties.put("kaptcha.textproducer.char.string", environment.getRequiredProperty("kaptcha.textproducer.char.string")); properties.put("kaptcha.textproducer.char.length", environment.getRequiredProperty("kaptcha.textproducer.char.length")); return properties; } }
对应的application.properties,具体含义可以看参考文献里,解释的很清楚了。
kaptcha.image.width = 200 kaptcha.image.height = 50 kaptcha.textproducer.char.string = ABCDEFGHKLMNPQRSTWXY3456789 kaptcha.textproducer.char.length = 6
CaptchaController 控制器
项目在实习前怕是赶不完来了,我还是少花点时间写博客吧。
控制器的实现Wiki上都有,主要的是要@Autowire之前我们做好的那个Bean,大概是这样。其实就是个基于set方法的注入嘛~
@Controller @RequestMapping("/captcha") public class CaptchaController { private Producer captchaProducer; @Autowired public void setCaptchaProducer(Producer captchaProducer) { this.captchaProducer = captchaProducer; } @RequestMapping(method = RequestMethod.GET) public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception{ // ... } }
参考文献:
- 在springmvc项目中使用kaptcha生成验证码
- 简单Maven的Web项目之验证码(Kaptcha篇)
- Spring mvc框架下使用kaptcha生成验证码
- https://code.google.com/archive/p/kaptcha/wikis