Spring MVC入门学习之(五)
springmvc的拦截器只能拦截控制器, 不能拦截jsp. 当我们不想让别人访问的jsp文件的时候, 可以把jsp界面放在WEB-INF文件夹中, 这个目录称为安全目录, 不能被外界直接访问.
当然我们可以通过设置过滤器拦截jsp界面.
编写的类需要实现HandlerInterceptor接口, 其中有三个方法(preHandle、postHandler、afterCompletion). 其实生命周期和servlet中的过滤器一致. preHandle方法是在执行控制方法之前执行, 如果返回为true, 放行, 即执行Controller, 如果返回false, 即不放行, 不执行Controller. postHandler为Controller方法执行之后 执行此方法. afterCompletion为最后执行, 即返回视图后才执行
<mvc:interceptors>
<!--配置一个全局的拦截器, 拦截所有请求-->
<!-- <bean class="cn.lacknb.interceptor.SessonInterceptor"/>-->
<mvc:interceptor>
<!--配置拦截器作用的路径-->
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/user/login.do"/>
<mvc:exclude-mapping path="/user/register.do"/>
<bean class="cn.lacknb.interceptor.SessonInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
现在有拦截器111和拦截器222, 执行分别输出111和222.
上述的配置顺序, 当执行完成之后, 输出顺序为:
preHandler------1111
preHandler------2222
postHandler------2222
postHandler------1111
afterCompletion------2222
afterCompletion------1111
需要准备的jar包
<!--自动封装json为实体类对象, 并返回给前台-->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.8</version>
</dependency>
在使用注解开发时需要用到两个重要的JSON格式转换注解, 分别是@RequestBody和@ResponseBody
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function () {
$('#btn').click(function () {
alert("hello")
$.ajax({
/*这里一定要表明contentType为json类型, 否则后台接收不到数据, 会报415错误*/
contentType: "application/json; charset=UTF-8",
url: "${pageContext.request.contextPath}/test/ajax.do",
dataType: "json",
method: "post",
data: JSON.stringify({"name": "张三", "sex": "女", "age": 13 }),
success: function (data) {
console.log(data)
}
})
})
})
</script>
</head>
<body>
<button id="btn">ajax模拟请求</button>
实体类user
package cn.lacknb.bean;
public class User {
private String name;
private String sex;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
控制器controller
package cn.lacknb.controller;
import cn.lacknb.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/test")
public class AjaxTest {
/*
*
* 这里的:
* RequestBody : 接收json数据 并封装成对象
* ResponseBody: 将对象封装成json数据 并返回
*
*
* */
@RequestMapping(value = "/ajax.do", method = RequestMethod.POST)
@ResponseBody
public User ajaxTest(@RequestBody User user){
System.out.println(user);
user.setAge(77);
user.setName("李四");
user.setSex("男");
return user;
}
}
<!--中央调度器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>