Results 1 to 6 of 6

Thread: Passing URL parameters to Spring Controller

  1. #1
    Sencha User
    Join Date
    Nov 2011
    Posts
    149
    Answers
    2
    Vote Rating
    1
      0  

    Default Answered: Passing URL parameters to Spring Controller

    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.
    Code:
    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,
    Code:
    @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
    Code:
    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
    Code:
    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

  2. 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.

    Code:
    @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:

    Code:
    @RequestParam("getId") int getId
    Yours looks kind of like you are trying to get a rest-like param from the url.

  3. #2
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,382
    Answers
    3997
    Vote Rating
    1536
      0  

    Default

    so you see the getid as a parameter of the request you just need to handle it in the java code?
    Mitchell Simoens @LikelyMitch
    Modus Create, Senior Frontend Engineer
    ________________
    Need any sort of Ext JS help? Modus Create is here to help!

    Check out my GitHub:
    https://github.com/mitchellsimoens

  4. #3
    Sencha User carol.ext's Avatar
    Join Date
    Jun 2007
    Location
    Littleton, Colorado, USA
    Posts
    708
    Answers
    25
    Vote Rating
    36
      0  

    Default

    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.

    Code:
    @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:

    Code:
    @RequestParam("getId") int getId
    Yours looks kind of like you are trying to get a rest-like param from the url.

  5. #4
    Sencha User
    Join Date
    Nov 2011
    Posts
    149
    Answers
    2
    Vote Rating
    1
      0  

    Default

    Thanks Carol and mitchellsimoens for the responses.
    @Carol I got it working like this.
    Code:
    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

  6. #5
    Sencha User carol.ext's Avatar
    Join Date
    Jun 2007
    Location
    Littleton, Colorado, USA
    Posts
    708
    Answers
    25
    Vote Rating
    36
      0  

    Default

    Spring Framework Reference v. 3 - Chapter 15 Web MVC - Section 15.3.2.4 Binding request parameters to method parameters with @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.

  7. #6
    Sencha User
    Join Date
    Nov 2011
    Posts
    149
    Answers
    2
    Vote Rating
    1
      0  

    Default

    Yes, actually I have less experience with spring and worked on servlets earlier.Actually i tried it with the standard
    Code:
    <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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •