我嘗試製作一個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