我尝试制作一个与服务器上的存储库连接的 Web 应用程序,并且我想在屏幕上打印数据。实际上我尝试创建一个 RESTful 客户端。可能有一个解析错误,不允许我看到显示数据我的jsp如下:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Get Author</h1>
<c:if test="${empty author}">
No records found!
</c:if>
<c:if test="${!empty author}">
<table style="border: 1px solid #333">
<tr>
<td style="width: 100px">Id</td>
<td>${data.id}</td>
</tr>
<tr>
<td>Name</td>
<td>${data.name}</td>
</tr>
<tr>
<td>Address</td>
<td>${data.address}</td>
</tr>
</table>
</c:if>
</body>
</html>
控制器如下(我用两种不同的方式实现控制器。其中一种已注释)
@Controller
public class Contraller {
protected static Logger logger = Logger.getLogger("controller");
private RestTemplate restTemplate = new RestTemplate();
@RequestMapping(value = "/datas", method = RequestMethod.GET)
public String getDatas(Model model) {
HttpEntity<Data> entity = new HttpEntity<Data>(headers);
// Send the request as GET
try {
ResponseEntity<DataList> result = restTemplate.exchange("http://www../data/",
HttpMethod.GET, entity, DataList.class);
// Add to model
model.addAttribute("datas", result.getBody().getData());
} catch (Exception e) {
}
// This will resolve to /WEB-INF/jsp/personspage.jsp
return "personspage";
}
/**
* Retrieves a single record from the REST provider
* and displays the result in a JSP page
*/
@RequestMapping(value = "/data", method = RequestMethod.GET)
public String getMyData(@RequestParam("id") Long id, Model model) {
try{
Data results = restTemplate.getForObject("http://www.../data/{id}",
Data.class, id);
model.addAttribute("data", results);
}catch(Exception e){
}
return "getpage";
}
该模型:
@XmlRootElement(name = "data", namespace="http://www...")
@XmlAccessorType(XmlAccessType.FIELD)
public class Data {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setFirstName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
在存储库中有 4000 个数据。当我给出以下 URL 时: localhost:8080/Client_for_rest/data?id=1 (从 1 到 4000 的所有 id)返回给我视图,但它不显示数据。如果我给出一个大于 4000 的 id,即 4001 会告诉我没有记录是真的。据此,我假设客户端与服务器端连接,但存在一个问题(我想是解析)它不允许在视图上显示数据。我不熟悉 Spring MVC 框架,我读过一些关于 marshaller 和 castor 的东西,但我不知道如何实现它们。其实我想知道是否有更简单的方法来解决我的问题。我必须使用 pojo maven 依赖项等吗?