今天改装一个无级树型菜单,用spring来管理事务,ssh架构的,其中struts的Action代码如下:首先做了一个BaseAction用来读取spring配置文件的bean定义
java 代码
- public class BaseAction extends Action {
- protected final Log log = LogFactory.getLog(getClass());
- private static final Long defaultLong = null;
- public ApplicationContext applicationContext;
- public Object getBean(String name) {
- ApplicationContext ctx =
- WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
- return ctx.getBean(name);
然后是处理控制的MenuManageAction extends BaseAction,选取其中的一段代码来看;
java 代码
- }else if (todoaction==null || "".equals(todoaction) || "list".equals(todoaction)){
- reeManager treeManager=(TreeManager)getBean("treeManager");
- list=treeManager.getRoots(SystemMenu.class);
- equest.setAttribute("objectlist",list);
- return mapping.findForward("list");
问题就出在这,TreeManager treeManager=(TreeManager)getBean("treeManager");居然 出现了ClassCastException!!!!!!!!!!!!!!!!!!! getBean方法虽然返回的是Object类型的但其实是个TreeManager类型的(见下面的配置文件),但就是一转型为TreeManager就得到ClassCastException,如果不转型得到的是Object类型的,但是就不能调用getRoot方法了,为了得到大家的帮助,我把其它相关代码也贴出来,spring配置文件如下:
xml 代码
- <bean id="treeManager" class="org.news.service.TreeManager">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- property>
- bean>
TreeManager.java如下:
java 代码
- public class TreeManager extends HibernateDaoSupport{
- public List getRoots(Class cls){
- return this.getHibernateTemplate().find(" from "+ cls.getName()+ " where enabled=true and parent_id is null order by id");