[英]how to modify my page and pass my selected option to java beans
i am using html code to display my page depending on the selected option i click on.我正在使用 html 代码来显示我的页面,具体取决于我单击的所选选项。 But now i want to pass my selected choice to java beans too.
但现在我也想将我选择的选项传递给 java bean。
my 4 options我的 4 个选项
<tr>
<td class="title">Category:</td>
<td class="field">
<select name="list" onchange="display(this,'truck','car');">
<option>Please select:</option>
<option value="invisible">Bike</option>
<option value="invisible">Quad</option>
<option value="car">Car</option>
<option value="truck">Truck</option>
</select>
</td>
</tr>
Script i am using我正在使用的脚本
<script type="text/javascript">
function display(obj,id1,id2) {
txt = obj.options[obj.selectedIndex].value;
document.getElementById(id1).style.display = 'none';
document.getElementById(id2).style.display = 'none';
if ( txt.match(id1) ) {
document.getElementById(id1).style.display = 'block';
}
if ( txt.match(id2) ) {
document.getElementById(id2).style.display = 'block';
}
}
</script>
display function is modifying my page doing the following:显示 function 正在修改我的页面,执行以下操作:
<tbody id="car" style="display: none;">
and和
<tbody id="truck" style="display: none;">
Now, how do i pass my selected options to java beans?现在,我如何将我选择的选项传递给 java bean?
Use a JSF component instead of a plain HTML element.使用 JSF 组件代替普通的 HTML 元素。 JSF components allows you to bind the value to a bean property.
JSF 组件允许您将值绑定到 bean 属性。 The JSF component representing a HTML
<select>
element is the <h:selectOneMenu>
wherein you can specify the options using <f:selectItem>
or <f:selectItems>
.代表 HTML
<select>
元素的 JSF 组件是<h:selectOneMenu>
,您可以在其中使用<f:selectItem>
或<f:selectItems>
指定选项。
Eg例如
<h:selectOneMenu id="list" value="#{bean.vehicle}" onchange="display(this,'truck','car');">
<f:selectItem itemValue="#{null}" itemLabel="Please select:" />
<f:selectItem itemValue="invisible" itemLabel="Bike" />
<f:selectItem itemValue="invisible" itemLabel="Quad" />
<f:selectItem itemValue="car" itemLabel="Car" />
<f:selectItem itemValue="truck" itemLabel="Truck" />
</h:selectOneMenu>
with和
private String vehicle;
// Getter+setter.
If you're using JSF 2.x, then you can also just use <f:ajax>
instead of all that JavaScript boilerplate.如果您使用的是 JSF 2.x,那么您也可以只使用
<f:ajax>
而不是所有 JavaScript 样板。 I'd suggest to invest some time in going through some decent JSF tutorials, it look like you're totally missing the point of JSF by writing all that HTML/JS manually.我建议花一些时间阅读一些体面的 JSF 教程,看起来你完全错过了 JSF 手动编写所有 HTML/JS 的要点。 Or if you intend to have full control over HTML/JS, better look for an action based MVC framework such as Spring MVC instead of a component based MVC framework such as JSF.
或者,如果您打算完全控制 HTML/JS,最好寻找基于动作的 MVC 框架,例如 Spring MVC,而不是基于组件的 MVC 框架,例如 JSF。
Use AJAX.使用 AJAX。 That allows you to send data to the server without changing anything on the web page.
这使您可以在不更改 web 页面上的任何内容的情况下将数据发送到服务器。
Try a framework like jQuery which offers AJAX ( docs ) and a lot of other cool stuff to build HTML5 applications.尝试像jQuery这样的框架,它提供 AJAX(文档)和许多其他很酷的东西来构建 HTML5 应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.