I am using JAVA STRUTS. I have an html form with an ActionForm class associated with it and the controls are created using tags. As I understand it, STRUTS will persist selections on an html form even if the form is refreshed. I need to prevent this behavior and force it to rebind the values from the ActionForm object.
Basically, I have a form with a radio button group that allows the user to select A, B, or C. I have an ActionForm that has the property "selection = A" set when the form is loaded. If the user selects B and submits it, the form is correctly set to "selection = B" and all is good. However, when I refresh the page, the ActionForm gets reset to "selection = A" (confirmed through debugging), but the radio buttons have B selected!
Does anyone know why this is or how I can prevent it from happening?
Regarding to your post i thought it was belongs to struts2 so,
According to struts2 it is not possible means, you have to give the static value or you have to make in programmatic manner through JPA (or)your java code.
For JPA you refer this link it is helpful JPA way
This explanation is refer to static way
First in jsp page you have to import struts2 tags through taglib uri then it shows the available struts2 tags in those tags you have to select <s:radio></s:radio> tag
eg:
<s:radio label="Selections" name="your name" list="#{'A':'A','B':'B'}" value="2" />
in this code i typed the value as 2 so, it select B as a default if you remove the value it won't select any default values.
Basically what i am trying to say here is without value it won't select any radio buttons
you have to give value through static way or dynamic way.
For dynamic way you can refer this link dynamic radio button
Related
Is it possible to set a variable in thymeleaf and then change it depending on an option in a select element and then display a specific div based on the selection? Here's what I mean.
I have three options in select:
1) DESKTOP_PC
2) LAPTOP_PC
3) TABLET_PC
When the user selects DESKTOP_PC, I would like to show the div with the related inputs for it. Likewise if it's LAPTOP_PC or TABLET_PC.
Only problem is, I'm not sure how I would go about doing this.
The other thing, is that I have the following:
In my entry class I have an enum class:
public static enum Type {
DESKTOP_PC,
LAPTOP_PC,
TABLET_PC
}
In my Thymeleaf form, I have the following to access and display this enum:
<div class="section-question">
<p>Type</p>
<select name="type" th:field="$*{type}">
<option th:each="type : ${T(com.project.entities.Entry.Type).values()}" th:value="${type}" th:text="${type}" ></option>
</select>
</div>
Any help would be much appreciated!
Thanks
it is very possible, but has very little to do with Java or Thymeleaf, even though you could do it with them too (not recommended)
All you need to do is create basic JavaScript function that onClick to different options makes different divs visible (through adding/removing classes with css defined in them like display: none;).
Of course you could implement it on backend, through each click being a separate request sent to a controller that then saves the option into a for example boolean variable and based on this variable returns a different thymeleaf view but that is an awkward looking solution for such simple case that doesn't really require backend processing.
Any simple book on JS will show you how to do this, I can recommend A smarter way to learn JS for this scenario
To achieve what you want we need to to take help of Javascript or jQuery.
Write an onchange function on select tag.
Get the value of option selected inside the function.
Show/hide div depending on value
Vote if it helps you
There are check-boxes in a jquery datatable. I want to submit them to a servlet by using submit button. I have defined form tags inside the table but still after formatting all the hidden type elements are passed and checkboxes in the table are not passed to the servlet. It shows null for check-box name reference.
Could anybody please help me to pass those values to java servlet.
It seems the DataTable plugin does not use <input> tags for checkboxes. Have a look at this example in the documentation:
https://datatables.net/extensions/select/examples/initialisation/checkbox.html
It says there:
The checkbox is not an element, but rather a
CSS that uses the :before and :after pseudo elements of the cell to
draw a box and the tick.
So first make sure you understand what elements there really are in your table. Use your browser's "Inspect" tool.
Then, if you are sure what elements are there and if there are really checkboxes, be aware that your servlet will not receive a value of a checkbox that is unchecked. This is very unlucky, but it is the specification:
https://www.w3.org/TR/html401/interact/forms.html#checkbox
Therefore, your servlet must internally initialize its final checkbox value with false. Only it receives a value from the checkbox, the final checkbox value will become true. So it might be the right thing that you receive null - then the checkbox was simply not checked.
I have a JavaScript code embedded in a Java method to retrieve properties of any element clicked upon in a web page.
That is, I navigate to a specific URL by creating an instance of Selenium WebDriver from my Java application and click on an object in the page that gets loaded. After the single click on the object, a HTML page opens listing the name, type, id, class name etc. associated to that object as a radio button group. I select one option from the list and click on OK. What I wanted to know is, can I push the selected data from the HTML back onto my Java application on the click event on the HTML?
Any help appreciated! Thanks! :)
WebElement interface of selenium Webdriver gives a method
getAttribute which provides value of a given attribute of the desired html element.
I hope this method is what you are looking for.
USAGE:
String field_value;
field_value = driver.findElement(By.xpath("xpath Locator")).getAttribute("attribute name")
The information which you want to get should be in any of the attribute associated to that element.
Similarly if you want all attributes associated with the element in one go you can use something like this :
((JavascriptExecutor)driver).executeScript("return arguments[0].attributes);", element);
The above will give you a comma separated list of all attributes on which you can apply your coding logic to separate them and get the corresponding values using loops.
I have the following mask/view:
After entering the name and description, I can add a list of existing attributes by clicking on "Add existing" button.
This opens another mask/view, where I can select from a list of existing attibutes. This (new) mask/view has it's own controller and model too.
When I'm finished with the selection, I have another button that bring me back to this mask.
The problem is that when I come back the name and description fields are empty.
This happens because when I click the "Add existing button" the controller/model of this mask is not called and the values are not stored.
How can avoid this and have the values stored?
Thank you for any help
Francesco
This only works if the new view is inside the same view. E.g. when it's an overlay dialog which is presented by a hidden <div> and not a plain window.open() popup window. This way the beans will have access to each other and you will be able to execute ajax updates instead of submitting/refreshing the entire page.
This is pretty complex to homegrow if you're not that familiar with HTML/CSS/JS. I suggest to look at a component library which offers such a component like RichFaces' <rich:popupPanel> and PrimeFaces' <p:dialog>.
You can store data in session or conversation bean.
Session bean approach have huge drawback when user works in multiple tabs/windows - he uses only one session bean, so he overwrites what he did in other windows.
Conversation scoped bean solves this. But conversation scope was not in javaEE 5, its in CDI (part of EE6).
i'm using struts (form) + validation
i just want to ask if can i set focus on some field on form after validation without javascript? i think i red something in book programming jakarta struts but i can't remember.
thanks
You cannot set focus on a certain field with pure HTML. The tabindex idea as suggested by Bozho is nice, but it will only work if you actually press tab for the first time. It has however the disadvantage that it changes the tabbing order of the input elements. Not really user friendly.
You'll really need to grab JavaScript for this. Just do something like:
window.onload = function() {
document.formname.${inputname}.focus();
// or:
document.getElementById(${inputid}).focus();
};
...where ${inputname} dynamically resolves to name of the input field as in <input name="foo"> and where ${inputid} resolves to ID of input field as in <input id="foo">.
That's all.
You can set the tabindex="1" attribute of the input which you want to have obtain the focus first, when the page reloads.
You can't set the focus on a field without using JavaScript. Others have tried and failed (CSS was the first place they looked at, but that doesn't cut it either).
Not sure what you've read in the book Jakarta Struts, but maybe you are referring to the focus attribute of the Struts <html:form> tag? That sets the focus on the desired form field without you needing to add JavaScript. But Struts will add the JavaScript, so that means no JavaScript from your side, and not no JavaScript at all.