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

最近在写一个基于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("");
				}
			}
		})
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值