在spring MVC的controller中获取ServletConfig
来源:互联网 发布:k均值聚类算法opencv 编辑:程序博客网 时间:2024/05/20 18:00
在使用SmartUpload进行文件上传时,需要用到srevletConfig:
如果是在servlet中写当然是很容易实现的:
private ServletConfig config;//初始化Servletfinal public void init(ServletConfig config)throws ServletException{this.config=config;}
init方法会在servlet初始化时获取到servletConfig.
但是在Controller中怎么获得呢?经过小编多方请教,可以通过继承ServletConfigAware,ServletContextAware这两个接口来实现:
package module.system.controller;import java.io.IOException;import javax.servlet.ServletConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import module.system.common.FileLoad;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.context.ServletConfigAware;import org.springframework.web.context.ServletContextAware;/** * 文件上传下载. * */@Controller@RequestMapping("/fileLoad")public class FileLoadController implements ServletConfigAware,ServletContextAware{private ServletContext servletContext;@Overridepublic void setServletContext(ServletContext arg0) {this.servletContext = arg0;} private ServletConfig servletConfig;@Overridepublic void setServletConfig(ServletConfig arg0) { this.servletConfig = arg0;}@RequestMapping(value = "/upload.do", method = RequestMethod.POST)@ResponseBody //此注解表明返回值跳过视图处理部分,直接写入 http response body中public String upload(HttpServletRequest request,HttpServletResponse response) { FileLoad fileLoad = new FileLoad();try {fileLoad.upload(request, response,servletConfig);} catch (ServletException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return "";}}
里边这个upload方法是另外写的一个方法,将会在下一篇文章中介绍。通过这样就可以得到servletConfig了
3 0
- 在spring MVC的controller中获取ServletConfig
- spring mvc,在controller中获取属性文件的值
- spring mvc controller中获取属性文件(properties)的值
- spring mvc controller中获取属性文件(properties)的值
- 在Spring MVC controller中配切点
- spring mvc中常用的几个controller
- spring mvc中常见的几个controller
- Spring MVC中基于注解的Controller
- spring mvc的Controller
- Spring mvc 的controller
- spring mvc Controller中使用@Value无法获取属性值
- spring mvc Controller中使用@Value无法获取属性值
- 从Spring MVC中获取Controller中的请求信息
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 剧场版复活的f 剧情
- 安卓服务——bind service一个绑定式的服务
- 6、linux ad7879触摸屏驱动
- android压力测试命令monkey详解
- monkeyrunner 环境配置
- 在spring MVC的controller中获取ServletConfig
- unity 奇葩错误报错 nternal compiler error. See the console log for... 然后所有的代码都报 The associated script can
- Oracle行转列、列转行的Sql语句总结
- 解决新GRE写作分数低的几条建议
- linux命令7--cut命令
- RTT学习笔记之内存池管理(静态内存池)
- [每天读书半小时] 2015-6-12 Servlet / JSP
- 不用加减乘除做加法
- 【java基础】——java面向对象(上)—面向对象、类与对象、封装、构造函数等