chengfn 2015-08-08 13:22 采纳率: 0%
浏览 1492

easyui+jsp+serlet返回数据页面显示的问题

$(function(){
$('#tt').datagrid({
title:'datagrid小例子',
iconCls:'icon-ok',
width:500,
height:320,
nowrap:false,
striped: true,
collapsible:true,
url:'StudentAction',
loadMsg:'数据装载中......',
sortName:'code',
sortOrder:'desc',
remoteSort:false,
frozenColumns:[[
{field:'ck',checkbox:true}
]],
columns:[[
{title:'学号',field:'id',width:'140',rowspan:2,align:'center'},
{title:'姓名',field:'name',width:'150',rowspan:2,align:'center'},
{title:'班级',field:'className',width:'150',rowspan:2,align:'center'}
]],
pagination:true,
rownumbers:true

});
$('#tt').datagrid('getPager').pagination({displayMsg:'当前显示从{from}到{to}共{total}记录'});
});

public class StudentAction extends HttpServlet {

private static final long serialVersionUID = 1L;
private static int Num = 10;
private int total;
private List<Object> rows;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("welcome to studentAction");
      this.total = 100;
      this.rows = new ArrayList<Object>();
      for(int i=0;i<Num;i++){
       Student student = new Student();
       student.setId("id_"+i);
       student.setName("name_"+i);
       student.setClassName("className_"+i);

       Map<String,Object> map = new HashMap<String,Object>();
       map.put("id", student.getId());
       map.put("name", student.getName());
       map.put("className",student.getClassName());

       this.rows.add(map);


      }
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-04 04:28
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在使用EasyUI与JSP和Servlet进行交互时,你需要确保你的服务器端代码(Servlet)能够正确地接收并处理客户端的数据。以下是一些建议来解决你在问题中的问题:

    1. 确保Servlet配置正确

    首先,检查你的Servlet是否正确配置。你可能需要在web.xml文件中添加必要的过滤器或映射。

    <servlet>
       <servlet-name>StudentServlet</servlet-name>
       <servlet-class>com.example.StudentServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
       <servlet-name>StudentServlet</servlet-name>
       <url-pattern>/StudentAction</url-pattern>
    </servlet-mapping>
    

    2. 验证URL路径

    确保你使用的URL路径是正确的,并且包含正确的参数。例如,如果使用的是GET方法,那么URL应该是 /StudentAction?num=10 或者 /StudentAction?num=10&rows=10

    3. 使用适当的HTTP请求头

    在发送数据之前,尝试使用适当的HTTP请求头,比如Content-Type,以告知服务器你想发送什么类型的数据。例如:

    String data = "{'id': 'id_1', 'name': 'name_1', 'className': 'className_1'}";
    HttpServletResponse response = (HttpServletResponse) response;
    response.setContentType("application/json"); // 设置响应内容类型为JSON
    

    4. 解析JSON数据

    一旦接收到数据后,你可以解析它。如果你使用的是Java的Jackson库,可以这样做:

    import com.fasterxml.jackson.databind.ObjectMapper;
    
    ObjectMapper mapper = new ObjectMapper();
    
    try {
        String jsonStr = "[{\"id\":\"id_1\",\"name\":\"name_1\",\"className\":\"className_1\"},{\"id\":\"id_2\",\"name\":\"name_2\",\"className\":\"className_2\"}]";
        Object obj = mapper.readValue(jsonStr, Object.class);
        System.out.println(obj);
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    5. 显示数据

    最后,确保你的前端组件(如GridView)能正确地处理这些数据。你可以通过监听事件或者设置data属性来实现这一点。

    $('#tt').datagrid({
       ...其他选项...
       data: rows,
    });
    

    结论

    以上建议应该帮助你解决在使用EasyUI与JSP和Servlet进行交互时遇到的问题。确保所有步骤都是按照正确的顺序执行,以便正确地传递和展示数据。

    评论

报告相同问题?