看看这段代码:
@Controller
@RequestMapping
@SessionAttributes("address")
public class HomeController {
@RequestMapping("/home")
public String welcome(Model model) {
Address address = new Address();
model.addAttribute("address", address);
return "welcome";
}
}
每次我到达url“ / home ”时,都会调用 welcome()方法,并添加一个新对象 Address 并保存到会话中,名称为地址。
为了避免执行此行代码,检查会话“地址” 的最佳方法是什么?
Address address = new Address();
model.addAttribute("address", address);
我使用了这种方法,但是我想有一种使用 Spring功能的更好且特定的方法。
@RequestMapping
public String welcome(Model model, HttpServletRequest httpServletRequest) {
Address vecchioAddress = (Address)httpServletRequest.getSession().getAttribute("address");
if(vecchioAddress == null) {
Address address = new Address();
model.addAttribute("address", address);
}
}
谢谢
答案 0 :(得分:1)
尝试使用@SessionAttribute批注从会话中检索现有属性。
const filename = "path of file to be uploaded";
const metadata = {
sourceFormat: 'CSV',
skipLeadingRows: 1,
autodetect: true,
};
const dataset =await
bigQuery.dataset('datasetname').table('tablename').load(filename,metadata);
答案 1 :(得分:1)
您可以使用@SessionAttribute
。
public String welcome(Model model, @SessionAttribute("address") Address addres) {
if(address == null){
Address address = new Address();
model.addAttribute("address", address);
}
return "welcome";
}