PDA

View Full Version : Passing URL parameters to Spring Controller



sachintaware
26 Mar 2012, 11:48 PM
Hello
I have a tree populated with some records.I want a grid to be populated based on the item clicked in the tree panel.So,I have to pass the id of the item clicked.I am getting the id onclick of the tree item like this.


treePan.getSelectionModel().on('select', function(selectionModel,record,index,options) {

var getid = record.getId('id');
console.log(getid); //This prints the id on console
store_company.load({
params: {

getid:getid

}
});

});

I can see the parameter in the url,on the console.

Now I have a spring controller,


@Controller
public class CandidatesController {

private CandidatesService candidatesService;

@RequestMapping(value="/candidates/view.action?id=getid")
public @ResponseBody Map<String,? extends Object> view() throws Exception {

try{
//List<Candidates> candidates = candidatesService.getCandidatesList();
List<CandidateStatus> candidatestatus = candidatesService.getCandidateStatusList();

return getMap(candidatestatus);

} catch (Exception e) {

return getModelMapError("Error retrieving Candidates from database.");
}
}

id=getid in the @requestMapping did not work.

and I have my query in the DAO


public List<CandidateStatus> getcandidateStatus() {

return hibernateTemplate.find("from CandidateStatus as candstat inner join fetch candstat.candidate inner join fetch candstat.requirement inner join fetch candstat.requirement.client where candstat.requirement.reqTitle='"+id+"'");
}


I want the parameter to be passed here.I achieved this in servlet by doing the simple


request.getParameter("id");


But not able to achieve the same in Spring.I am using spring 3 version and exjs4.

Thanks
Sachin
S/W Engg.
www.optionsconsultancy.com

mitchellsimoens
27 Mar 2012, 10:17 AM
so you see the getid as a parameter of the request you just need to handle it in the java code?

carol.ext
27 Mar 2012, 12:19 PM
We use @RequestParam in the method signature.

We are passing a String, maybe yours is int. This is basically from the "Web MVC framework" chapter of the Spring 3.0 docs. Not sure if your param is called id or getId from your post.


@RequestMapping(value="/getMyDetails", method = RequestMethod.GET)
public @ResponseBody Map<String,? extends Object> view(@RequestParam String getId) throws Exception {

Now I am looking at the doc and it shows request param like this, again in the method signature:


@RequestParam("getId") int getId

Yours looks kind of like you are trying to get a rest-like param from the url.

sachintaware
27 Mar 2012, 8:54 PM
Thanks Carol and mitchellsimoens for the responses.
@Carol I got it working like this.


public @ResponseBody Map<String,? extends Object> view(@RequestParam(value="getid")String getid) throws Exception {
String id=getid;
List<CandidateStatus> candidatestatus = candidatesService.getCandidateStatusList(id);

return getMap(candidatestatus);
}


I am new to spring and I am not exactly able to get the view(@RequestParam(value="getid")String getid) part of view method.Can you just point me to the documentation or give a brief explanation of this working??

I know this is a extjs forum and not spring,but it would be helpful in my further run.

Thanks

carol.ext
28 Mar 2012, 5:33 AM
Spring Framework Reference v. 3 - Chapter 15 Web MVC - Section 15.3.2.4 Binding request parameters to method parameters with @RequestParam (http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestparam)

The Spring Framework Reference is available online as I have linked, or in the Spring download docs directory.

@RequestParam maps a request parameter to a method parameter.

Is it working and you just don't understand part of it? Sorry, not sure what you are asking.

sachintaware
28 Mar 2012, 7:48 AM
Yes, actually I have less experience with spring and worked on servlets earlier.Actually i tried it with the standard
<br>request.getParameter();but learnt that not a good way of handling request.I saw the implementation on some forum and tried it that way without worrying of how actually the annotation handles it.(differently from request.getParameter();)Hence the question.And thank you very much for the link!>:)

Sachin
S/W Engg
www.optionsconsultancy.com (http://www.optionsconsultancy.com)