这样一个问题如何处理,有关页面的include
就是一个典型的电子商务网站的首页index.jsp,外观上来看分成三个区域,上面一栏top.jsp,下面分成左边login.jsp,即未登录时显示登录框,已登录时显示欢迎信息什么的,右面就是主页面
由于三个区域都用到数据库查询,我把设计数据库查询的类比如mypackge.mydb用
<jsp:useBean id="myBean" class="mypackage.mydb" scope="page"/ >
定义在top.jsp里,然后index.jsp里用
<%@ include file="top.jsp"%>
包含进来,之后index.jsp里就可以用myBean进行数据库操作了。
但是login.jsp里也包含对数据库的操作,用
<%@ include file="login.jsp"%>
把它include到index.jsp里之后,我认为它也可以使用myBean,因为实际上top.jsp和login.jsp在一个页面里。可是index.jsp可以编译通过,login.jsp总是报找不到myBean这个变量,我应该怎么处理?
上面三个页面的包含关系实际上我是从一个网上下载的源码例子改的,可是那个例子可以运行,我的却编译不通过。
问题点数:20、回复次数:6Top
1 楼tiannet(http://tiannet.yculblog.com)回复于 2005-09-04 10:27:50 得分 0
不用useBean
直接new好了Top
2 楼simon0512(虫虫)回复于 2005-09-04 10:31:14 得分 0
代码贴出来看看Top
3 楼losingaries()回复于 2005-09-04 11:11:40 得分 0
to tiannet(天网) :直接new 的话login.jsp里一样会编译不通过的亚Top
4 楼losingaries()回复于 2005-09-04 11:14:50 得分 0
有这样三个文件:top.jsp, index.jsp,login.jsp
top.jsp的内容大致是:
<%@ page contentType="text/html;charset=gbk" language="java" import="mypackage.*" %>
<head>
<link href="css.css" rel="stylesheet" type="text/css">
<jsp:useBean id="myBean" class="mypackage.myBean" scope="page"/>
</head>
index.jsp的内容大致是:(包含了top.jsp和login.jsp)
<%@ page import="mypackage.*,java.sql.*"%>
<html>
<head>
</head>
<%@ include file="top.jsp"%>
<body bgcolor="#ffffff">
<table width="778" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="10"></td>
</tr>
</table>
<table width="778" border="0" cellspacing="0" cellpadding="0" align="center" height="148">
<!--DWLayoutTable-->
<tr valign="top">
<td width="180"> <table width="180" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top">
<%@ include file="login.jsp"%>
</td>
</tr>
...
</body>
</html>
login.jsp的内容大致:(这里把具体的条件略去了,代之以true)
<%@ page import="mypackage.*,java.sql.*"%>
<table border="0" width="180" cellspacing="0" cellpadding="0" height="18">
<%
if(true)
{
PreparedStatement ps = null;
ResultSet rs = null;
Connection cn = null;
cn=myBean.getConnection();
ps=cn.prepareStatement("select * from tb_userInfo where realname='"+name+"'");
rs=ps.executeQuery();
...
<%
}
}else
//not logged
{
%>
...
<%}%>
</table>
由于top.jsp里定义了myBean,所以index.jsp中静态的把top.jsp include进来之后,就可以使用myBean,编译没有问题
但是login.jsp使用myBean就会编译不通过,我include使用的是<%include %>的方式,所以我认为index.jsp编译的时候应该是把top.jsp和login.jsp放进来一起编译的,但是运行的时候左边也就是login.jsp就是显示不出来Top
5 楼cvb114215(str_conn)回复于 2005-09-04 12:16:33 得分 0
top.jsp改成
<%@ page contentType="text/html;charset=gbk" language="java" import="mypackage.*" %>
<jsp:useBean id="myBean" class="mypackage.myBean" scope="page"/>
<head>
<link href="css.css" rel="stylesheet" type="text/css">
</head>
试试Top
6 楼cvb114215(str_conn)回复于 2005-09-04 12:20:12 得分 0
还有login.jsp页面是不是该把变量定义在if语句前面啊
PreparedStatement ps = null;
ResultSet rs = null;
Connection cn = null;
if(true)Top




