springMVC拦截器中如何获得被拦截方法的返回值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springMVC拦截器中如何获得被拦截方法的返回值相关的知识,希望对你有一定的参考价值。
参考技术A 去阿帕奇下载apache.org/poi项目的源码:
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.xssf.usermodel.XSSFCell
import org.apache.poi.xssf.usermodel.XSSFRow
import org.apache.poi.xssf.usermodel.XSSFSheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook
/**
* Created by WangQing on 15/11/20.
*/
class TestGroovy
void updateResourceDate()
def filePath = "./assets/people.xlsx"
File file = new File(filePath)
FileInputStream is = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(is);
workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
//循环sheet
(0..<workbook.sheetIterator().collect return it.@size).each s->
XSSFSheet sheet = workbook.getSheetAt(s);
int rows = sheet.physicalNumberOfRows;
//忽略第一行,标题行
(1..<rows).eachr->
XSSFRow row = sheet.getRow(r);
def cells = row.physicalNumberOfCells;
(0..<cells).eachc->
XSSFCell cell = row.getCell(c);
def name = "";
switch (c)
case 0:
name = "A:"
break;
case 1:
name = "B:"
break
case 2:
name = "C:"
break
case 3:
name = "D:"
break
print name + " "+cell+ ", ";
println "";
static main(args)
TestGroovy a = new TestGroovy();
a.updateResourceDate();
SpringMVC的拦截器
自定义拦截器很简单,只有如下三步:
1. 创建拦截器类实现HandlerInterceptor接口
2. 配置拦截器
3. 测试拦截器的拦截效果
当拦截器的preHandle方法返回true则会执行目标资源,如果返回false则不执行目标资源
多个拦截器情况下,配置在前的先执行,配置在后的后执行
拦截器中的方法执行顺序是:preHandler-------目标资源----postHandle---- afterCompletion
拦截器
public class MyInterceptor1 implements HandlerInterceptor {
//在目标方法执行之前 执行
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
System.out.println("preHandle.....");
}
//在目标方法执行之后 视图对象返回之前执行
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
System.out.println("postHandle...");
}
//在流程都执行完毕后 执行
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
System.out.println("afterCompletion....");
}
}
在SpringMVC的配置文件中配置
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--对哪些资源执行拦截操作-->
<mvc:mapping path="/**"/>
<bean class="com.code_g.interceptor.MyInterceptor1"/>
</mvc:interceptor>
<!--这里可以配置多个拦截器,形成拦截器链,拦截顺序为配置顺序-->
</mvc:interceptors>
测试类
@Controller
public class TargetController {
@RequestMapping("/target")
public ModelAndView show(){
System.out.println("目标资源执行......");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
}
以上是关于springMVC拦截器中如何获得被拦截方法的返回值的主要内容,如果未能解决你的问题,请参考以下文章