<!--配置扫描注解-->
<context:component-scan base-package="com.th"></context:component-scan>
<!--配置试图解析器view-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置访问文件路径-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!--配置文件后缀名-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--配置静态访问资源,不被拦截-->
<mvc:resources mapping="/js/" location="/js/**"></mvc:resources>
<!--开启springmvc框架注解的支持-->
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<script src="js/jquery-3.5.1.min.js"></script>
<script>
$(function () {
$("#btn").click(function () {
alert(123)
$.ajax({
url:"user/textAjax",
contentType:"application/json;charset=UTF-8",
date:'{"username":"tanh","password":"ddndPB","age":18}',
dataType:"json",
type:"post",
success:function (date) {
alert(date.username)
}
});
});
});
</script>
@Controller
@RequestMapping("/user")
public class ControllerTh {
/**
* 异步传输
*/
@RequestMapping(value = "/textAjax",method = RequestMethod.POST)
public @ResponseBody User textAjax(@RequestBody User user){
System.out.println("执行了。。。。");
user.setUsername("tan");
return user;
}
你的User对象怎么定义的有get、set方法么
User对象没有实现Serializable接口不能序列化,最后接收到的对象为空所以报错了,在User对象后面加上下面的这行代码
implements Serializable
变成这样:
public class User implements Serializable{
............
}
ajax参数修改一下 date-->data httpMessageConverter消息转换器 没有拿到你前台传过来的消息