Reputation: 495
there is 1 jsp page say view.jsp, so via spring it is rendered as view.htm, ok that is fine, but now i want to pass a list to it, and according to the given id from the list, the view.htm is change to view.htm/{id} but if i have 100 data, then do i need 100 jsp pages ?? how can i resolve this via spring
Upvotes: 0
Views: 637
Reputation: 8240
So if you have a collection of objects, you want to be able to view each object individually, and you want one view to be able to handle all of this. Ok, I'm hoping that your list of objects are in fact all objects stored in your database. So you have one action that returns a list of all the objects. You can use The Elite Gentleman's example to generate a link for each item in the list. That link goes to a different endpoint, passing in the database id of the object to the controller. That controller fetches the object with that id, and renders the view using that object. Here's an example:
// Your 'index' action
@RequestMapping('myObjectType/index/')
public ModelAndView index() {
List<MyObject> list = // Get your objects from the database
return new ModelAndView("index", "list", list); // insert your list into the MAV
}
// Your 'show' action
@RequestMapping('myObjectType/show/{id}')
public ModelAndView show(@PathVariable("id") String id) {
MyObject obj = // Get your object from the database from the id param
return new ModelAndView("show", "obj", obj);
}
So in your index.jsp, you iterate over your list object and create a link for each item in the collection which points to the 'show' endpoint passing in the id of the object. Then in your show.jsp, you just show whatever information you want about the object returned.
Hope that helps.
Upvotes: 0
Reputation: 89169
You can add the list in the request and iterate through it in your view.jsp
.
Like So (exaggerated example),
List<String> datas = new ArrayList<String>();
//Add 100 datas...
request.setAttribute("datas", datas);
Now, you can use JSTL to iterate over it...in view.jsp
.
<c:if test="${requestScope.datas != null}">
</c:if>
Or, didn't I understand you correctly?
Edit based on your question, you can still iterate through the data on the list and create an anchor tag that appends the data id to the view.htm
.
something, like (in pseudo code)
${someData.description}
Where someData
is the replaced datas
as put on first post. The anchor tag will be situated inside the <c:if>
tag, hence if you have 100 items in an array, you'll have 100 anchor tags.
Upvotes: 2