Spring MVC. Controller and JSP collaboration - java

I have jsp page which contains tab control. Each tab has category name as a caption and contains a table with information about groups of selected category. Clicking on the tab should make a query in the DB and populate the table in the tab with a list of category groups. So I have a method in controller
public List<Group> getGroups(int idCategory) {
return groupService.getList(idCategory);
}
How can I call this method in jsp?
The only way I see is to put a Map in the ModelMap where a key is a category Id and a value is a list of Groups. But I don't like this idea because we need to make all queries to fill the map, but we may not need some of them if we don't click all the tabs.

If you don't want to load all the data in the controller when you render the page, then you should probably load only the data for the tab which will be displayed initially and retrieve the data for the rest of the tabs with AJAX calls as needed.
For this you would have to have some client side logic in JavaScript and probably some dedicated controller on the backend which will return the groups for given category ID for example in JSON or XML.

The only way is with the model. If you want to use it that way you should make a call using ajax and then manipulating the data with javascript or a plugin for tables(DataTables for example).

Related

How to make id clickable and link to another jsp page

Database result screenshot has been attached. Now i want to make emp_no clickable and link it to another jsp file for more details.
Need suggestions.
Simplest option is to use the anchor tag and link it to the appropriate controller:
Something like this:
<a href='employeeInfoController?id=<%=id>'><%=id></a>
employeeInfoController is your Controller Servlet or JSP that shows the details of the employee whose id is passed as a parameter. The controller can be a JSP or a serlvet.
You will need to put in all the appropriate checks to ensure the person viewing the details is authorized, etc.
Hope this helps!

How to automatically reload a jsp based on other page action

I have a situation that i can't handle and thats why need your help.
I have a jsp page (mention as A in below pic) where there are many rows and each of them can be edited.
At the end of the A jsp page, there is an option to print the page data.
Now, if some body clicks on the edit link/button, another page will open contain the data for that particular row and user can modify the data in the second page(i,e B).
Now, i want, as soon as the user save the B page, A page should be refreshed automatically to provid the updated data for printing.
Please guide me on how to acheive that . I'm using Spring MVC framework for the java application.
The Spring MVC way to meet your requirement is:
the Edit buttons in page A should be links calling page B with the id of the line to edit, something like Edit
the SaveAndClose button in page B should be a submit button that posts the edited values to a controller method. After server side processing, the controller method should redirect to page A. As a side effect, you use the PostRedirectGet pattern which avoids the ugly do you want to send again ... ?"
#RequestMapping(path = "...", method = RequestMethod.POST)
public String saveAndClose(...) {
...
return "redirect:/path/to/pageA";
}
Of course this will display all pages in same window.
If you want to redisplay the window containing pageA on the Save & Close from page B, still allowing the save to be known to the server, you should redirect to a special page (say pageC) that just contains javascript code asking the browser to redisplay pageA. You can either pass the name of the window containing pageA in a hidden field, or you can decide that as the programmer of the web application you know where it should be.
It can be achieved like this. Follow the steps mentioned
1] When you click on edit button in Page A, pass the id of the row to Page B as request parameter.
2]In Page B JSP receive the id of the row and store it in a hidden element.
3]Create a JavaScript function in Page A which should receive row Id and Modified data as parameter. Lets name it this function as updateRows(rowId,modifiedData). In this function write code to update the with id 'rowId' with modified data using javascript
4]Now When you click on 'Save & Close' Button in Page B. Save the data using call to server. If save succeeds then invoke the function updateRows passing it rowId stored in hidden element and modified data as parameters. This function will update the DOM with latest data.
This way you will avoid making server call to refresh the data
There is one more way if you don't want to use ajax.
In Page A define a javascript function refreshPageA(). In this function add page refreshing logic.
When you click on 'Save & Close' button in Page B save the data in server and forward to a plain jsp. In this JSP declare a onload handler. Inside onload handler add following code
opener.refreshPageA();
window.close()
This will refresh pageA and close page B window

How to select the particular row data in jsp using radio button

I know its a basic question and please bear with this.
I'm new to struts frame work and I'm creating a sample application using struts 1.2. I've inserted few values into the database using form beans. And i'm displaying all the database records in the jsp page using function. and i also placed the radio button for each row..
Now my question is, i want to select a particular data from the records displayed in the jsp page by clicking the radio button and i need to store the selected records into the another table.
Give me some suggestion regarding the condition specified above...
Thanks in advance..
Once you set those unique values in radio button, you can get the values for your purpose. Like using jquery,
var selRadio = $('input:radio[name=cname]:checked').val();
You can find out fiddle explains how to get checked value.
Use some ajax for send that value to server (servlet) and add the values to new table without refresh entire page.
$.ajax({
url : "ur_servlet_url" + selRadio,
type : "POST",
async : false,
success : function(data) {
alert("success");
}
});
Let me know if this helps.

dynamically set target of form to iframe conditionally

I am developing a web project using spring mvc and dojo. I have a unique problem. This is what I am doing :
Submit a form to the controller and generate a pdf in the controller. Set the pdf in the response
The target of the form is an iframe so that the pdf shows in the form after submit.
This works perfect.
Now, my problem is :
I am doing validations on the form using spring mvc validator and if there is an error I must return to the same page with the error message(s).
The problem is to combine these two approaches. If I find an error and return with the return view, since the target of the form is the iframe, the response gets shown in the iframe which obviously I don't want (I want to show the new page) and if everything is good, I must set the pdf in the response and show it in the iframe using the target. How can I achieve this?
You didn't mention whether you are using ajax to submit the form. If its ajax then it would be straight forward, I think you are not.
If you are doing a normal form submit, then
Don't target the form submit to an iframe.
That solves your problem but it introduces a new one, handling the PDF response.
When your form submission passes the validation instead of returning the PDF return the same view but with some javascript code, dojo in your case, at the bottom of the page to popup an iframe whose url points to the pdf stream.
If you are using Spring MVC 3.1 you can make use of Spring MVC Flash Attributes to share any data between your form processing method and PDF serving method, If you are using previous versions of Spring you can use Session or capture the information in the redirect url to share any information between those two methods.
If you can use AJAX:
Submit the form using AJAX and return an URI for the PDF. Then set the source of the iframe to the returned URI if the validation was ok.
If can't or don't want to use AJAX:
Return the same page again but this time with the source of the iframe already set to the PDF, if the validation was ok.

How to make a button: 1. activate a bean's method and 2. navigate to a bookmarkable (!) location

h:button can navigate to a bookmarkable link, but can not call beans' methods
h:commandButton can make calls to beans' methods, and can use action attribute to navigate to somewhere but it wont be a bookmarkable location
The question is how to make a button do both
Example:
We got two JSF pages:
list.xhtml
item.xhtml
item.xhtml?id=5 opens the page with the item #5's data. There's also a "Save" button
How to make "Save" button 1. save data and 2. navigate back to list so there would be "list.xhtml" in the browser's address field and not "item.xhtml"
Just return something like this from the action method:
return "/list.xhtml?faces-redirect=true";
This will send a redirect to the client and the client will then fetch the list page in a separate GET request.

Categories

Resources