使用jsp作为视图和控制器
我有以下课程 JAVA 文件(数据)
package p1;
class data
{
private String pro;
private String sta;
public void set1(String a)
{
pro=a;
}
public void set2(String b)
{
sta=b;
}
}
用于从数据库检索数据的类文件 JAVA 文件(conb)
package p1;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;
import p1.*;
public class conb
{
public static List<data> datadb() throws SQLException
{
List<data> n1=new ArrayList<data>();
Connection conn = null;
Statement st = null;
try
{
String userName = "frank";
String password = "asdf";
String url = "jdbc:mysql://localhost:8080/work";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
}
catch (Exception e)
{
e.printStackTrace();
}
try {
st = conn.createStatement();
st.execute("select * from work1");
ResultSet rs = st.getResultSet();
while(rs.next())
{
data d1=new data();
d1.set1(rs.getString("pron"));
d1.set2(rs.getString("sdata"));
n1.add(d1);
}
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
conn.close ();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
return n1;
}
}
该文件位于 WEB-INF/Classes/p1 中 我想在jsp文件中显示数据(使用jsp作为控制器和视图) 我在windows下使用的是tomcat 6.0。 我也为此编写了 jsp 页面,
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ import "java.util.*" %>
<%@ import "p1.conb" %>
<!doctype HTML public "-//W3C//DTD HTML 4.0 Frameset//EN">
</head>
<body>
<table cellpadding="2" cellspacing="2" width="100%">
<tr>
<td>Name</td>
<td>Start time</td>
</tr>
<tr>
<%
List<data> da1 = new List<data>();
da1=p1.conb.datadb();
%>
<c:forEach items="${da1}" >
<td><c:out value="{$da1.pro}" /></td>
<td><c:out value="{$da1.sta}" /></td>
</tr>
</c:forEach>
</table>
<html>
<head>
但出现错误 我如何仅使用jsp从数据库检索数据。 我不想使用 servlet 作为控制器。
错误是:
org.apache.jasper.JasperException:org.apache.jasper.JasperException:无法加载JSP org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:161)的类org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:340) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.爪哇:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我必须承认,我对这个答案感到不舒服,但如果您只是想让它运行,请删除 循环。
将其替换
为类似的内容(您必须在
data
类中创建 getter 方法:仅供参考 - 您的代码存在大量问题,仅举几例:
I must admint that I do not feel comfortable with this answer, but if you simply want to get it running remove the
<c:forEach [...]
loop.Replace this
with something like this (you have to create the getter methods in your
data
class:Just for info - there is a huge number of issues with your code, to name a few:
我意识到我迟到了,但是当我重构 JSP 以停止使用 scriptlet 时,我已经多次遇到过此类问题。在中间阶段,很多时候我都有加载数据的 scriptlet,但我想将该数据与一些 JSTL 标签/EL 表达式一起使用。这是我通常的临时解决方案:
您可以轻松地将数据添加为请求范围中的属性,但我会尽可能从本地开始,然后根据需要转移到更广泛的上下文。
I realize that I'm late to the party, but I've come across this type of problem many times as I've refactored JSPs to stop using scriptlets. In the intermediate stages, there are a lot of times where I have scriptlets that load data, but I want to use that data with some JSTL tags/EL expressions. This is my usual, temporary solution:
You could just as easily add your data as an attribute in the request scope instead, but I start as local as possible and move to broader contexts as I need it.