使用MVC和DAO模式在JSP页面中在HTML中显示JDBC结果集
我正在使用JSP和JDBC实现MVC。我已经将数据库类文件导入了我的JSP文件,我想显示DB表的数据。我不知道如何将 Resultset
从Java类返回到JSP页面并将其嵌入HTML。
我该如何实现?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在设计良好的MVC方法中,JSP文件不应包含任何Java代码行,而Servlet类不应包含任何JDBC代码行。
假设您想在网络商店中显示产品列表,则需要创建以下代码。
a
产品
代表产品真实实体的类,它应该只是 javabean 。a dao JDBC工作并返回一个不错的
list< product>
。a servlet class 获得列表并将其放在请求范围中。
最后a jsp 在
/web-inf/products.jsps.jsps.jsp
中使用/web-inf/products.jsp
a href =“ https://stackoverflow.com/tags/jstl/info”> jstl< c:foreach>
以迭代list> list< product< product> 可在 el
,并使用JSTL$ {products}
< c:out>
逃脱字符串属性,以避免 XSS 当涉及用户 - 控制输入时,为了使其工作,只需通过其URL致电servlet。前提是servlet被注释
@webservlet(“/products”)
或用web.xml
用< url-pattern>/products</url-模式>
,然后您可以通过http://example.com/contextname/products
来调用它,请参见:
In a well designed MVC approach, the JSP file should not contain any line of Java code and the servlet class should not contain any line of JDBC code.
Assuming that you want to show a list of products in a webshop, the following code needs to be created.
A
Product
class representing a real world entity of a product, it should be just a Javabean.A DAO class which does all the nasty JDBC work and returns a nice
List<Product>
.A servlet class which obtains the list and puts it in the request scope.
Finally a JSP file in
/WEB-INF/products.jsp
which uses JSTL<c:forEach>
to iterate overList<Product>
which is made available in EL by${products}
, and uses JSTL<c:out>
to escape string properties in order to avoid XSS holes when it concerns user-controlled input.To get it to work, just call the servlet by its URL. Provided that the servlet is annotated
@WebServlet("/products")
or mapped inweb.xml
with<url-pattern>/products</url-pattern>
, then you can call it byhttp://example.com/contextname/products
See also:
在Web应用程序上下文中,MVC不包括使用JSP的类。它在于使用以下模型:
因为JSP通常使用JSP标签(例如JSTL)和JSP Expression语言,并且由于JSP标签和EL设计为从Javabeans获取信息,您最好以Javabeans或Javabeans集合的形式获得数据。
因此,控制器(操作类)的作用是获取数据,创建包含数据的Javabean实例,以适合JSP的格式,将它们放在请求属性中,然后将其派往JSP。然后,JSP将通过Javabean实例迭代并显示其包含的内容。
您不应该自己实施MVC框架。使用现有的(条纹,支柱等)
MVC, in a web application context, doesn't consist in using a class from a JSP. It consists in using the following model :
Since the JSP usually uses JSP tags (the JSTL, for example) and the JSP expression language, and since JSP tags and the EL are designed to get information from JavaBeans, you'd better have your data available in the form of JavaBeans or collections of JavaBeans.
The role of the controller (the action class) is thus to fetch the data, to create JavaBean instances containing the data, in a suitable format for the JSP, to put them in request attributes, and then to dispatch to the JSP. The JSP will then iterate through the JavaBean instances and display what they contain.
You should not implement the MVC framework yourself. Use existing ones (Stripes, Struts, etc.)
我不知道应该如何将结果集从类文件返回到JSP页面
好吧,您不这样做。
mvc 的点是将模型(在这种情况下为 m db信息)与您的视图( v a JSP)分开)以这种方式,您可以在不制动应用的情况下更改视图。
为此,您可能会使用中间对象表示您的数据(通常称为DTO - 在数据传输对象之后 - 不知道它们是如何称呼它的),而其他对象则获取它(通常是DAO)。
因此,基本上您有JSP文件,获取请求参数,然后从DAO中调用一种方法。 DAO在内部有连接到数据库并获取数据并构建DTO集合的方法,该集合将返回到JSP进行渲染。
像这样简化(和不安全)的代码类似的内容:
employee.java
opplyeedao.java
employe.jsp
我希望这给您一个更好的主意。
I don't know how should I return the ResultSet from the class file to the JSP page
Well, you don't.
The point of MVC is to separate your model ( the M DB info in this case ) from your view ( V a jsp, in this case ) in such a way you can change the view without braking to application.
To do this you might use an intermediate object to represent your data ( usually called DTO - after Data Transfer Object -, don't know how they call it these days ), and other object to fetch it ( usually a DAO ).
So basically you have your JSP file, get the request parameters, and then invoke a method from the DAO. The dao, internally has the means to connect to the db and fetch the data and builds a collections of DTO's which are returned to the JSP for rendering.
Something like this extremely simplified ( and insecure ) code:
Employee.java
EmployeeDAO.java
employee.jsp
I hope this give you a better idea.
我有问题。我不明确理解代码。我的代码也有类似的问题。
我创建了数据库SQL并填写。然后,我想实现来自数据库和其他JSP页面中富裕数据的MainServlet(下面的代码),我想在H1,H2 ECC等部分中插入该数据...我必须使用$ {} Sintax,但我不喜欢知道这是怎么回事。
简而言之,在JSP文件(下面的代码,我必须使用$ {} sintax)我想“呼叫” Mainservlet,在那里我想从数据库中富裕数据并在JSP文件中查看。
希望我能正确解释,非常感谢!
mainservlet.java
dublerow.jsp
这是我的数据库:
我想将每个珠宝插入不同页面(每个珠宝都有一个JSP文件)
I have a problem. I don't understand clearly the code. I have a similar problem with my code.
I have created database SQL and filled up. Then I want to implement a MainServlet (code below) that richieve data from database and in a different jsp page, I want to insert that data in section like h1, h2 ecc... I must use the ${} sintax but I don't know how do that.
Briefly, In jsp file (code below, I MUST USE ${} SINTAX) I want to "call" MainServlet and there I want to richieve data from database and view in jsp file.
I hope I have explained correctly, thank you very much!
MainServlet.java
doublerow.jsp
This is my database:
I want to insert each jewel in different pages (each jewel have a jsp file)
您可以使用
&lt; c:foreach&gt;
tag您可以在以下链接中找到详细的示例 示例使用
You can use the
<c:forEach >
tagyou can find a detailed example in the following link example use
我认为,将表数据包含在列表之类的集合中,并从Java类返回列表,然后在JSP中重复使用此集合,这将更好。
I think it will be better for you to contain the data of the table into a collection such as list and return the list from the Java class and reuse this collection in the JSP.