<!-- 让系统认识到spring -->
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
然后在LoginAction中:
//在此调用DAO
//系统找不到指定的文件错误:ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
ServletContext application = this.getServlet().getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);
IDao dao = (IDao)wac.getBean("dao");
dao.getCustomerByAccount(account);
若继承ActionSupport,则代码写成:
//继承actionSupport后,application就可以不要了。
//ServletContext application = this.getServlet().getServletContext();
//WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);
WebApplicationContext wac = this.getWebApplicationContext();
IDao dao = (IDao)wac.getBean("dao");
dao.getCustomerByAccount(account);