이것은 바보 같은 질문 일 수 있지만 혼란 스러울뿐입니다. 개체가 생성 된 컨트롤러가 있습니다 & 다른 여러 컨트롤러에서 해당 개체를 사용하고 싶습니다.
현재 내 개체는 다른 사용자 요청이나 세션 요청이있을 때 덮어 쓰는 클래스 변수입니다. 좋지는 않다라고 확신합니다. & 일부 세션 문제가 발생합니다.다른 컨트롤러에있는 개체 사용
@Controller
public class DemoController
{
/* CURRENTLY THIS IS WHAT I'M DOING BUT I DON'T WANT THIS VARIABLE GLOBAL*/
private MyCommonObject myCommonObject = new MyCommonObject();
@RequestMapping(value="/demo-one", method=RequestMethod.POST)
public ModelAndView postControllerOne(@ModelAttribute SearchForm searchForm,
ModelMap modelMap)
{
//I want to use this object in all other controllers too
myCommonObject = someMethodToGetApiResult(searchForm);
SomeOtherObject someOtherObject = getSomeObject(myCommonObject);
modelMap.addAttribute("someOtherObject",someOtherObject);
return new ModelAndView("/firstJSP");
}
@RequestMapping(value="/demo-two", method=RequestMethod.POST)
public ModelAndView postControllerTwo(@ModelAttribute SomeForm someForm,
ModelMap modelMap)
{
// Used the class variable here
SomeOtherObject someOtherObject = getSomeObject(myCommonObject,someForm);
modelMap.addAttribute("someOtherObject",someOtherObject);
return new ModelAndView("/secondJSP");
}
@RequestMapping(value="/demo-three", method=RequestMethod.POST)
public ModelAndView postControllerThree(@ModelAttribute SomeForm someForm,
ModelMap modelMap)
{
// Used the class variable here
SomeOtherObject someOtherObject = getSomeObject(myCommonObject,someForm);
modelMap.addAttribute("someOtherObject",someOtherObject);
return new ModelAndView("/thirdJSP");
}
@RequestMapping(value="/demo-four", method=RequestMethod.POST)
public ModelAndView postControllerFour(@ModelAttribute SomeForm someForm,
ModelMap modelMap)
{
// Used the class variable here
SomeOtherObject someOtherObject = getSomeObject(myCommonObject,someForm);
modelMap.addAttribute("someOtherObject",someOtherObject);
return new ModelAndView("/fourthJSP");
}
}
감사합니다 :의 아래 가정하자 나의 시나리오입니다.
들여 쓰기를 수정하여 코드를 사람이 읽을 수있게 만들면 코드를 실제로 볼 수 있습니다. – Andreas
@ Andreas 그랬습니까. 그 코드를 스택 에디터에 썼는데 IDE가 아니기 때문에 .. 솔루션을 얻으면 행복 할거야. – Yadvendra