67,513
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<title>1checkUser.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="js/jquery-1.4.2.js"></script>
<script>
$().ready(function(){
$.ajax({
type: "POST",
url: "countAll.action",
cache: false,
dataType: "text",
success: function(data){
var num = JSON.parse(data);
alert(num.data);
$("#countAll").attr("value",num.data);
}
});
})
</script>
<head>
<body>
总人数是:<input type="text" id ="countAll" readonly="readonly"/>
</body>
</html>
package controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import net.sf.json.JSONObject;
import service.UserService;
@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/countAll.action")
@ResponseBody
public String countAll(){
int number = userService.countAll();
JSONObject json = new JSONObject();
json.put("data", String.valueOf(number));
return json.toString();
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.UserMapper">
<select id="countAll" resultType="int">
select count(id) from user
</select>
</mapper>