我正在努力在JSP中创建自定义标记以提供数据库连接。 我制作了标签并将其与相应的java类相关联。 但我无法从index.jsp页面获取传递给此自定义标记的参数。 这是文件。
form.jsp
<%@ page import="java.util.*,java.io.*;" language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Get User Data</title>
</head>
<body bgcolor="#AABBCC">
<form name="form" action="dbConnect.jsp" method="POST">
<table>
<tr>
<td>JDBC Driver</td>
<td> <input type="text" name="jdbcDriver" size="30"> </td>
</tr>
<tr>
<td>JDBC URL</td>
<td><input type="text" name="serverURL" size="30"></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" size="30"></td>
</tr>
<tr>
<td>Query</td>
<td><input type="text" name="query" size="30"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit_query" value="Submit" ></td>
</tr>
</table>
</form>
</body>
</html>
customTag.tld
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<tag>
<name>dbTag</name>
<tag-class>com.appperfect.training.CustomDBTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>jdbcDriver</name>
<required>true</required>
<type>java.lang.String</type>
<fragment>false</fragment>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>serverURL</name>
<required>true</required>
<type>java.lang.String</type>
<fragment>false</fragment>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>username</name>
<required>true</required>
<type>java.lang.String</type>
<fragment>false</fragment>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>password</name>
<required>true</required>
<type>java.lang.String</type>
<fragment>false</fragment>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>query</name>
<required>true</required>
<type>java.lang.String</type>
<fragment>false</fragment>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
dbConnect.jsp
<%@ page import="java.util.*,java.io.*,java.sql.*" %>
<%@ taglib prefix="db" uri="WEB-INF/CustomTag.tld" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String driver, url, uname, pass, query;
driver = "jdbcDriver";
url = "serverURL";
uname = "username";
pass = "password";
query = "query";
%>
<db:dbTag
jdbcDriver="<%= request.getAttribute(driver).toString() %>"
serverURL="<%= request.getAttribute(url).toString() %>"
username="<%= request.getAttribute(uname).toString() %>"
password="<%= request.getAttribute(pass).toString() %>"
query="<%= request.getAttribute(query).toString() %>">
</db:dbTag>
</body>
</html>
customDBTag.java
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class CustomDBTag extends SimpleTagSupport {
private String jdbcDriver, serverURL, username, password, query;
public void setJdbcDriver(String driver) {
this.jdbcDriver = driver;
}
public void setServerURL(String url) {
this.serverURL = url;
}
public void setUsername(String uname) {
this.username = uname;
}
public void setPassword(String pass) {
this.password = pass;
}
public void setQuery(String que) {
this.query = que;
}
StringWriter sw = new StringWriter();
@Override
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
if(jdbcDriver!=null)
out.println(jdbcDriver);
if(serverURL!=null)
out.println(serverURL);
if(username!=null)
out.println(username);
if(password!=null)
out.println(password);
if(query!=null)
out.println(query);
}
}
有谁能建议我做错了什么?
PS:我在Mac OS X 10.7上使用Eclipse EE Indigo,Apache Tomcat 7和Mysql 5
答案 0 :(得分:1)
您正在发送参数和阅读属性
将其更改为读取参数
jdbcDriver=<%= request.getParameter("driver")%>
另见