提交请求时候报错
无法获得到service,始终为NullapplicationContext.xml配置文件<bean id="UserInfoDAO" class="test.dao.impl.UserInfoDAO" scope="prototype">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 业务逻辑层注入 -->
<bean id="UserInfoService" class="test.service.impl.UserInfoService" scope="prototype">
<property name="userInfoDAO" ref="UserInfoDAO" />
</bean>
<!-- Action注入 -->
<bean id="userAction" class="test.action.UserInfoAction" scope="prototype">
<property name="userService" ref="UserInfoService"/>
</bean>
struts.xml<struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<action name="userAction" class="userAction">
<result name="success">/showAll.jsp</result>
</action>
</package>
<!-- Add packages here --></struts>
web.xml<!-- 加载时加载所有spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 拦截器 拦截客户所有请求 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>Action代码 public class UserInfoAction extends ActionSupport
{
private UserInfoService userService;
private UserInfo user; @Override
public String execute() throws Exception
{
if(userService.getUserInfoByName(user))
{
System.out.println("成功");
return SUCCESS;
}
return null;
} public UserInfo getUserInfo()
{
return user;
}
public void setUserInfo(UserInfo user)
{
this.user = user;
}
public void setUserService(UserInfoService userService)
{
this.userService = userService;
}
}
无法获得到service,始终为NullapplicationContext.xml配置文件<bean id="UserInfoDAO" class="test.dao.impl.UserInfoDAO" scope="prototype">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 业务逻辑层注入 -->
<bean id="UserInfoService" class="test.service.impl.UserInfoService" scope="prototype">
<property name="userInfoDAO" ref="UserInfoDAO" />
</bean>
<!-- Action注入 -->
<bean id="userAction" class="test.action.UserInfoAction" scope="prototype">
<property name="userService" ref="UserInfoService"/>
</bean>
struts.xml<struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<action name="userAction" class="userAction">
<result name="success">/showAll.jsp</result>
</action>
</package>
<!-- Add packages here --></struts>
web.xml<!-- 加载时加载所有spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 拦截器 拦截客户所有请求 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>Action代码 public class UserInfoAction extends ActionSupport
{
private UserInfoService userService;
private UserInfo user; @Override
public String execute() throws Exception
{
if(userService.getUserInfoByName(user))
{
System.out.println("成功");
return SUCCESS;
}
return null;
} public UserInfo getUserInfo()
{
return user;
}
public void setUserInfo(UserInfo user)
{
this.user = user;
}
public void setUserService(UserInfoService userService)
{
this.userService = userService;
}
}
加过这个包了,启动容器的时候控制台没有报错
登陆提交的时候,控制台也是干净的,页面上报错,java.lang.NullPointerException
service是Null,没有注入成功
applicationContext.xml配置文件中
我看不懂你的class里的写法。是命名问题。还是写错啦。下边是我你参考下把
<bean id="depthnewsDao" class="org.myledu.dao.impl.DepthnewsDaoImpl">
//class="里是你实现接口dao的DaoImpl类"
<property name="sf" ref="sessionFactory" />
</bean>
<bean id="depthnewsService" class="org.myledu.service.impl.DepthnewsServiceImpl">
//class="里是你实现接口Service的ServiceImpl类"
<property name="depthnewsDao" ref="depthnewsDao"/>
</bean>
刚学习struts2,只做了实现,命名也没有太在意
也没有搞接口
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
http://blog.csdn.net/chinarenzhou/archive/2009/07/15/4349730.aspx
依照现在来看,不能明显的看出哪里有问题
LZ可再提供些信息
输出看看user里是否有值
注入的时候只需要set方法,
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
即可解决空指针的问题