您的位置:首页 > Web前端 > JavaScript

(controller与ajax之间的数据传输)关于controller无法返回json类型参数回ajax中的问题

2019-05-09 16:10 441 查看

最近在写一个基于ssm框架的javaWeb项目,第一次使用ssm框架,遇到了controller无法将json返回页面的ajax中,昨天下午得到了解决,现在在这里做一下记录。

首先要导入json的三个架包:

下载地址:https://github.com/Lyr0422/llyr/tree/master/jackson
接下来配置springmvc.xml配置文件,配置json格式数据转换

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

<context:component-scan base-package="com.shopWatch.controller" />

<mvc:annotation-driven />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver" />

<!--json格式数据转换的配置  -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
p:ignoreDefaultModelOnRedirect="true">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>

</beans>

然后就是controller与ajax部分的编写了,需注意的是,在controller类中的方法上面要加

@ResponseBody
注解。

实现简单登录验证的controller层代码:

@RequestMapping(value = "userLogin.action", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> UserLogin(User user,HttpServletRequest request) {
Map<String, String> rel = new HashMap<String, String>();
User isUser=userService.UserLogin(user);
if(isUser==null) {
rel.put("type", "error");
rel.put("msg", "用户名或密码错误");
return rel;
}
rel.put("type", "success");
rel.put("msg", "登录成功!");
return rel;
}

在jsp页面中的ajax代码:

<input type="button" id="ok" name="ok" value="登录" onclick="login()"/>
function login(){
var username=$("#userName").val();
var password=$("#passWord").val();
$.ajax({
url:'${pageContext.request.contextPath}/userLogin.action',
data:{username:username,password:password},
type:'post',
dataType:'json',
success:function(data){
if(data.type=="success"){
alert(data.msg);
window.location.href="${pageContext.request.contextPath}/findAllGoods.action"
}else{
alert(data.msg);
$("#passWord").val("");
}
}
})
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐