I've got forms that I display on almost every pages of a website (a login form, for example), so I don't use an action for those. The "post" of those forms just goes to an action and if there is a validation error, it displays another "central" page. The only thing that doesn't seems to work in the form page is displaying a message in different languages using the tag <s:text>
It always use the default locale defined in struts.properties. I guess it's because I don't call any action before displaying the form.
Anyway, I have 2 files : struts_fr.properties and struts_en.properties
In struts.properties I have
struts.locale = fr_CA
And I use something like :
<s:text name="login.label.username" />
The message displayed is always the one from the "struts_fr.properties".
The question is : How can I set the locale for struts, before using the s:text tag, when there is no action called?
Well it seems that it worked "alone" after a while... maybe the work directory was in cause here. We also have some custom interceptor so maybe that's why it worked.
Related
I am in bit of a delicate situation here. In my organization we design stock management systems and it is a web application based on JSP pages and servlets which handles them.
I have been asked to fix a specific problem. We have a JSP page with an HTML form table where there are stock details. When user enters the details manually and submit the form, stock details updated in the database and it works fine.
Problem is this : When the user press the browser's back button, user can come to the previous page where he submitted the details. And when the user submit this, data is saved once more to the database.I need to prevent this behaviour.(Something likeclear and reload the page.)
Things I did so far : clear the browser cache.Code works fine but not the expected result.
Unfortunately I cannot share the code due to company regulations. What I need is a help to prevent this behaviour or a workaround.
Thanks in advance..
You can use a javascript function with the help of a hidden attribute to reload the web page. When the user press the back button,based on the value of the hidden attribute, page will be reloaded without loading the cached page.
Your approach of clearing cache is correct. Coupled with that, you can use this approach.
<input type="hidden" id="refreshed" value="no">
<script type="text/javascript">
onload=function(){
var e=document.getElementById("refreshed");
if(e.value=="no")e.value="yes";
else{e.value="no";location.reload();}
}
</script>
One drawback of this approach is if your clients' browsers have disabled JS, this will not work.Otherwise it should work.
When the user press the browser's back button, user can come to the
previous page where he submitted the details. And when the user submit
this, data is saved once more to the database.
According to how you described it, that is based on a doGet request. Which means every time you visit that URL, it will send the request with whatever parameters were added.
As someone already mentioned, if you switch the form to a post method and switch the Servlet to a doPost, you won't have this issue anymore.
Alternatively you can circumvent this with a javascript solution. Here are some options:
You can check if the user clicked the back button, disable form if true.
Another way is by storing a cookie which you check on page load, if it exists you can disable the form.
You can use this code also
$(document).ready(function() {
function disableBack() { window.history.forward() }
window.onload = disableBack();
window.onpageshow = function(evt) { if (evt.persisted) disableBack() }
});
You must use a Post-Redirect-Get pattern: https://en.m.wikipedia.org/wiki/Post/Redirect/Get.
Actually, every use of standard HTML forms with method="post" should be implemented with that pattern. It doesn't have any use for AJAX-posted forms, which actually could be another solution but will require more work and probably some architectural changes.
I had this same problem while building a django web app, and my solution was to not allow caching of the html that contains the form. In your request handler, do not allow the browser to cache the page. This will force the browser to get the page fresh from the document.
Which, in this case, you can just verify in your request handler if the requested form has already been submitted.
My code for reference:
from django.views.decorators.cache import never_cache
#never_cache
def GetForm(request, pk):
# Logic #
if (IsFormCompleted(pk)):
# Handle request #
Here is a solution.
give a random id in a hidden field on the form. Then on the server side, if the user resubmit, check if the random id already on the database. If so, redirect user.
Currently, i am dealing with rich:tabPanel in my web application based on JSF 2.0.
I encounter a very strange problem, which is related to my richface component.
Basically, i print some same stuff on each panel (here, it is a schedule table of a show, tab contains the day and content of tab contains the differents hours ).
Consequently, i have something like that:
<rich:tabPanel>
<c:forEach items="#{show}" var="hour" ...>
<rich:tab>
<a4j:commandLink ...>
<a4j:param value="hour.something" assignTo="#{bean.method}" />
</a4j:commandLink>
</ ..... >
When i click on the first commandlink, when my webpage is displayed, it's ok. But when i choose an other tab, and i click on the commandlink, the "bean.method" is not call. I need to click a second time to make the call of the function.
Finally, when i put the tabPanel as "switchtype=server", it works very well (without clicking 2 times).
But that's not the purpose, i want to use the client mode.
I see that on JIRA of richfaces v3, this problem has been solved JIRA JBoss. But there is no more information (except a comment but it's not working).
If anyone can help, it would be great.
Regards,
The problem is you are using nested forms(form within form). This is not recommended in JSF. Even in HTML it is invalid. Remove one form and it will work.
Read this post too.
After a customer is successfully added in my JSF application on the page add_customer.jsf, the user is redirected to the page list_customers.jsf.
On this page I would like a success message to be shown. The message should only be shown if the user is really coming from the add_customer.jsf page.
How can I do this?
One approach is to add a message to The Flash in the action method that adds the customer.
You don't need to explicitly check that the user was coming from add_customer.jsf, if it's that page (or actually the code behind it) that sets the message. On the destination page you only need to have an <h:messages> component.
A co-worker of my posted an example CRUD app that does exactly this at: Sample CRUD application with JSF.
The relevant code is in the backing bean UserEdit and the page where the redirect happens to.
Note that if your JSF implementation is Mojarra, The Flash only works between pages in the same directory. If this bothers you, please consider voting for this issue: http://java.net/jira/browse/JAVASERVERFACES-2136
The best way to do it would be using a <h:message> tag with which you could bind a message which can be populated before you redirect to list_customer.jsf.
We have been using this approach for showing messages in our application from last 3-4 years and occasionally used partial triggers to refresh on the same page.
In the base template for the pages of my Wicket application, there's a form I don't want Wicket to handle, like this:
<form id="myForm" action="">
<!-- input fields and submit button -->
</form>
I left the action attribute empty to always send it to the current page. On the application's main page, it works, but on other pages, Wicket adds a "../" in the action attribute, which seems to be meant well but is not what I want.
I'm using Wicket 1.4.17. How can I stop or change this behaviour?
The form is meant to enable the user to submit a short message as feedback to the site admin. It appears on every page and the input is collected from the PageParameters in the constructor of my pages' base class. If there is a more Wicket way to do this, I'll appreciate hints, but this should be a) stateless and b) very very simple.
I'd go the Wicket way and write a component for your feedback form which is then inserted into every page. As you have an (abstract) base class for all of your pages, you can simply add it there and it will appear on every page.
In your feedback form component, simply overwrite the onSubmit() method and send the message to the site admin.
i'm doing a JSF2 project.
1 . i have a form with some checkbox. Look the following code
<ui:repeat>
<h:selectManyCheckbox value="#{wagent.selectedPra}">...
</h:selectManyCheckbox>
</ui:repeat>
So i use selectManyCheckbox inside ui:repeat and i want that the value of the different selectManyCheckbox point to the same value.
But when the form is submit i didn't have all the selected boxs.
How i can do that ?
2 . I have a form with some inputs. On my action i want to merge some pdf files, stay on the same page and that a pop-up appear to offer to download the merged files.
3 . Does all the managed beans with request scope are created for each request or just if i used them in the xhtml page?
4 . I have a commandlink to logout. On my action i use session.invalidate() and return "login". So i go back to the login, but when validating the login, my session managed bean doesn't seem to be created. Error is something like yourSessionBean is null. What's wrong ?
1: So i use selectManyCheckbox inside ui:repeat and i want that the value of the different selectManyCheckbox point to the same value. But when the form is submit i didn't have all the selected boxs. How i can do that ?
Let them point to a different value instead. With the given example the bean value will be overridden everytime until end of the loop. As an example, use a List<List<Pra>> in a bean (or whatever Pra means in your question):
<ui:repeat value="#{wagent.allSelectedPra}" var="selectedPra">
<h:selectManyCheckbox value="#{selectedPra}">
...
</h:selectManyCheckbox>
</ui:repeat>
2: I have a form with some inputs. On my action i want to merge some pdf files, stay on the same page and that a pop-up appear to offer to download the merged files.
At least two things needs to be done:
facesContext.getExternalContext().addResponseHeader("Content-Disposition", "attachment;filename=name.pdf"); // Force "Save As" dialogue.
facesContext.responseComplete(); // Prevent JSF from taking response in hands.
3: Does all the managed beans with request scope are created for each request or just if i used them in the xhtml page?
They are created for every HTTP request. The scope which you described only applies on view scope (if I understand you right).
4: I have a commandlink to logout. On my action i use session.invalidate() and return "login". So i go back to the login, but when validating the login, my session managed bean doesn't seem to be created. Error is something like yourSessionBean is null. What's wrong ?
You are probably accessing the session scoped managed bean the wrong way. You need to either inject it as #ManagedProperty or to grab it by Application#evaluateExpressionGet().