大家好。
请帮助我。我有应用程序和简单的控制器,它从数据库中搜索数据,当数据库中的数据建立时,它也会在浏览器中呈现它们,当数据出现在页面上时,如果用户想要在Excel文件中呈现它,也会出现Render in EXCEL
按钮。这是控制器:
@Scope("session")
@Controller
public class SearchController {
//Apache log4j logger for this controller to bring info to console of executing inside of this controller
@SuppressWarnings("unused")
private static final Logger logger = LoggerFactory.getLogger(SearchController.class);
@Autowired
private EducationWebServiceInterface educationWebService;
List<FormDate> listForm = null;
@ModelAttribute("listOfDates")
public List<Date> prepareModelDate() {
List<Date> listOfDates = educationWebService.fetchAllDatesService();
return listOfDates;
}
@ModelAttribute("listOfNames")
public List<String> prepareModelNames() {
List<String> listOfNames = educationWebService.fetchAllInstitutionNamesService();
return listOfNames;
}
@ModelAttribute("listOfTypes")
public List<String> prepareModelTypes() {
List<String> listOfTypes = educationWebService.fetchAllInstitutionTypesService();
return listOfTypes;
}
@RequestMapping(value="/search", method=RequestMethod.GET)
public String search(FormBackingObjectSearch fbos, Model model) {
model.addAttribute("fbosAttributes", fbos);
return "search";
}
@RequestMapping(value="/result", method=RequestMethod.GET)
public String resultHTML(@RequestParam String particularDate,
@RequestParam String nameOfInstitution,
@RequestParam String typeOfInstitution,
@ModelAttribute("fbosAttributes") @Validated FormBackingObjectSearch fbos,
BindingResult bindingResult,
Model model) throws Exception {
ValidatorSearch validatorSearch = new ValidatorSearch();
validatorSearch.validate(fbos, bindingResult);
if(bindingResult.hasErrors()) {
return "search";
}
listForm = new ArrayList<FormDate>();
//Case 1:
if(!fbos.getParticularDate().equals("") && !fbos.getNameOfInstitution().equals("") && fbos.getTypeOfInstitution().equals("")) {
listForm = educationWebService.fetchByDateAndNameService(DateChangerUtils.dateConvertation(fbos.getParticularDate()), fbos.getNameOfInstitution());
model.addAttribute("findAttributes", listForm);
//Case 2:
} else if(!fbos.getParticularDate().equals("") && fbos.getNameOfInstitution().equals("") && !fbos.getTypeOfInstitution().equals("")) {
listForm = educationWebService.fetchByDateAndTypeService(DateChangerUtils.dateConvertation(fbos.getParticularDate()), fbos.getTypeOfInstitution());
model.addAttribute("findAttributes", listForm);
//Case 3:
} else if(!fbos.getParticularDate().equals("") && fbos.getNameOfInstitution().equals("") && fbos.getTypeOfInstitution().equals("")) {
listForm = educationWebService.fetchByDateService(DateChangerUtils.dateConvertation(fbos.getParticularDate()));
model.addAttribute("findAttributes", listForm);
//Case 4:
} else {
throw new Exception("Exception occurs because it's not correspond to any case in controller");
}
return "search";
}
@RequestMapping(value="/result.xls", method=RequestMethod.GET)
public String resultXLS(Model model) throws NullPointerException {
if(listForm == null || listForm.isEmpty() == true) {
throw new NullPointerException("Can not create Excel file because no data to create from");
} else {
model.addAttribute("findAttributesXls", listForm);
}
return "xlspage";
} //End of the resultXLS(..) method
} //End of the class
现在让我们在浏览器中使用选项卡: 我的问题是,当我将浏览器选项卡一中的渲染数据保存为 Excel 文件并在浏览器选项卡二中打开新选项卡时,再次在选项卡二中找到并呈现一些不同的数据,然后返回浏览器的第一个选项卡,再次尝试将表一中的相同数据保存为 Excel 文件我从表二中获取数据(最新我渲染), 但我想从第一个选项卡中获取旧数据我在 Servlet 之前学习,并且对会话同步非常感兴趣。它在我的情况下工作 在servlets中,它只能通过以下方式访问:HttpSession session = request.getSession();
如何在春季 MVC 中执行此操作???它会解决我的问题吗?请给我建议。
谢谢大家。一切顺利。
您可以通过添加参数 HttpSession 会话来访问控制器方法中的会话
@RequestMapping(value="/result.xls", method=RequestMethod.GET)
public String resultXLS(HttpSession session, Model model) throws NullPointerException {
Object myObj = session.getAttribute("myAttr");
....
}
另一种选择是在控制器方法中使用 HttpServletRequest 类型的参数
@RequestMapping(value="/result.xls", method=RequestMethod.GET)
public String resultXLS(HttpServletRequest request, Model model) throws NullPointerException {
Object myObj = request.getSession(false).getAttribute("myAttr");
....
}
但是,同步会话不会解决您的问题。会话同步最适合大量并行请求不断出现并修改存储在会话中的共享数据的情况。这不是你所说的情况。
你想要的是基于选项卡的状态,这是你不会得到任何现成的解决方案的东西,也不是一个好的做法。这将使您的会话非常繁重,并且您的 Web 应用程序将无法扩展。