首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

j2ee用@Resource诠注报空指针错

2013-07-01 
j2ee用@Resource注解报空指针错@Controller(/control/product/type/list)public class ProductTestActio

j2ee用@Resource注解报空指针错

@Controller("/control/product/type/list")
public class ProductTestAction extends Action {
@Resource(name="productTypeServiceBean")
private  ProductTypeService productTypeService;


@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
    productType type=productTypeService.find(productType.class, 3);
            System.out.print(type.getName());
            request.setAttribute("productType", type);
    return mapping.findForward("list");
}


在jsp页面
 产品名称:${productType.name } 

不出来内容,运行这句空System.out.print(type.getName());就报空指针错误

在junit.test测试中能出来
public class productTest {
private static ApplicationContext cxt;
private static ProductTypeService productTypeService;  
@BeforeClass
public static void setUpBeforeClass() throws Exception{
try {
cxt=new ClassPathXmlApplicationContext("beans.xml");
productTypeService=(ProductTypeService) cxt.getBean    ("productTypeServiceBean");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testFind(){
productType type=productTypeService.find(productType.class, 3);
System.out.print(type.getName());
}

谁知道这个问题怎么解决吗? @Resource 空指针错
[解决办法]
    productType type=productTypeService.find(productType.class, 3);
            System.out.print(type.getName());
这两个是执行哪句时报错,如果执行productType type=productTypeService.find(productType.class, 3);报错,那么productTypeService,没有注册进来,你跟进去看productTypeService实现类里,看每有什么问题,因为看的控制类没什么不对,。如果productTypeService实现类没有什么问题 你可以去掉resource的name,试试
如果System.out.print(type.getName());报错,那么是执行查询方法没有查询到数据
[解决办法]
肯定不能用Main直接跑啊。用单元测试

热点排行