我尝试制作一个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个DATAS。当我给出以下URL:localhost:8080/Client_for_rest/data?id = 1 (所有ID从1到4000)返回给我的视图,但它不显示数据。如果我给一个大于4000的ID,即4001给我回来,没有记录是真的。据此我想,客户端连接到服务器端,但有一个问题(我想解析),它不允许数据在视图上显示。我不熟悉Spring MVC框架,并且阅读了一些关于编组和脚本的知识,但我不知道如何实现它们。其实我想知道是否有更简单的方法来解决我的问题。我必须使用pojo maven依赖关系吗?
请人!在你的控制器中有 –
有一些异常块,你可以在那里打印堆栈跟踪。这只是为了检查代码是否进入并且异常被抑制。你也可以在getPerson方法中保留一个断点,并检查其余的调用是否给出了想要的作者。这些是我想缩小问题的一些线索。 – raddykrish
也可以尝试在调试器中运行或在'model.addAttribute()'的行的上方添加一些日志记录,以便您可以查看Author对象是否使用值填充。还可以考虑通过spring xml创建restTemplate,并在其中添加mediaType声明,然后将注释添加到控制器中。最好还是把所有的代码放到Service类中,以便测试它。 – nickdos