struts2\spring\hibernate采用自动装配,即第二种装配方式,action中的service总是报NullPointException,郁闷啊?还请高手解决啊!struts代码如下:
package com.hengbao.test.action;
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;
import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;import org.springframework.context.ApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;
import com.hengbao.core.page.Page;import com.hengbao.test.entity.TUser;import com.hengbao.test.service.TUserService;import com.hengbao.test.util.SystemUtils;import com.opensymphony.xwork2.ActionSupport;
public class TUserAction extends ActionSupport { private TUserService tUserService; private TUser tUser; private List tUserList; private int total; private int page; private List rows;
public String query() { HttpServletRequest request = ServletActionContext.getRequest(); Page page_ = new Page(request); ServletContext sc = ServletActionContext.getServletContext(); ApplicationContext ac = WebApplicationContextUtils .getRequiredWebApplicationContext(sc); System.out.println("tUserService:" + tUserService);
page_ = tUserService.query(new Page(null, null)); total = page_.getTotalLine(); page = page_.getCurrentPage(); rows = initRows(page_.getDataList()); return "json"; }
private List initRows(List list) { List mapList = new ArrayList(); for (int i = 0; i < list.size(); i++) { Map cellMap = new HashMap(); cellMap.put("id", ((TUser) list.get(i)).getId().toString()); cellMap.put("cell", new Object[] { ((TUser) list.get(i)).getName(), ((TUser) list.get(i)).getLoginName(), ((TUser) list.get(i)).getSex(), SystemUtils.formatDateForBirthday(((TUser) list.get(i)) .getBirthday()), ((TUser) list.get(i)).getMobilePhone(), ((TUser) list.get(i)).getHomePhone(), ((TUser) list.get(i)).getHomeAddress(), ((TUser) list.get(i)).getJobPhone(), ((TUser) list.get(i)).getJobAddress(), ((TUser) list.get(i)).getEmail(), ((TUser) list.get(i)).getPostcode() }); mapList.add(cellMap); } return mapList; }
public TUserService getTUserService() { return this.tUserService; }
public void setTUserService(TUserService tUserService) { this.tUserService = tUserService; }
public TUser getTUser() { return this.tUser; }
public void setTUser(TUser tUser) { this.tUser = tUser; }
public List getTUserList() { return this.tUserList; }
public void setTUserList(List tUserList) { this.tUserList = tUserList; }
public int getTotal() { return total; }
public void setTotal(int total) { this.total = total; }
public int getPage() { return page; }
public void setPage(int page) { this.page = page; }
public List getRows() { return rows; }
public void setRows(List rows) { this.rows = rows; }}
<package name="tUser" extends="json-default"> <action name="*TUser" class="com.hengbao.test.action.TUserAction" method="{1}"> <result name="addSuccess" type="redirectAction"></result> <result name="updateSuccess" type="redirectAction"></result> <result name="deleteSuccess" type="redirectAction"></result> <result name="querySuccess" type="redirectAction"></result> <result name="json" type="json"> <param name="includeProperties"> total,page,rows.* </param> </result> <result name="success">/{1}TUser.jsp</result> </action> </package>spring代码如下:<bean id="tUserService" class="com.hengbao.test.service.imp.TUserServiceImp" />dao代码如下:<bean id="tUserDao" class="com.hengbao.test.dao.imp.TUserDaoImp" />web.xml代码如下:<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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>说明:(1)applicationContext.gebBean("tUserService") 可以取到正确的service,代表spring配置文件已经加载成功;(2)运行中没有报任何异常;
struts2 自动装配 service=null
答案:3 悬赏:70 手机版
解决时间 2021-05-24 23:03
- 提问者网友:缘字诀
- 2021-05-24 12:16
最佳答案
- 五星知识达人网友:千夜
- 2021-05-24 13:04
你没有 下载 struts-spring-plugin 的插件jar
全部回答
- 1楼网友:神的生死簿
- 2021-05-24 15:01
把错误贴出来看看呢~
- 2楼网友:人间朝暮
- 2021-05-24 13:25
你的action没有进行spring的配置
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯