在web.xml 中我已经配置了
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
并指明了路径
但是在action 中
必须要加上
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserAction u = (UserAction) ctx.getBean("userAction");spring 才能 注入成功,否则调用service 的方法就会报空指针异常
请问是怎么回事
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
并指明了路径
但是在action 中
必须要加上
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserAction u = (UserAction) ctx.getBean("userAction");spring 才能 注入成功,否则调用service 的方法就会报空指针异常
请问是怎么回事
在struts配置文件中加
<constant name="struts.objectFactory" value="spring" />
struts1刚加
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
UserAction u = (UserAction) ctx.getBean("userAction");
是读配置文件的
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
++ 先要对Spring容器进行实例化,再到spring配置文件中取那些注入的bean对象。。
上面的方法都试了还是没用啊。
下面是我的代码:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="TreetestDAO" class="dao.TreetestDAO"
>
</bean>
<bean id="myaction" class="dao.myaction">
<property name="dao">
<ref bean="TreetestDAO" />
</property>
</bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.opensymphony.xwork2.ActionSupport;
public class myaction extends ActionSupport { /**
* S
*/
private static final long serialVersionUID = 1L;
private String names; public String getNames() {
return names;
} public void setNames(String names) {
this.names = names;
}
private TreetestDAO dao;
public void setDao(TreetestDAO dao) {
this.dao = dao;
} public String userAdd() throws Exception{
String l = dao.findAll();
names=l;
return SUCCESS;
}}TreetestDAO.java
public class TreetestDAO implements itreedao {
public String findAll() {
String aa= "321321";
return aa;
}}
① 你的web.xml中 没有配置路径
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
② 你的在web.xml中配置的spring文件 没有import 你所要加载的spring的xml
③ 你的spring的xml中对应的类没有注入