Springmvc实现
(1)先创建lombok
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
(2)实体类User
package com.gt.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
}
(3)我们来获取一个集合对象,展示到前端页面 controller添加方法
```java
package com.gt.controller;
import com.gt.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//自动返回一个字符串
@RestController
public class AjaxController {
@RequestMapping("/t1")
public String test() {
return "hello";
}
@RequestMapping("/a1")
public void a1(String name, HttpServletResponse response) throws IOException {
System.out.println("a1:param=>+name");
if ("gt".equals(name)) {
response.getWriter().print("true");
} else {
response.getWriter().print("false");
}
}
@RequestMapping("/a2")
public List<User> a2() {
List<User> userList = new ArrayList<User>();
// 添加数据
userList.add(new User("山姆说Java", 1, "男"));
userList.add(new User("山姆说前端", 2, "男"));
userList.add(new User("山姆说后端", 3, "男"));
return userList;
}
}
```
(4) 前端页面 Test2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>