Jsp 为什么<;sql:query>;它不能正常工作吗?
我希望在JSP中使用有效的SQL语句,如下所示:Jsp 为什么<;sql:query>;它不能正常工作吗?,jsp,jstl,Jsp,Jstl,我希望在JSP中使用有效的SQL语句,如下所示: <sql:query var="test" dataSource="DL"> select p.name name, j.description job_description, d.description department_description from person as p, job as j, department as d where ... </sql:query> 选择p.name名称、j.desc
<sql:query var="test" dataSource="DL">
select p.name name, j.description job_description, d.description department_description
from person as p, job as j, department as d
where ...
</sql:query>
选择p.name名称、j.description职务描述、d.description部门描述
人员p、工作j、部门d
哪里
因为我连接了3个表,所以我有两个重复的名称:“description”,我通常在SQL中使用这些字段名的别名来解决这个问题。
在JSP中,这是没有正确处理的,结果集不能通过别名访问,只有“描述”可用,但没有“工作描述”或“部门描述”
这是不够的:
<c:forEach var="row" items="${test.rows}">
${row.description}
</c:forEach>
${row.description}
这根本不起作用:
<c:forEach var="row" items="${test.rows}">
${row.job_description}
</c:forEach>
${row.job_description}
这个问题有什么解决方案吗(这是一个bug)
这是my context.xml:
<Context path="/test" docBase="test" reloadable="true" crossContext="true">
<Resource
name="DL"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="xxx"
password="xxx"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"/>
</Context>
我不确定我是否记得正确,但在SQL中,我相信您必须使用
作为
关键字来创建列别名
我不确定我是对的,但试试这个:
<sql:query var="test" dataSource="DL">
select p.name name, j.description as job_description, d.description as department_description
from person as p, job as j, department as d
where ...
</sql:query>
选择p.name名称,j.description作为职务描述,d.description作为部门描述
人员p、工作j、部门d
哪里
如果不起作用,则只需在数据库中为您的选择创建一个视图,并从该视图中选择行或重命名重复的列。问题在于
中的数据源属性。假设您已在同一页面中使用
<!-- rest of attributes omitted here -->
<sql:setDataSource var="DL">
是的,这是一种丑陋的处理方式,但似乎没有其他使用JSTL的解决方案。作为建议,最好从Servlet获取数据,并将结果设置为请求属性的一部分,以便显示它。谢谢。“as”是可选的,但无论如何:这两个选项都不起作用。写视图是可能的,但我不想用通常不必要的东西填满我的数据库。那么,它真的是一只虫子吗?我相信是的,但我不确定。谢谢你,路易吉。但我看不出这有什么区别${DL}只是使用变量而不是固定字符串。将“jdbc”放在DL前面也没有什么区别,因为这只是contenxt.xml中给出的一个名称。因此,就我所知,结果没有变化。@Alex004如果您发布如何定义数据源的开始,这将非常有帮助(因为这可能是问题的根源)。Luiggi,我添加了context.xmlabove@Alex004请做一个测试,如果有效,请保留它。正如我们在秘鲁这里所说的那样:no busques tres pies al-gato,这将被翻译为不要看猫的三只爪子,意思是猫有四只爪子(正常情况下),不要期望它们只有三只爪子(所以不要试图给出更多的问题)。我刚刚测试过。如果我使用“jdbc/DL”或仅仅使用“DL”,则没有任何区别。在这两种情况下都可以找到ressource,问题是相同的。
<sql:query var="test" dataSource="${DL}">
your query...
</sql:query>
<sql:query var="test" dataSource="jdbc/DL">
your query...
</sql:query>
<!-- I came up with the where part, it could be different in your case -->
<sql:query var="test" dataSource="jdbc/DL">
select p.name name,
concat(j.description, '') 'jdescription',
concat(d.description, '') 'ddescription'
from person p, job j, department d
where p.jobID = j.id AND p.departmentID = d.id
</sql:query>
Descriptions:
<br />
<table>
<c:forEach var="row" items="${test.rows}">
<tr>
<td>${row.name}</td>
<td>${row.jdescription}</td>
<td>${row.ddescription}</td>
</tr>
</c:forEach>