精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
I am creating a page in jsp, using struts, and I am totally abeginner in these.
我用JSP创建页面,也用struts,我完全是小白。
In my jsp page I have.
在我的jsp页面我有如下代码。
<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">
each time an option clicked in select tag, it need to display a text in.
在选择标签点击时,对应的选项需要显示文本。
<html:text property="result"/>
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.
我需要的是当我点击一个选项,它需要执行Java功能,功能包含带可选值和结果文本的DB查询,结果文本会显示到文本字段里。
How can I do this?
我怎样才能做到这一点?
It's not very clear whether the value to be displayed in the textbox is coming from which query.
这不是很清楚,要显示在在文本框的值中从哪个查询生成。
You can use a LEFT JOIN between m_user and whatever the table from which you want to display the value in textbox. 您可以使用LEFT从中要显示在文本框中的值m_user和任何的表之间的连接。
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>
Few Notes:
Never use scriplets (i.e. Java code between <% & %>
切勿使用脚本<%%> (即Java代码)
Always use JNDI data source for obtaining resources such as Database Connection
始终使用JNDI数据源获取资源,如数据库连接
Use Action class to fetch the data to be shown in View, never put that logic in view
使用Action类获取数据,接着在视图中显示数据,不要把这个逻辑放到视图中
Have a look at following tutorials: 看一看以下教程:
Struts HTML Select Tag Tutorial :Struts的HTML选择标记教程
Mastering Struts - Select Tag :掌握Struts的 - 选择标记
Struts HTML Tag :Struts的HTML标签