I have an index.jsp file and I want to call a method in Bean on load of this page. How should I implement this part?
Thanks
You cannot know onload in JSP. You need to make a request to jsp by writing some javascript.
onload event is in client side and you are wanting to do some server side stuff.
These are totally two things. Do you want to do something when the HTML loaded or before the JSP is invoked.
case 1: No better ways than using jQuery ready event. It tries to wait until the page is loaded. But no one can assure this.
Example code:
$.ready(function(){
/* do something. If it is an AJAX, use $.ajax(); */
});
case 2: You can either use a filter or just simply put the code on top of the JSP file.
Not sure if it is really what you want. If you want to call a method on an object on server when your page is called, you have to ways to do it :
a scriptlet at the beginning of the page, you can put the bean that you want to call is session or application scope
do not call directly the jsp but create a servlet. Do all your java treatment in the servet, load beans in request attributes if you want to use them later in the jsp and forward to the jsp.
The latter solution avoid the scriptlet and is easier to write and to test.
Related
I am using a JSP:include tag on every view to show my menu. Yesterday i found out that you can actually include a controller method which is very neat because it allows me to do some processing before returning the menu. What I am doing is i'm adding this to every page :
<jsp:include page="/menu/showMenu"/>
I used to do just this :
<%# include file="menu.jsp" %>
This change allows me to pass boolean values to the model when the menu is returned which i am using to either display or not display certain options depending on what the user has access to. This works perfectly when first loading a page but the menu completely disappears when any sort of controller processing is done. For example, i have a log page with a table showing log entries from the database. The column headers contain search fields which are part of a form that, when submitted, calls a controller method to filter the log list and display the filtered data. The return statement from that controller method is simply :
return "logs";
This works perfectly when using the original include that points to the actual JSP file instead of the controller method but with the new approach of including a controller method the menu vanishes when the filtering logic returns the page. What i can always do is include the JSP file as i used to and place the service calls in the JSP directly to check whether the user has access to each function but i really prefer the idea of having a controller doing this stuff and passing boolean values to the JSP.
Any idea why i'm seeing this behavior? I can provide code examples if necessary.
Thanks!
I figured it out! I had to add a POST method to my menu controller. It does exactly the same thing as the GET method but it is necessary when included in a page that is returned from another controller's POST method. Hopes this can help someone!
We have a Struts2 application using the <sx:tabbedpanel>. I know this has since been deprecated, but we have not yet had time to replace it.
We're populating the tabs by using the <sx:div> tag and specifying the href attribute, which makes an asynchronous call to the server to populate the contents of the tab. The downside to this is that we lose validation information like <s:actionerror>.
Here's what we think is happening... when the user performs an invalid action, the action class returns validation errors. When the resulting jsp is loaded, the validation messages are available. However, the <sx:div> then makes the asynchronous call back to the server to reload the contents. This time, the action class is just loading data to display, so it doesn't generate any validation messages. The results of this ajax call are then displayed in the browser, without any validation messages.
I've seen many examples on the web of using the <sx:div> tag this way within the tabbedpanel, so I'm guessing this is a problem that has been solved before, we just haven't found it.
Does anyone know of tutorials or examples that show how to do validation in this case?
Thanks for the help, I really appreciate it.
Would it make sense to put the errors outside of the tabbedpanel like:
<s:fielderror />
<sx:tabbedpanel...>
...
</sx:tabbedpanel>
Perhaps we can assist you a bit more if you can post some sample code or provide more info as to what kind of errors are expected and what the content of the tabbed panel is.
We found that we could populate the divs by using the <s:action> tag instead. This gets rendered at the time of the initial request, it does not make an ajax call later. Therefore, the JSPs have access to the validation messages and errors.
i want to pass one arraylist for one jsp and another one arraylist for another one servlet from same servlet . i have already sent that first arraylist to servlet to jsp it works.but i want to sent another one arraylist to another servlet, am using same code syntax but it doesn't works? what can i do?
What do you mean when you say that you pass array list from servlet to jsp?
If you are forwarding or redirecting HTTP request you can pass as many parameters as you wish using HTTP session attributes.
From my guess, I belive what you are looking for is the MVC pattern for passing you model (List) to view (JSP). Here is the best example I found with google of how to use MVC with JSP
If you have problem with adopting the code to work with yours, please give me explanation of what the problem is and what codes or error messages look like. It will be more helpful to those who try to help.
We're using this JSP template solution almost verbatim at work:
http://java.sun.com/developer/technicalArticles/javaserverpages/jsp_templates/
When it gets JSP pages to be included, it uses pageContext.include, which leaves us with one problem, and that is that we have a lot of scriplet code that gets initialized in the JSP itself (tag soup). My thought was to modify the template tag with an additional attribute that is a package path reference to a class with an init or execute method. That execute would get called first, and would add page context attributes before including the JSP. We would then use JSTL to access those attributes. However, I was told this wouldn't work because of how pageContext.include works, and the inability to pass through attributes scoped to the page. Is that true, and are there workarounds? I'm so-so on knowing all my scoping rules.
Correct, the problem is that the PageContext is literally that, a Page Context. When you run the include, that resource (assuming it's a JSP) get's its own PageContext, and that is lost upon return.
JSP has 4 scopes: Application, Session, Request, and Page. Each of those has its own lifecycle, which should be self explanatory.
The use of the Request scope here is the right idea.
If you look at the template code that you linked to, that's exactly what the Insert tag is doing. In this case, it's putting Hashtables on to a Stack that is maintained in the Request.
Then it uses the "put" and "get" tags to put/get items on and off that current "stack".
A simple thing that you can do is before the PageContext.include, invoke your "execute" method as appropriate. Have that method simply return a Map of name/value pairs. Then you can take that Map and populate the existing (or soon to be existing) Hashtable on the Stack.
Basically your Init class is logic that's similar to calling a lot of the "put" tags.
Other than that your template tags work the same.
Or you can merge the results straight in to the Request, for use by the JSTL. Or you can keep the "stack" nature, pushing your own "context" in to the Request.
You can pass an attribute scoped to the request:
<c:set var="myAttribute" value="myValue" scope="request" />
or
<% request.setAttribute("myAttribute", "myValue"); %>
And then on your included page:
<c:out value="${myAttribute}" />
We need some input on what is a good design pattern on using AJAX in a Java application.
Consider a simple scenario:
User clicks a button which sends a request to a Java method to fetch data from DB.
Java object is returned by method and needs to be converted into a HTML table.
HTML table is shown on JSP.
What we currently do:
On a JSP page, user clicks "Show Users" button
Button using Prototype.js calls a "middleman" JSP which forwards the request to the Java method to get the data from the DB.
The method returns the Java object to the "middleman" JSP which converts the Java object into HTML (since the AJAX call from the calling JSP won't be able to handle the Java object directly).
The HTML is then returned to the Prototype call which updates the div on the calling JSP.
Our concerns are:
We would like to keep the separation of business/presentation logic and would prefer no HTML/JavaScript code inside our Java methods.
Keeping (1) in mind, is having a "middleman" JSP an OK way to do this? Or should we return the Java object as XML/XSLT to the AJAX request?
The above way we're doing has very little JavaScript and works in all browsers.
We looked at some other packages - DWR, GWT, but either there was too much dependency on JavaScript or required UI components to be present in the Java classes.
Is our way of doing things above OK? Or is there another preferable way?
Any help/thoughts would be appreciated.
Thanks,
SP
Sounds fine. You are separating view components from model components. It shouldn't matter how the call comes to the server, AJAX or not, it should be received by a controller (a servlet say) that interacts with the model, thats your Java classes that get the data from the database and forward to a JSP page for rendering the view.
There are frameworks that could simplify the boilerplate code but the design you describe sounds fine.
I'm not sure if you noticed, but there's one significant difference between your solution and what Vincent proposed. This is that the request should be initially received by a servlet (or controller, or Struts action, etc.) rather than a "middleman" JSP.
MVC dictates that JSPs should really only we used for generating the view from the model data, flow control is better handled in Java code.