如何使用jstl在jsp中获取数据? [英] how to get data in jsp using jstl?

查看:236
本文介绍了如何使用jstl在jsp中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在使用scriptlet在jsp中获取数据,但现在想使用JSTL,因此我试图将自己的scriptlet代码转换为JSTL.但是对于以下情况,我不知道如何在jstl中获取数据.

Currently I'm getting data in jsp using scriptlet but now want to use JSTL so I'm trying to convert my scriptlet code to JSTL. But for following scenario I don't know how to get data in jstl.

让我通过示例进行解释:

Let me explain by example:

有3个对象

  1. 用户(用户名,用户名,密码)
  2. Box(box_id,box_name,list_of_boxCat)
  3. BoxCat(box_cat_id,box_id,user_id,cat_name)

现在,我需要显示用户拥有的盒子列表.因此,我通过在servlet中触发查询来创建Box对象的列表,并将该列表传递给jsp的request属性,然后在jsp中对其进行访问.在此之前,一切正常.但是现在我必须访问BoxCat对象,该对象没有直接来自Box对象的任何引用.要获得BoxCat对象,我必须将Userid和Box ID结合起来,然后才能获得BoxCat ID.因此,在scriptlet中,我调用我的DAO并通过运行查询获取列表.但是我不知道如何执行此JSTL.有人请帮我怎么做?

Now I need to display list of boxes a user own. So i have created a list of Box objects by firing query in servlet and pass that list in jsp in request attribute then access it in jsp. Until this everything goes fine. But now I have to access BoxCat object which do not have any reference from Box object directly. To get BoxCat object I have to combine Userid and Box id and then I can get BoxCat id. So in scriptlet I call my DAO and get list by running query. But I don't know how to do this JSTL. Anyone please help me on how to do this?

推荐答案

您应该重新设计或映射模型,使其适合您的视图需求.视图是否需要List<BoxCat>作为User的属性,或者需要BoxCat作为Box的属性?如果是这样,那么就这样做,然后更改您的控制器和DAO以预先填充它.

You should redesign or map your model so that it suits whatever your view needs. Does the view need a List<BoxCat> as a property of User or maybe BoxCat as a property of Box? If so, then make that so and change your controller and DAO to fill that beforehand.

否则,使用实体通过其ID(例如Map<Long, Entity>)进行的映射,最终将导致笨拙且可能导致内存效率低下的变通方法.

Otherwise you will end up in clumsy and potentially memory-inefficient workarounds using a mapping of the entities by their ID such as Map<Long, Entity>.

这篇关于如何使用jstl在jsp中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆