java - View 解析出错?

标签 java jsp rest spring-mvc

我尝试制作一个与服务器上的存储库连接的 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 如下(我用两种不同的方式实现 Controller 。其中一种已注释)

  @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 (所有 id 从 1 到 4000)返回 View ,但不显示数据。如果我给出的 id 大于 4000,即 4001,则返回没有真实记录。据此,我认为客户端与服务器端连接,但存在一个问题(我想是解析),它不允许在 View 上显示数据。我不熟悉 Spring MVC 框架,我读过一些有关 marshaller 和 Castor 的内容,但我不知道如何实现它们。其实我想知道是否有更简单的方法来解决我的问题。我必须使用 pojo maven 依赖项等吗?

最佳答案

如果方法返回类型是字符串,请使用映射对象和bindresult作为该方法的参数。

并向该 map 对象添加元素。

您可以直接在 jsp 页面上访问该 map 对象

举个例子:

@RequestMapping("/locationUpdate.do")
    public String locationUpdate(Map<String, Object> map,@ModelAttribute("location") Location location, BindingResult result) {
        map.put("location", locationService.getLocationByCode(locationcode));
        map.put("locGrp", listLocationGroup());

        return "location/locationAdd";
    }

现在您可以在 jsp 中直接访问位置和 locGrp

关于java - View 解析出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10376658/

相关文章:

python - 在 Python 中模拟 HTTP 服务器

java - 阻塞或非阻塞 - 在 Java 的 HashMap 中重新散列期间添加元素

java - 如何在magnolia中的jsp中迭代ContentMap

java - Java代码模型是否支持GenericEntity

java - 将非英文字符的数据传递给网络服务器

java - 在两个不同的 html 表中显示从 GAE 数据存储中检索到的数据

ruby-on-rails - 以多个 id 作为参数的 Rails 3 自定义路由

arrays - 使用嵌套结构数组创建 Coldfusion JSON 结构

java - LinkedBlockingQueue 和原语

java - 如何使用 Mockito 在 Java 中模拟链式依赖