大家好日子。Spring MVC中的會話同步
請幫幫我。 我有應用程序和簡單的控制器,從數據庫中搜索數據,當數據建立在數據庫中時,它也在瀏覽器中呈現它們,當數據出現在頁面上時,如果用戶想要在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表文件保存相同的數據作爲Excel文件我從表二(最新我渲染)的數據,但我想獲得我的舊數據從標籤之一 我在學習Servlets之前對真正感興趣的會話同步。這是我的作品在我的情況下在servlets中,它可以達到:HttpSession session = request.getSession();
我怎麼能在Spring MVC中做到這一點?它會解決我的問題嗎? 請給我建議。
謝謝你們。 一切順利。
爲什麼你有decalred throws NullPointerException? –
在我的resultXLS(...)方法??認爲我必須檢查我的模型數據爲空,並在發生時使用NullpointerException呈現自定義消息。這不對嗎? – Vad
不能處理NullPointerException,也不能將其聲明爲方法簽名(引發)的一部分。如果你得到NullPointerException,那意味着你的代碼有一些錯誤。 –