jsp strut的问题 [英] problem with jsp strut
问题描述
I am creating a page in jsp, using struts, and I am totally abeginner in these.
In my jsp page I have
<html:select styleId="mailerName" property="mailerName">
<%
try {
String url = "jdbc:mysql://localhost:3306/m_details?user=root&password=root";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url);
ResultSet rs;
PreparedStatement pst = con.prepareStatement("select m_name from m_details.m_user");
//String sql="select auto_mailer_name from mailer_details.auto_mailer_2";
rs = pst.executeQuery();
while (rs.next()) {
String name = rs.getString(1);
%>
<option value="<%=name%>"><%=name%></option>
<%
}%><option value="--select--" onclick="disableAll()" selected>--select--</option>
<%
con.close();
rs.close();
} finally {
}
%>
</html:select>
<pre lang="text"><pre lang="text">
每次在select标签中点击一个选项时,都需要显示一个
each time an option clicked in select tag, it need to display a text in
<html:text property="result"/>
,其中是db查询的结果。
我已经提交了表单。但是当我点击提交我在select标签中选择的选项更改为--select--再次。
有没有办法在没有提交的情况下进行?
我需要什么当我单击一个选项时,它需要执行一个java函数,包括带有选项值的数据库查询,结果文本需要显示在文本字段中。
我该怎么做?
, which is a result of a db query.
I had done it with submit the form. But when I click submit the option I selected in select tag change to --select-- again.
Is there any way to do it without submit?
What I need is when I click an option , it need to execute a java function including DB query with option value and result text need to display in the text field.
How can I do this?
推荐答案
Hello Agav,
你可以在m_user和你想要显示的表格之间使用LEFT JOIN文本框中的值。
Hello Agav,
You can use a LEFT JOIN between m_user and whatever the table from which you want to display the value in textbox.
SELECT a.m_name, b.x_something FROM m_details.m_user LEFT JOIN x_table b ON b._x_name = a.m_name
接下来你要做的就是改变你的循环,如下所示。
The next thing you are going to do change your loop as shown below.
<![CDATA[<%rs = pst.executeQuery();
ArrayList valueList = new ArrayList(16);
while (rs.next()) {
valueList.add(new OptionItem(rs.getString(1), rs.getString(2));
}%>]]>
<html:select styleId="mailerName" property="mailerName">
<option value="--select--" onchange="updateResult(this);">--select--</option>
<html:optionsCollection name="valueList" value="id" label="label"/>
</html:select>
<script type="text/javascript">
function updateResult(ctrl) {
var txt = document.getElementById('result');
txt.value = ctrl.options[ctrl.selectedIndex].text;
}
</script>
几个笔记:
这篇关于jsp strut的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!