0

I would like to write whole controller to work with entity. I would like to declare the id of entity on class level and use it on each method. Here is the controller class:

@Controller
@RequestMapping(value="/job/{j_id}/instance")
public class JobController extends GenericController {
    private final String htmlDir = "job/";

    @RequestMapping(value="{i_id}/open", method=RequestMethod.GET)
    public ModelAndView open(@PathVariable Long instance_id) {
        ModelAndView result = new ModelAndView(htmlDir  + "instance");
        result.addObject("instance_id", instance_id);

Here I would like to use variable j_id from @RequestMapping

        return result;
    }
}

Can I achive this? Please help. Give me some code snippest please.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
masterdany88
  • 5,041
  • 11
  • 58
  • 132

1 Answers1

2

Have a try like this

@Controller
@RequestMapping(value="/job/{j_id}/instance")
public class JobController  {
    private final String htmlDir = "job/";

    @RequestMapping(value="{i_id}/open", method=RequestMethod.GET)
    public ModelAndView open(@PathVariable(value="j_id") Long instance_id) {
        ModelAndView result = new ModelAndView(htmlDir  + "instance");
        result.addObject("instance_id", instance_id);
        System.out.println("Instance Id -------------> " + instance_id);
        return result;
    }

}

Please notic "@PathVariable(value="j_id")"

To get both variables, you can change that line as following:

    @RequestMapping(value="{i_id}/open", method=RequestMethod.GET)
    public ModelAndView open(@PathVariable(value="j_id") Long jnstance_id, @PathVariable(value="i_id") Long instance_id) {
            .....
    }
black bean
  • 31
  • 6