java - Spring @SessionAttributes 返回 null

标签 java spring spring-mvc jakarta-ee spring-annotations

我尝试在我的 Controller 类中使用 @SessionAttributes,如下所示:

@Controller
@SessionAttributes({"calMethods", "taxList"})
@RequestMapping("/secure")
public class reportController extends BaseController {

//..
@ModelAttribute("taxList")
    public List<multiCalDto> getTaxList() {
        return new ArrayList<multiCalDto>();
    }
//....

@RequestMapping(value = "/confirmCal.html", method = RequestMethod.GET)
    public ModelAndView launchconfirmCal(HttpServletRequest request, @RequestParam("seqNo") String seqNo) {
    ...........

    ModelAndView modelAndView = new ModelAndView("confirmCalView");
    modelAndView.addObject("taxList", calBean.getTaxList());
    return modelAndView;
      }


@RequestMapping(value = "/executeCalPay.html", method = RequestMethod.POST)
  public ModelAndView executeCalPay(HttpServletRequest request, @ModelAttribute("taxList") List<multiCalDto> taxList) {
      // ............



      ModelAndView modelAndView = new ModelAndView("calView");
      System.out.println("taxList -- "+taxList);
      return modelAndView;
    }

}

我在 launchconfirmCal() 中添加了taxList,并尝试在executeCalPay() 中访问相同的内容。 我尝试在添加到 modelAttribute 之前打印taxList,大小为12,当我在executeCalPay() 中退出时,它显示为null。 我不会改变它在 JSP 中的值。

最佳答案

删除或注释掉此方法并重试

@ModelAttribute("taxList")
    public List<multiCalDto> getTaxList() {
        return new ArrayList<multiCalDto>();
    }

@ModelAttribute 注释方法在 ALL 请求映射方法之前调用,因此它会在调用 post 方法之前重置您的taxList

关于java - Spring @SessionAttributes 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28863910/

相关文章:

java - 在 Spring Boot 中为 @WebMvcTest 禁用 Spring Security 配置类

spring - 使用 TX :annotation-driven prevents Autowiring a bean

java - 将鼠标悬停在相邻节点上时不会触发 MouseEntered/Exited

java - 在java代码中从wsdl调用web服务

java - 如何将 FindBugs maven 插件配置为仅检查 @Nonnull 等注释违规?

java - 试图理解为什么 @transactional(noROllBackFor .... 不起作用

jsf - 哪些技术最适合我的应用程序 : Struts with Hibernate or Spring with Hibernate

java - 制作没有标签的折线图

java - 如何在@RequestParam中设置默认值为LocalDateTime

java - 线程 "main"中的异常 org.springframework.jdbc.CannotGetJdbcConnectionException