Maryam Moeini
Maryam Moeini

Reputation: 29

How to pass value from url (taken from JSP) to the controller in Spring MVC

I'm trying to create a delete button in my JSP to delete rows in my DB. By clicking on the button i will get the row ID and set it in my URL like this:

<tr>
	<td><c:out value="${project.projectId}" /></td>
	<td><c:out value="${project.title}" /></td>
	<td><c:out value="${project.domain}" /></td>
	<td><c:out value="${project.lang}" /></td>
	<td><c:out value="${project.author}" /></td>
	<td><c:out value="${project.created}" /></td>
	<td><a href="${pageContext.request.contextPath}/project/edit/${project.projectId}">Edit</a>
    <a href="${pageContext.request.contextPath}/project/delete/${project.projectId}">Delete</a>
	</td>

and this is the url that i get: http://localhost:8080/IRS/project/delete/414

Now 414 is the Row ID. at this point i want to sent this ID to my controller and from there delete the row. Can someone please help me on that. Thank you

Upvotes: 1

Views: 1228

Answers (2)

Maryam Moeini
Maryam Moeini

Reputation: 29

OK, So Thanks to Adnriy i fount out that my SpringDispacher in the xml file is not directing my url to the correct Controller.

Since I don't have enough knowledge about xml files I found another way to go around it, however i know its a little bit creepy but it was good for quick fix.

Here is the new HTML:

<tr>
	<td><c:out value="${project.projectId}" /></td>
	<td><c:out value="${project.title}" /></td>
	<td><c:out value="${project.domain}" /></td>
	<td><c:out value="${project.lang}" /></td>
	<td><c:out value="${project.author}" /></td>
	<td><c:out value="${project.created}" /></td>
	<td><a href="${pageContext.request.contextPath}/project/${project.projectId}/edit.do">Edit</a>
    <a href="${pageContext.request.contextPath}/project/${project.projectId}/delete.do">Delete</a>
	</td>

and then in my controller i used this value:

@RequestMapping(value="/project/{prjId}/delete.do" ,method = RequestMethod.GET)
public ModelAndView delete(HttpServletRequest request, @PathVariable String prjId) throws IOException{
  ......
  }

Upvotes: 0

andriy
andriy

Reputation: 4164

This url can be mapped as following in your controller:

@RequestMapping(value="/project/delete/{projId}")
public String deleteFunction(@PathVariable String projId){

  ...

}

EDIT: Without having detailed information about your project, it is little bit hard to answer your question. The information that I'm talking about is: project folder structure, web.xml, application context configurations, if your configurations are xml or annotation based. If you are not very experienced in Spring MVC, I'd suggest you to:

  • Carefully read this documentation
  • Take a look at this and this spring project templates to understand Spring MVC.

Definitively your issue in the controller mapping. Verify if in your context configuration file (equivalent to /src/main/webapp/WEB-INF/application-servlet.xml from this template) you have <mvc:annotation-driven/>

Upvotes: 1

Related Questions