0

Good day folks. I have Controller in my application:

        @Controller
        public class LoginSearchController {

            //List to store data from resultHTML method
            List<Cars> listCars = null;


            @Autowired
            private EducationWebServiceInterface educationWebService;

                //This method render jsp page for user and create model for resultHTML
                @RequestMapping(value="/search", method=RequestMethod.GET)
                public String search(FormBackingObjectSearch fbos, Model model) {   
                        model.addAttribute("fbosAttributes", fbos);

                    return "search";        
                }   

               //   This method get name form form back object and use it for fetchCarsByNameService method
              // After if something found it stores it in listofSerchObjects and after listofSerchObjects stores it in listForm List
                @RequestMapping(value="/result", method=RequestMethod.GET)
                public String resultHTML(@RequestParam String name,
                                         @ModelAttribute("fbosAttributes") FormBackingObjectSearch fbos, 
                                         BindingResult bindingResut, Model model) throws Exception {


                    listCars = new ArrayList<Cars>();
                    List<Cars> listofSerchObjects = null;

                    listofSerchObjects = educationWebService.fetchCarsByNameService(dateConvertation(fbos.getName()));
                        model.addAttribute("findAttributes", listofSerchObjects);   
                        listCars.addAll(listofSerchObjects);

                    return "search";
                }


                  //This method fetch data from listForm to make Excel document form the list
                @RequestMapping(value="/result.xls", method=RequestMethod.GET)
                public String resultXLS(Model model) throws Exception {

                        if(listCars == null || listCars.size() == 0) {
                            throw new NullPointerException("ArrayList<FormDate> formDateList is empty list");
                        } else {            
                            model.addAttribute("findAttributesXls", listForm);
                        }               
                    return "xlspage";
                }
        }

What happens in controller:

User goes on Search page fill name of Car he would like to find(in my case) and after click button FIND

--> Request goes to /result. This resultHTML method invoke fetchCarsByNameService from service layer to fetch data corresponding to particular name to listofSerchObjects and render it on jsp page. After when listofSerchObjects have data from fetchCarsByNameService it will add all data to other list listCars .

--> Next step, when all data was rendered on jsp page there is button appears SAVE AS EXCEL, what happens: when user click this button(if user want too) it goes to /result.xls AND TAKE ALL THE DATA FROM listCars to save it in Excel file.

I'm very curious if multiple users will work on the same jsp page form different computers in my web application, how my listCars will behave. I'm scary that it will store last data witch was extracted from DataBase and if user1 want to render it in Excel file that he will have data from user2 if user2 would make search last. What you think??? Give me please advice. Thank you

4

4 回答 4

2

listCars已损坏,甚至可能偶尔抛出ConcurrentModificationException

以下是该问题的解决方法。

  1. listCars一个局部变量。

    或者

  2. Controller用_@Scope("session")

这是一个很好的博客,它描述ScopeController它的优点和缺点。

于 2013-08-07T22:26:37.527 回答
2

您的listCars实例将在每个用户之间共享,因为您将其设为类级别变量。由于每个用户(和请求)都将获得自己的线程,因此该类级别变量将在所有人之间共享,并且您的数据可能会损坏,具体取决于请求的速度。

于 2013-08-07T16:47:25.420 回答
1

A@Controller是一个 Spring bean,它只会被容器实例化一次。将与特定请求相关的任何变量保留在堆栈上(在方法内声明)。

于 2013-08-07T16:48:43.390 回答
0

listCars 将在应用程序的所有用户之间共享。

/result 和 /result.xls 的两个调用完全相互独立。您需要再次查找汽车列表或将 listCars 作为参数传递给此方法。您还可以将 listCars 存储到用户的会话中并在 result.xls 调用期间检索它,但这确实会用可能永远不会使用的不需要的数据污染会话。

根据 listCars 可能有多大以及 fetchCarsByNameService 有多昂贵,应该确定您是将列表作为参数传回,还是只传入与 /result 相同的参数并重新运行该方法。如果操作成本高昂,我可能倾向于重新运行该方法并在 fetchCarsByNameService 上进行某种缓存。

于 2013-08-07T17:44:27.937 回答