我编写了一个带有表单的JSP,表单包含下拉列表和texfield。使用bean作为DAO和DTO从mysql数据库填充下拉列表。问题是下拉列表不显示数据库中的值,但是当我从表单中删除文本字段并仅保留下拉列表时,它显示正常。谁知道为什么?请让我知道该怎么做。表格的代码如下:
有效的JSP:
<%@ page import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="camp" scope="session" class="p1.campusDao"/>
<%--
Document : addCamp
Created on : May 20, 2011, 11:28:31 AM
Author : ken
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
body {
background-color:#AFC7C7;
padding: 0;
margin: 0;
}
div.wrapper {
margin-left: 10%;
margin-right: 10%;
background-color:#6D7B8D;
height: 607px;
padding-top:0px;
border: thin solid #000000;
}
div#image{
padding-top:1%;
padding-bottom:1%;
}
div#adminlogin{
width:35%;
height:40%;
background-color:#AFC7C7;
border-width:thin;
border-style:solid;
border-color:#000000;
margin: 0 auto;
text-align:left;
overflow: hidden;
padding: 5px;
}
hr {
height:1px;
color:#000000;
background-color:#000000;
width:99%;
margin-left: 0 ;
margin-right: auto ;
border-style:solid;
}
.inputtext {
width: 250px;
height: 30px;
Font-Family:Arial;
Font-Size:18px
}
</style>
<script language="javascript" type="text/javascript">
function clearText(field){
if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;
}
</script>
</head>
<body>
<div class="wrapper">
<div id="image">
<p>
<img src="${pageContext.request.contextPath}/images/logo.gif"
alt="banner"
width=100%
height="100"
/></p>
</div><!--end of image div-->
<div id=adminlogin >
<p style="text-align:center">Adding Hostel</p>
<hr/>
<form id="cam" method="POST" action="${pageContext.request.contextPath}/getHos">
<select name="campuses" size="1" id="camps">
<c:forEach items="${camp.stateList}" var="ca">
<%--<option value="1"><c:out value="${ca.campnm}"/></option>--%>
<option>${ca.campnm}</option>
</c:forEach>
</select>
<br>
<input type="Submit" name="cmdSub" value="SUBMIT">
</form>
<p STYLE="color : #E41B17;">${message}</p>
<c:remove var="message" scope="session" />
</div><%--end of admin login div--%>
</div>
</body>
</html>
带有textfield形式的JSP,并且不显示UI中的下拉元素:
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ page import="java.util.*" %>
<jsp:useBean id="campu" scope="session" class="p1.campusDao"/>
<%--
Document : addCamp
Created on : May 20, 2011, 11:28:31 AM
Author : ken
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
body {
background-color:#AFC7C7;
padding: 0;
margin: 0;
}
div.wrapper {
margin-left: 10%;
margin-right: 10%;
background-color:#6D7B8D;
height: 607px;
padding-top:0px;
border: thin solid #000000;
}
div#image{
padding-top:1%;
padding-bottom:1%;
}
div#adminlogin{
width:35%;
height:40%;
background-color:#AFC7C7;
border-width:thin;
border-style:solid;
border-color:#000000;
margin: 0 auto;
text-align:left;
overflow: hidden;
padding: 5px;
}
hr {
height:1px;
color:#000000;
background-color:#000000;
width:99%;
margin-left: 0 ;
margin-right: auto ;
border-style:solid;
}
.inputtext {
width: 250px;
height: 30px;
Font-Family:Arial;
Font-Size:18px
}
</style>
<script language="javascript" type="text/javascript">
function clearText(field){
if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;
}
</script>
</head>
<body>
<div class="wrapper">
<div id="image">
<p>
<img src="${pageContext.request.contextPath}/images/logo.gif"
alt="banner"
width=100%
height="100"
/></p>
</div><!--end of image div-->
<div id=adminlogin >
<p style="text-align:center">Adding Hostel</p>
<hr/>
<form method=POST id="hostel" action="${pageContext.request.contextPath}/getHos">
<p/>
<%--The textfield for hostel name--%>
<label>Hostel Name<input type="text" id="hostel" name="hosNm" value=""/></label><br/>
<br/>
<label>Campus Name <select name="campuses" size="1" id="hostel">
<c:forEach items="${campu.stateList}" var="cam">
<%--<option value="1"><c:out value="${cam.campnm}"/></option>--%>
<option>${cam.campnm}</option>
</c:forEach>
</select></label>
<br/>
<br/>
<input type="submit" value="Add" style="font-size:14pt; margin-left: 158px; height: 30px;"/>
</form>
<p STYLE="color : #E41B17;">${message}</p>
<c:remove var="message" scope="session" />
</div><%--end of admin login div--%>
</div>
</body>
</html>
答案 0 :(得分:1)
在你的第二个JSP中(你真的有两个独立的JSP文件吗?)你有一个
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
而不是
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
相应地修复它。然后将解析<c:xxx>
标签。
我也会摆脱<%@ page import="java.util.*" %>
因为它没用,只允许不良做法( scriptlets )。