HTML+AJAX实现增,根据ID查

AJAX:基本的步骤!
$.ajax({
type : “get”,//请求方式
url : “http://localhost:8081/getAllType”,//后台接口
dataType:‘json’, //预期的服务器响应的数据类型
data:{
tid:test1,
},//接口需要传入参数用此段代码

案例一

用AJAX+HTML实现根据ID查询

Controller层

TypeBean:
int tid
String type

	// 根据id查,
	@RequestMapping(value = "getAllType")
	@ResponseBody
	public String getAllType(Integer tid) {
		TypeBean data = typeService.getAllTypeService(tid);
		System.out.println("执行了改查询方法!");
		// 将实体类类型转换成json数据格式
		String jsonString = JSON.toJSONString(data);
		return jsonString;
	}
	
	// 根据id查的访问路径
	@RequestMapping("getAlltoPage")
	public String getAlltoPage() {
		return "listType";

	}
前台页面显示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 引这个js需要网 -->
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<style>
#text{
 border-collspace:collspace;
 };
 tr{
  height:35px;
  
 }
</style>
<body>
<button class="btn">查询</button>
<table id="text">
<!-- 规定这是个头 -->
<thead>
                       <tr>
                            
                            <th>设备类型</th>
                            <th>设备Id</th>
                       
                        </tr>
         </thead>       
                 
 <!-- 这里存放拼接好的值-->
                 <tbody id="content">
                 
                 </tbody>
                    </table> 
                    <from>
                    <input type="text" name="tid" id="tid" >
                    </from>

</body>

<script type="text/javascript">
//根据id查
$(".btn").on("click" , function() {
	var test1=$("#tid").val();
	            $.ajax({
		            type : "get",//请求方式
	                url : "http://localhost:8081/getAllType",//后台接口
	                dataType:'json', //预期的服务器响应的数据类型
	                //接口需要传入参数用此段代码!       
   	            	data:{
	            	  tid:test1,
	                 }, 
	                success : function(data) {   //如果请求成功,返回数据。
	                console.log(data);//在控制打印数据
	                var html = "";
	                  html = "<tr>"+
	                   "<td>"+data.tid+"</td>"+
	                   "<td>"+data.type+"</td>"+
	                   "</tr>";
	                   $("#content").html("");
	                   $("#content").append(html);
	                   $("#test").html(html); //在html页面id=test的标签里显示html内容
	               }
	            })
	        })
</script>

</html>
案例二

用HTML+AJAX实现添加方法
判重名,添加成功返回提示信息

Controller层
	// 添加类型

	@RequestMapping(value = "addType")

	@ResponseBody
	public void addType(String type,HttpServletRequest req, HttpServletResponse resp) throws IOException {
	    resp.setCharacterEncoding("utf-8");
		System.out.println("..." +type);
		String info = "";
		Map<String, String> map = new HashMap();
		if (type!=null) {
			//typeService.addTypeService(typeBean);
			if(type.equals("A类"))
			info = "该名称已存在";
			else {
			info="该名称可用";
			}
		} else {
			info = "名字不可为空!";
		}
	
		map.put("info", info);
		//异步 
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(map));
        writer.flush();
        writer.close();
	}
	// 访问添加的页面
	@RequestMapping(value = "addTypetoPage")
	public String addTypetoPage() {
		return "addType";
	}
前台页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 引这个js需要网 -->
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<style>
#text {
	border-collspace: collspace;
}

;
tr {
	height: 35px;
}
</style>
<body>
	<button class="btn">添加</button>


	<from> <input type="text" name="type" id="tid">
	<table id="text">
		<tbody id="content">
		</tbody>
	</table>
	</from>

</body>

<script type="text/javascript">
//根据id查
$("#tid").on("blur" , function() {
	var test1=$("#tid").val();
	            $.ajax({
		            type : "get",//请求方式
	                url : "http://localhost:8081/addType",//后台请求的数据
	                dataType:'json', //预期的服务器响应的数据类型
	                //接口需要传入参数用此段代码!          
   	            	data:{
	            	  type:test1
	                 }, 
	                success : function(data) {   //如果请求成功,返回数据。
	                console.log(data);
	               // alert(data.info)
	              var html = "";
	                   html = "<tr>"+
	                   "<td>"+data.info+"</td>"+
	                   "</tr>"; 
	                   $("#content").html("");
	                   $("#content").append(html);
	                   $("#test").html(html); //在html页面id=test的标签里显示html内容 
	               }
	            })
	        })
</script>

</html>

目前欠缺:
AJAX: 
1.如何遍历后台集合
2.js基础很薄弱。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot和EasyUI是两个常用的Java Web开发框架,Spring Boot提供了一种快速构建生产级Web应用的方式,而EasyUI则是一个轻量级的前端UI库,常用于简化前端开发,特别是表格操作和数据管理。 要使用Spring Boot和EasyUI实现删改(CRUD)操作,你可以按照以下步骤进行: 1. **添加依赖**: - 在Spring Boot项目中,添加EasyUI的依赖到你的pom.xml文件或build.gradle文件中。 - 对于Maven,添加EasyUI UI Grid的依赖: ```xml <dependency> <groupId>com.jeeplus</groupId> <artifactId>easyui</artifactId> <version>1.3.5</version> </dependency> ``` - 或者对于Gradle: ```groovy implementation 'com.jeeplus:easyui:1.3.5' ``` 2. **创建控制器**: 创建一个控制器类,继承自Spring的`RestController`,用来处理HTTP请求。例如: ```java @RestController public class MyController { @Autowired private YourService yourService; // 假设YourService负责数据库操作 @GetMapping("/list") public List<YourEntity> getList() { return yourService.getAll(); } // ...其他方法如@PostMapping("add"), @PutMapping, @DeleteMapping等,对应删改操作 } ``` `YourService`是你自定义的服务类,包含数据库操作的方法。 3. **编写服务接口和实现**: - 定义一个接口,比如`YourService`,包含CRUD方法的声明。 - 实现这个接口,使用JPA、MyBatis或其他ORM工具进行数据库交互。 4. **前端页面**: - 使用EasyUI的`datagrid`组件展示数据列表。在HTML模板中引用EasyUI样式和JS库,并调用后端API获取数据。 - 使用`form`组件来创建表单,处理用户输入的数据,例如: ```html <table id="dg" title="表格"></table> <form id="form" method="post" action="{:U('yourController', 'add')}" enctype="multipart/form-data"> <!-- 表单元素 --> </form> ``` 5. **Ajax调用**: 利用EasyUI提供的内置Ajax功能,处理数据的删改操作。例如,点击按钮时,通过JavaScript发起POST或PUT请求。 6. **错误处理和响应**: 确保后端对每个HTTP操作返回合适的HTTP状态码和JSON响应,前端可以根据这些信息更新界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值