SpringMVC的AJAX返回

SpringMVC非常适合跟页面的异步交互。

控制器代码:

package com.smile.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smile.po.UserInfo;

@Controller
public class UserController {

	@RequestMapping("/ajaxUser.do")
	public void userNumber(String userNumber, HttpServletResponse response) {

		System.out.println(userNumber);
		try {
			// 响应
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();

			if ("smile".equals(userNumber)) {
				out.println("对不起,请重新输入账号");
			} else {
				out.println("恭喜您,账号可以使用");
			}
			out.flush();
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/ajaxUser2.do")
	public @ResponseBody UserInfo ajaxUser(Integer userId){
		
		System.out.println("编号:"+userId);
		UserInfo user = new UserInfo();
		user.setUserId(userId);
		user.setUserName("张三");
		user.setUserAge(18);
		return user;
	}

}
bean类代码:

package com.smile.po;

/**
 * 实体类
 *
 */
public class UserInfo {

	private Integer userId;
	
	private String userName;
	
	private Integer userAge;

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Integer getUserAge() {
		return userAge;
	}

	public void setUserAge(Integer userAge) {
		this.userAge = userAge;
	}

	@Override
	public String toString() {
		return "UserInfo [userId=" + userId + ", userName=" + userName
				+ ", userAge=" + userAge + "]";
	}
	
	
}

最后来实现jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
	$().ready(function(){
		
		$("#but01").click(function(){
			var userNumber = $("#userNumber").val();
			if(userNumber.length == 0){
				alert('请输入账号');
			}else{
				$.post("<%=basePath%>ajaxUser.do",{userNumber:userNumber},function(data){
					alert(data);
				});
			}
			
		});
		
		$("#but02").click(function(){
			var userId = $("#userId").val();
		
			$.post("<%=basePath%>ajaxUser2.do",{userId:userId},function(data){
				alert(data.userId+' --'+data.userName+' --- '+data.userAge);
			},"json");
			
			
		});
		
		
	});

</script>
</head>
<body>

<hr>
账号:<input type="text" id="userNumber" name="userNumber"/>
<input type="button" id="but01" value="验证"/>
<hr/>

编号:<input type="text" id="userId" name="userId"/>
<input type="button" id="but02" value="查询"/>
<hr/>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值