简体   繁体   English

如何修改我的页面并将我选择的选项传递给 java bean

[英]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.

相关问题 如何使用Java和Selenium为我的驱动程序传递无头选项? - How to pass a headless option for my driver using Java and Selenium? 如何将java bean传递到jsp页面以便jqQrid使用json显示? - how to pass java beans to jsp page for jqQrid to display using json? 如何在我的 Java 代码中添加 Yes 或 No 选项 - How to add Yes or No option in my java code Java-修改我的To String方法 - Java -Modify my To String method 标准库是 Java 加载/读取和编辑/修改和保存 html 文件而不重新格式化的最佳选择吗? - Is the standard library my best option for Java to load/read and edit/modify and save a html file with no reformatting? 如何在JSF中按名称获取我的托管bean? - How to get my managed beans by name in JSF? 如何修改我的 Android 项目中的 BuildConfig.java? - How to modify BuildConfig.java on my Android project? 如何在Java中修改此星形模式代码以获得所需的输出? - How to modify this star pattern code in java to get my desired output? 如何避免在Java Bean中使用Spring注释,而仅在配置类中使用它们? - How to avoid the use of Spring annotations in my java beans and use them only in the configuration class? Java 我如何将随机整数传入我的开关 java - Java how do i pass in the random int into my switch java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM