@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空指针错
解决方案 »
- 求一个ajax调用java方法得例子
- java.lang.NoSuchMethodError: org.springframework.web.context.ConfigurableWebAppl
- java高手解决
- 用Java怎么调用xfire写的web service?
- 这样的SQL语句,不是很明白!(struts+hibernate)
- newxy新坐标已开源,源码可下载
- Tomcat到底怎么配置啊?刚才配置了一下,怎么我的Tomcat没打开,在浏览器中也能看到欢迎页面啊
- 关于两个对象的clob赋值问题???
- 急:XML在我的IE中无法解释
- 关于maven配置,环境变量配置好了,但是不能创建maven项目。。
- struts2中如何保持一个NIO的长连接?
- hibernate学习困惑???
private UserService userService;就行了,如果跑起来是空的话,那你去看看你的service的声明是不是有。
}@Service
@Transactional
public class ProductTypeServiceBean extends DaoSupport implements ProductTypeService {service 层是这样声明的
@Autowired 这个注解要用什么包吗,我引用不了。
System.out.print(type.getName());
这两个是执行哪句时报错,如果执行productType type=productTypeService.find(productType.class, 3);报错,那么productTypeService,没有注册进来,你跟进去看productTypeService实现类里,看每有什么问题,因为看的控制类没什么不对,。如果productTypeService实现类没有什么问题 你可以去掉resource的name,试试
如果System.out.print(type.getName());报错,那么是执行查询方法没有查询到数据
return mapping.findForward("list");
这个你放session试试,你跳转了,request没有了,要不用重定向
public static void main(String[] args) {
ProductTypeService productTypeService=new ProductTypeServiceBean();
productType type=productTypeService.find(productType.class, 5);
System.out.println(type);
}
报这个错误
Exception in thread "main" java.lang.NullPointerException
at com.itcast.service.base.DaoSupport.find(DaoSupport.java:48)
at com.itcast.web.action.product.ProductTestAction.main(ProductTestAction.java:34)这是DaoSupport48行报的错误:
@Transactional
public abstract class DaoSupport implements DAO{
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public <T> T find(Class<T> entityClass, Object entityid) {
return em.find(entityClass, entityid); //这是48行
}
是什么原因错的呢