How to prevent reset in a Struts form? - java

I have this struts form:
<s:form ...>
<input id="foo" value="123"/>
</s:form>
I change the value of foo then I hit back button. When I come back , foo has lost my value and now is 123.
How can I avoid this ?

This is less of Strust's concern but more of browser's decision.
Check out the following related discussions:
Browser back button restores empty fields
Clear Form on Back Button? (doing the opposite)

Related

Disable a checkbox in spring and pass the checked value to controller

Hello I have to disable checkbox in spring html form and pass the checked value to controller. I use the below code, but the problem is that I am not able to pass the checked value to controller when the disabled attribute is set to true
<form:checkbox path="agreementCats" disabled="true" value="${x.value}" class="category-checkboxes" id="agreementCat-cb-${i.index}" /><label class="control-label" for="agreementCat-cb-${i.index}"> ${x.label}</label>
<form:hidden path="agreementCats" value="${x.value}" />
The above code sends all the values to controller. Please help!!
in HTML have two tag with similar meaning but different context is Disabled and ReadOnly.
disabled: Disabled the input in the form and the data isn't in form query.
readonly: Omit the user UI interaction but the input is in form query.
Try use readonly tag instead disabled, I hope this help you.
That solution works for all inputs except checkbox... Checkbox can't use readonly, but you can do a little trick, is a dirt trick, but it can you help to not permit user touch check box.
If you know in server side what checkbox can be edited, try this code:
<input type="checkbox" <c:if test="condition">onclick="return false;"</c:if> ></input>
That code not permit user click the checkbox if the condition is true. Maybe with this can solved your problem.
Instead of disabled="true" change it to readonly= "true"
Disabled doesn't send a value if you insert but readonly does.

HTML button/input I want to keep different text and value and read value in server

I tried googling this but didn't get a proper answer, so here goes the question to the experts on this forum:
I want to render a button on my web page such that it's value is different from what is displayed on screen. e.g if I use below html tag, Confirm is displayed.
<button name="type" value="save">Confirm</button>
However I want to read value "save" in the server so that I can have certain logic based on that. If I use
request.getParameter("type");
I get "Confirm" instead of "save".
My objective is to have multiple buttons with different values and at server side I want to know what button was clicked. I don't want to link the server code with the displayed text.
I can do a workaround such that onsubmit I call a javascript function that captures the button clicked and puts "save" in hidden field with name="type". But this seems like such a common client-server problem that there must be a more elegant solution to this problem that I am not aware of.
Appreciate any help.
Can you post more of your code? Both the HTML form and the server side code? The behavior you are expecting IS in fact the expected behavior for the code you have shown. I suspect you have a bug elsewhere in your code (like maybe reusing the variable name somewhere).
It would make sense that <button> works such that value attribute is sent in POST request whereas the displayed text is whatever is between <button>...</button>.
Unfortunately, standards don't define this behavior, so the browsers are left to implement it the way they want. In Firefox, value attribute is posted. But in Internet Explorer (I know about IE8, don't know if it changed in later version), value is not posted. Instead displayed text is posted!
In a nutshell you have to workaround this problem. There are a couple of good suggestions posted here.
This is what I found most useful or worked for me the most. Add a hidden field with the same name as I intend to use in the server.
<input type="hidden" name="type" id="btntype" />
<button type="submit" name="submitbtn" value="save">Confirm</button>
<button type="submit" name="submitbtn" value="verify">Verify</button>
<button type="submit" name="submitbtn" vallue="cancel">Cancel</button>
And this in Javascript:
$(document).ready(function() {
$(":submit[name='submitbtn']").on("click", function() {
$("#btntype").val($(this).val());
});
});
Now, in the servlet, I can use:
request.getParameter("type");
It will give value save if Confirm is clicked, verify if Verify is clicked and cancel if Cancel button is clicked.
This way also it will work. Just define the id for button.Whenever that particular submit button will be clicked just call .on() method
<button id="filter" value="value1" type="submit">Confirm</button>
<button id="filter" value="value2" type="submit">Verify</button>
<button id="filter" value="value3" type="submit">Cancel</button>
$(window).on("click", "#filter", function() {
alert($(this).val());
});

How to use Struts 2 push tag for form input fields?

I have two classes one is Action class MyAction and other is POJO class User. Inside MyAction, i have made getter-setter for User class.
Then I have index.jsp page for user input. In this, I am using struts 2 push tag for the properties stored in User class. It looks something like this:
<s:form action="go" method="post">
<s:push value="user">
<s:textfield name="id" label="usr-id"/> <!-- user.id -->
<s:textfield name="fname" label="first-name"/> <!-- user.fname -->
</s:push>
<s:submit/>
<s:token/>
</s:form>
But I am getting an error. If I remove the push tag and prefix the User properties with user, then it runs fine. Can any one guide me, where I am wrong and how to use push tag for input fields in form.
No matter if you use <s:push>, <s:set/>, <s:iterator> with its var or IteratorStatus attributes... :
to show a value, any way is a good way;
to send a value, the only way is specifying the full "path" in the name attribute.
For example, you can use the pushed object in the value attribute, but to make it work when submitting, you still need to put the user in name attribute:
<s:push value="user">
<s:textfield name="user.id" value="id" /> <!-- user.id -->
<s:textfield name="user.fname" value="fname"/> <!-- user.fname -->
</s:push>
This makes the use of <s:push> in your case totally useless.
But in an use-case where source and destination differs, eg. you read user.fname from ActionSource, and send its value to ActionDestination in a selectedFname String, the jsp would be
<s:push value="user">
<s:textfield name="selectedId" value="id" /> <!-- user.id -->
<s:textfield name="selectedFname" value="fname"/> <!-- user.fname -->
</s:push>
So it would have done "something usefull".
But basing on my experience, you won't pretty much never use push. If you need it, your data structure is probably too complex.
Your code looks like ok, but to send values from textfields you need to push the user object to the stack again. Better do it with some interceptor before the params interceptor populates the action. The same thing is doing modelDriven interceptor.
The Model-Driven interceptor watches for ModelDriven actions and adds the action's model on top of the value stack.
Note: The ModelDrivenInterceptor must come before the both StaticParametersInterceptor and ParametersInterceptor if you want the parameters to be applied to the model.
Note: The ModelDrivenInterceptor will only push the model into the stack when the model is not null, else it will be ignored.
You can use model driven approach it's pushing a model for the view and for the controller. The last is missing from your code.
The example of using ModelDriven approach.
Push is not a ui tag. Push is used for put objects into top of value stack. If your object is not in top of stack you get your values by using object.attributname. If your object is in the value stack, you can access it directly attributename.

Struts 2: Sending values of form fields from jsp to action class

I know it must be simple, but still I am not able to figure it out.
I have a link on a jsp page.
When this link is clicked I want another tab (of browser) to open up.
The view of this new page is defined by action class, which needs a form field value.
The problem I am facing is that I can not get the values of form fields to the action class without submitting the form. And submitting the form changes the view of the original jsp as well, which defeats the whole purpose of opening a new tab.
So I need a way to get the form field values to action class without resetting the view of original jsp page.
One way I came across was URL re-writing but that would be my last option.
Please suggest something!!
Thanks!!
Firstly I would like to point out that currently possible (to my knowledge anyway) to force a new tab to appear, it is dependent on the users' browser and the settings that they have see here for more infomation.
Now onto your question, since links cannot send form data (on their own) you have 2 options:
You can use a form "submit" button pointing to the URL you want to send the data to and to and add the target="_blank" to the form which will cause a new page to open up when the form is submitted.
You can add a javascript event to your link so that when it is pressed you append the value of the input to the URL and open a new window with that URL.
Personally I would choose the first option.
Here is a simple example of option one which doesn't remove the input value when you submit...
<html>
<body>
<form action="test1.html" method="post" target="_blank">
<input type="text" name="bob" />
<input type="submit" value="Hello"/>
</form>
</body>
</html>
You could do an ajax call, or dynamically build the link url with get parameters in it.

How to use RichFaces a4j:commandButton not using submit

I have an a4j:commandButton which looks like this
<a4j:commandButton id="stopBtn" type="button" reRender="lastOp"
action="#{MyBacking.stop}" value="Stop" />
</a4j:commandButton>
When the app is deployed, and the button clicked, the stop() method is not being called. All the a4j:commandButton examples refer to forms, but this button is not in a form - it's a button the user is going to use to cause the server to run some back-end logic. At the moment, the method is
public void stopNode() {
logger.info("STOPPING");
setLastOp("Stopped.");
}
Other methods which don't use this type of button are updating the lastOp field, but I'm not seeing anything on the console with this one. Am I right to cast this as a button? Should I put this in a h:form tag?
The firebug console says:
this._form is null
which I don't understand.
Any help well appreciated.
UICommand components ought to be placed inside an UIForm component. So, your guess
Should I put this in a h:form tag?
is entirely correct :) This because they fire a POST request and the only (normal) way for that is using a HTML <form> element whose method attribute is set to "post". Firebug also says that a parent form element is been expected, but it resolved to null and thus no actions can be taken place.
Only "plain vanilla" links like h:outputLink and consorts doesn't need a form, because they just fires a GET request.
Yes, wrap it in a form. I'm sure BalusC will post a detailed explanation while I'm typing my answer. (yup, there it is)
I have to ask why you didn't just try a form first, before posting here.
Look at your code:
<a4j:commandButton id="stopBtn" type="button" reRender="lastOp" action="#{MyBacking.stop}" value="Stop" />
You finished <a4j:commandButton with />, why need that orphan </a4j:commandButton> ?
If for some reason you don't want to place the button inside a form, you can do something like this:
<a4j:commandButton onclick="fireAjax()"/>
<h:form>
<a4j:jsFunction name="fireAjax" action=".."/>
</h:form>

Categories

Resources