SSH中Action 的service 空指针错误
Action代码:
public class ListMember extends ActionSupport{
//通过applicationContext.xml配置文件注入memberService的值
private MemberService memberService;
public MemberService getMemberService() {
return memberService;
}
public void setMemberService(MemberService memberService) {
this.memberService = memberService;
}
private int page;//第几页
private PageBean pageBean;//包含分布信息的bean
public int getPage() {
return page;
}
public void setPage(int page) {//若URL中无此参数,会默认为第1页
this.page = page;
}
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
@Override
public String execute() throws Exception {
//分页的pageBean,参数pageSize表示每页显示记录数,page为当前页
//这一行memberService报null;
this.pageBean = memberService.queryForPage(2, 1);
return SUCCESS;
}
}
struts.xml的配置:
<struts>
<package name="siseffa" extends="struts-default">
<action name="Login" class="com.siseffa.struts.action.LoginAction">
<result name="success">/main.jsp</result>
<result name="login">/login.jsp</result>
</action>
<action name="selectAllMember" class="com.siseffa.struts.action.MemberAction">
<result name="success">/right.jsp</result>
</action>
<action name="addMember" class="com.siseffa.struts.action.MemberAction">
<result name="success">/showmember.jsp</result>
</action>
<action name="addUser" class="com.siseffa.struts.action.UserAction">
<result name="success">/index.jsp</result>
</action>
<action name="updateUserPassword" class="com.siseffa.struts.action.UserAction">
<result name="success">/index.jsp</result>
<result name="input">/updateUserPassword.jsp</result>
</action>
<action name="updateUserInfo" class="com.siseffa.struts.action.UserAction">
<result name="success">/index.jsp</result>
</action>
<action name="selectMember" class="com.siseffa.struts.action.MemberAction">
<result name="success">/updateMember.jsp</result>
</action>
<action name="listMember" class="com.siseffa.struts.action.ListMember">
<result name="success">/right.jsp</result>
</action>
</package>
</struts>
applicationContext.xml中的部分配置:
<bean id="memberDao" class="com.siseffa.MemberDao.Imp.MemberDaoImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="memberService" class="com.siseffa.MemberDao.Imp.MemberServiceImp">
<property name="memberDao" ref="memberDao"/>
</bean>
<bean id="listMember" class="com.siseffa.struts.action.ListMember">
<property name="memberService" ref="memberService"/>
</bean>
ssh j2ee java web action --------------------编程问答-------------------- 网上说struts.xml里action的class配置应该跟spring 的bean id一样,但是更改了之后
严重: Dispatcher initialization failed
Unable to load configuration. - action - file:/D:/Program%20Files%20(x86)/apache-tomcat-7.0.37/webapps/siseffa/WEB-INF/classes/struts.xml:28:48
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
报错..不能加载 --------------------编程问答-------------------- struts.xml
<action name="listMember" class="com.siseffa.struts.action.ListMember">片段改成
<result name="success">/right.jsp</result>
</action>
<action name="listMember" class="listMember">这里的class是你applicationContext.xml中beanID
<result name="success">/right.jsp</result>
</action>
空指针原因是你这样配置的struts.xml中service根本没有注入成功 --------------------编程问答--------------------
试了改成这样,我在2楼那里阐明了,会报错,不过又查了下资料,发现我没有吧struts托管给spring 所以就算改了class为spring beanId 也会报错? --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 应该是没有把struts2的对象工厂交给spring管理。
试试在struts2的xml文件里面添加:
<constant name="struts2.ObjectFactory" value="Spring"/> --------------------编程问答-------------------- 没有交给spring管理吧?struts配置文件内没有相应的说明加上
<constant name="struts2.ObjectFactory" value="spring"/>
补充:Java , Web 开发