I have a page whereby I am including a freemarker FTL in this way:
<#include "header.ftl">
Now, the page that it is in does not reload as it's a one pager. However, I would like that at a certain point I refresh header.ftl without doing a full page refresh. Is this possible by any chance? I am new to FTL.
You'll need two serverside endpoints, one for the whole page and another for the fragment(s).
You can then use an ajax request to refresh the fragment (eg jquery)
Related
I have a search.jsp page that has some html content and a form. When the form is submitted, there is a servlet handle the form data and forward the results to the search.jsp page. However, the url in the browser after processing the form is changed to the servlet name:
http://localhost:8080/MyProject/SearchServlet
not the search.jsp page:
http://localhost:8080/MyProject/Search.jsp
How I can change the url to the search.jsp? In other words, I just want to refresh the search.jsp page to display the results in the same page. How I can do that?
You cannot do that by forwarding the request: you need to "tell" the browser to generate a new http request by using the response.sendRedirect() method.
Now the question is why do you want the url bar to display the name of the Jsp?
Hiding the real destination path is a desired feature when forwarding requests: users do not need to know the server side redirects (that's how they are also called) happening in your web app.
Think about it: to carry out its tasks a servlet potentially can forward the request a number of times before getting to the final destination: you don't want the url bar to change each and every single time.
Give a fancier name to your servlet like: "Search" rather than "SearchServlet" so that users will know they are on the search page of your web application and not in the "SearchServlet" page.
In addition to that, if you visit any professional website, you will hardly ever see the .jsp or .html or .php extension on the address bar. While that is not a requirement or specification and you are free to do so, I believe the first approach is best practice (it looks even better to me honestly). There is even a folder WEB-INF whose purpose is to hide your .jsp pages from direct access via url bar.
What I like doing is having a servlet as the landing-welcome page of the web app, that will be responsible to forward and redirect requests based on the user input and the inner working of the application.
Now back to your final request (pun intended)
"In other words, I just want to refresh the search.jsp page to display the results in the same page. How I can do that?"
What I would do is:
redirect the user to the "Search" servlet from the welcome/home servlet.
In the doGet method of the Search servlet I would forward the request to the search.jsp page (you could set attributes before forwarding if you need to).
In the search.jsp I would set the action attribute of the form to "Search" (the name of the servlet) and the method to POST.
In the doPost method of the Search servlet you would implement whatever logic you wish to implement and finally forward the request to the search.jsp
After hitting the search button (and even after the submit button is clicked) what the user will see on the address bar is simply
http://localhost:8080/MyProject/Search
Hope that makes sense.
Are you using the same search.jsp for searching and well as showing the result? It is possible to use the same jsp to perform both the functions but it's easier and desirable to make another jsp which will only show the results.
If you are not able to see the results on search.jsp then make sure that you are setting the proper response in the Servlet class before forwarding it to the jsp and also whether you are reading the response sent by the Servlet class properly in the jsp.
If you want, the page to not refresh at all, then go for AJAX.
I have been going crazy about resetting some validation error on one of my jsp pages. This is a project inherited from people I cannot reach anymore (dead or unavailable). I have a jsp page with lots of custom taglibs where further pages are added as tabs and the parent page has action buttons to open things like forms. There is a validation error and some configuration parameter being set/modified both in the tabs section and the parent page. But the interesting thing is that I can see the heavy use of pageContext.setAttribute(), session.setAttribute() and pageContext.getSession().setAttribute(). If my initial knowledge is right, pageContext is quite heavily used in servlet-based implementations. but how different things would be if I use those following three on my JSp pages to set attributes?
** Example Scenario (my problem): **
I have some attributes set in the parent page, which are also being set/modifed in the tabs page (embedded in the parent page). I want to remove them such that if there is a validation error, I will simply remove what I have in the tabs page i.e. next time the page will simply load those attributes from the parent page. Do I use pageContext.setAttribute() in the tabs page, but use pageContext.getSession().setAttribute() in the parent page?
KR,
Page scope
When we put in our JSP page, scope is available only for the JSP page that put it.
This is the default scope, so is the same to call pageContext.setAttribute("", "", PageContext.PAGE_SCOPE); same as pageContext.setAttribute("", "");
Session scope
session.setAttribute() and pageContext.getSession().setAttribute() both are same.
What you put on your session scope is available across all requests on the same user session.
Is the same to call pageContext.setAttribute("", "", PageContext.SESSION_SCOPE); same as session.setAttribute("", "");
I have a web application, which is an implementation of the Front Controller Pattern, as described by the image below. Specifically, I have followed the code example of the Front Controller Pattern, as described in this question's answer:
Design Patterns web based applications
I'm having updating issues with one of my .jsp pages. This page in particular has a popup-editor, which allows the user to add or remove links to sets of data from an external database. If the user hits a save-button, it sends a request, which is intercepted by the Front Controller. Then, the correct action for saving or deleting a reference link (many to many ID numbers) is executed on the systems own database. After, the page should be reloaded, which triggers a chained command, which loads data from both databases, before redirecting to essentially the same page, without the popup.
However, the page is not reloaded, due to some form of caching that I do not understand. I've tried every solution I could find, to prevent loading cached data, from adding a unique value to the URL, to adding meta-tags on the jsp-page, to prevent it from caching at all. I've confirmed that the data is loading on server side. A simple F5-click updates the page like it should be doing automatically.
I've also implemented an onClose-function on the popup, that calls a window.location.reload(). However this call interrupts the Front Controller, and it stops everything it is currently doing, to handle the newest request. This creates a race condition, where the data is sometimes able to be inserted/deleted, and sometimes not, before the Front Controller starts reloading the page.
Should I make a queue?
Is there a good reason not to do it this way?
Should the Front controller's execution method, or the class itself implement some kind of Synchronization?
Your popup-editor should be placed inside an iframe so that it will not reload the window when it submits. The response that returns from request sent by the iframe should trigger the window.location.reload(). For example:
<html>
<script type='text/javascript'>
window.onload = function() {
window.location.reload();
};
</script>
</html>
Another option is to send the update with an Ajax call and then reload the page when the Ajax call returns. Or nicer, update the page dynamically without a reload but with JSON data returned by the Ajax call.
I have a single jsp page, mypage.jsp . this has 2 tabs- firsttab and secondtab. Each of these tabs have their own forms.
Each tab has to handled by separate controller, say firsttab is handled by firsttabcontroller and secondtab is handled by secondtabcontroller.
Now if i submit a form in firsttab there may be chance that content in secondtab is altered.
and the same effects can be expected with changes in secondtab affecting firsttab content.
Everytime i make a request to any controller the very same page- mypage.jsp should be rendered.
so the problem is handling multiple forms/tabs mapped to different controllers from the same view.
How to achieve this?
I looked into some similar questions but none of them gave me satisfactory answers.
I would probably do this using AJAX. You can populate the tabs with HTML requested from the two different controller URLs.
This might look like the following where the tab you're populating has a div with id mytab.
$.get('<spring:url value='/myurl'/>', function(data) {
$('#mytab').html(data);
});
When you submit the form in tab 1 you just need to trigger a GET to the controller which populates tab 2 to refresh the contents.
Is there any way I can redirect to a different page from a Spring Controller that gets called from a JSP using <c:import>?
Scenario is as follows: I have a Spring WizardFormController, that handles a multi-page form and is included into the website using a JSP and <c:import>. After the wizard is finished, I would like to redirect to a different page, but that seems to be impossible from the Controller. At least, if I could get a message to the surrounding JSP, it would already help.
It seems, the only way is to use JavaScript to create a client-side redirect like this:
<script type="text/javascript>
window.location.href = '<URL of Target>';
</script>