
config包中的代码
package demo.config;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class SpringMVCConfig extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {//匿名对象
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("***** preHandle*****");
String name = request.getParameter("name");
if("admin".equals(name)){
return true;//放行
}else{
return false;//不放行
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("***** postHandle*****");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("***** afterCompletion*****");
}
};
registry.addInterceptor(handlerInterceptor);//拦截器添加进来
}
}
controller中的代码-TestController
package demo.controller;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
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 demo.entity.User;
@Controller
//各类配置一个路径
//@RequestMapping("test")
//那么该如何调用该类下的方法呢?------------http://127.0.0.1:8080/test/t1
public class TestController {
@RequestMapping(value = "hello" , method = RequestMethod.GET)
@ResponseBody
public String helloWorld(){
System.out.println("进入到controller里来了!");
return "hello world1";
}
//解析json,搭配User实体,响应json
@RequestMapping("t1")
@ResponseBody
public User test1(){
return new User("tom", 18);
}
//请求传参----简单参数(不仅有简单参数,还有复杂参数)
//测试路径:http://127.0.0.1:8080/t2?name=tom1&age=19
@RequestMapping("t2")
@ResponseBody
public String test2(String name,int age){
return "Request参数是:name="+name+",age="+age;
}
//请求传参----数组(复杂参数)
//测试路径:http://127.0.0.1:8080/t3?hobby=eat&hobby=drink
@RequestMapping("t3")
@ResponseBody
public String test3(String[] hobby){
return "Request参数是:"+Arrays.toString(hobby);
}
//实体传参
//测试路径:http://127.0.0.1:8080/t2?name=tom1&age=20
@RequestMapping("t4")
@ResponseBody
public User test4(User user){
return user;
}
@RequestMapping("t5")
public String test5(HttpServletRequest request){
request.setAttribute("str","你好!测试请求转发。");
return "forward:result";//相对路径 (绝对路径:forward:/test/result)
}
@RequestMapping("t6")
public String test6(HttpServletRequest request){
request.setAttribute("str","你好!测试重定向。");
return "redirect:result";
}
@RequestMapping("result")
@ResponseBody
public String result(HttpServletRequest request){
String reslut = request.getAttribute("str")+"";
return "Request域中的str的值:"+reslut;
}
}
主方法,MainApplication
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
三、运行
注意:拦截器目前设置的条件是只运行name=admin的条件通过。
运行网址:http://127.0.0.1:8090/hello?name=admin
**运行方式:右键单击主方法程序——run as —— java application 然后按照上面的运行网址,运行程序,浏览器中会出现如下画面 **
返回到程序编辑窗口,查看console,出现如下画面,证明拦截器设置成功。