Caused by: java.lang.NullPointerException
at org.apache.struts2.ServletActionContext.getResponse(ServletActionContext.java:130)
at com.ssh2.action.UserAction.<init>(UserAction.java:20)
Spring 容器啟動時實例化單例 Bean userAction,調用構造器。構造器中調用 ServletActionContext.getResponse(),但此時 **Struts2 的 FilterDispatcher 尚未初始化**,ThreadLocal 中無 HttpServletResponse,返回 null,隨後操作拋出 NPE。
execute() / prepare() 或 SessionAware / ServletResponseAware 接口方法中。@Scope("prototype"),每次請求新建實例,但仍需確保在 ActionInvocation 之後調用。struts.xml 中 class="com.xxx.Action",Spring 只注入 Service,不攔截 Action 生命週期。@Autowired 注入 Service,避免手動 getBean。struts2-spring-plugin 自動掃描注解。