This is my Problem: I have a jsp with a table using datatables. It displays some users with editing buttons at the end of the table. If you hit the "edit" button, it calls a javascript function getting the userID from the table and pass it via ajax $post.
method to a servlet. The servlet receives data from database via hibernate and maps it into objects. My code works until this point. Now i want to pass the objects to a jsp and open it in a new window ( a popup or something with a form inside for setting them with data from objects to edit the called user).
There are are two approaches to do this: Client Side Rendering or Server Side Rendering:
1.Server Side
You don't need that servlet, what you need is to make your second JSP file for input form. And in your Javascript, open a new window with Form entry JSP page address and add userId parameter to the query string of that page:
function onRowClick(userId){
//Open new window with the address: 'formEntry.jsp?userId=' + userId
}
And in the formEntry.jsp you can get this ID from request params:
String userId = request.getParameter("userId");
Then you can use this userID to retrieve your user object:
//get user from dao or service objects
User myUser = someUserService.findUserById(userId);
And then in your JSP page you can create your input fields like this:
<input name="firstName" type="String" value="<%= myUser.getFirstName %>"
About using java code in jsp (comment): You are write about not writing java code in JSP pages, but for that purpose you have 2 options:
2.Client Side
The second way is to use more javascript to handle the task. You can go with your servlet way and have your servlet return JSON String of User object. In javascript, you get the json and parse it and put its fields inside html tags:
function ajaxRequestSuccessHandler(data){
var user = //parse data and make a javascript object
document.getElementById('firstName').value = user.firstName;
....
//load all of data in your form.
}
and then you can submit your form to a servlet and save updated data.
1.It's better to use JAX-RS or Spring-MVC to expose RESTful services and use theme in javascript instead of doing this with pure servlets.
2.Better to utilize some client side MV* framework like angularJS or ReactJS.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.