转载于:http://blog.sina.com.cn/s/blog_6151984a0100ko2y.html
1.环境 spring 2.5.6 struts 2.1.6 hibernate 3.3.2 JBOSS 4.2.2 GA JBOSS 5.0.0 GA |
2.spring 配置文件开启component-scan spring-hibernate-c3p0.xml: <context:component-scan base-package="com.machome"/> |
3.业务类加@Service标注,自动被扫描进spring bean pool @Service("stuService") @Transactional public class StuService_hibernate implements Stu_hibernate_intf { @Resource private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true) public List<Stu_hibernate> findAll(){ return sessionFactory.getCurrentSession().createQuery("from Stu_hibernate").list(); } |
4.Action类加@Controller标注,自动被扫描进spring bean pool,并采用@Resource注入前面扫描生成的业务bean @Controller("stuList") @Scope("prototype") public class StuList extends ActionSupport { private static final long serialVersionUID = 1L; @Resource(name="stuService") private Stu_hibernate_intf stuService; public void setStuService(Stu_hibernate_intf stuService) { this.stuService = stuService; } @Override public String execute() throws Exception { List<Stu_hibernate> sList = stuService.findAll(); ActionContext.getContext().put("testList",sList); return SUCCESS; } } |
5.struts2 配置 struts.xml <package name="example" namespace="/example" extends="com.machome.struts2.core" > <action name="stuList" class="stuList"> <!-- class name为spring bean name --> <result type="dispatcher">/jsp/stu_list.jsp</result> </action> |
6.前端jsp文件 /jsp/stu_list.jsp : <body> <% List<Stu_hibernate> testList=(List<Stu_hibernate>)request.getAttribute("testList"); for(Stu_hibernate temp:testList) out.println(temp.getStu_id()+":"+temp.getStu_name()+"<br />"); %> </body> |
7. 项目web.xml配置 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- ##################### 初始化spring容器 ###########--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-hibernate-c3p0.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- ###################### struts2 过滤器 ############ --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- ################################################# --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
8.访问Action http://localhost:8080/A_ssh2_basic/example/stuList |
9.结果: 1.JBOSS 4.2.2 GA测试通过 2.JBOSS 5.0.0 GA,5.1.0 GA , 出错: Unable to instantiate Action, stuList, defined for 'stuList' in namespace '/example'stuList from BaseClassLoa root cause java.lang.ClassNotFoundException: stuList from BaseClassLoader@114e72e{VFSClassLoaderPolicy@1557f9d{name=vfsfile:/E:/jboss-5.0.0.GA/server/default/deploy/A_ssh2_basic.war/ |
1.下载jboss-spring-int-vfs.jar,加入build path (在此处下载: http://jira.springframework.org/browse/SPR-5120) 该jar包内包含两个类: VFSClassPathXmlApplicationContext VFSXmlWebApplicationContext |
2.web.xml添加下列语句 <!-- ##################### 初始化spring容器 ###########--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-hibernate-c3p0.xml</param-value> </context-param> <context-param> <param-name>contextClass</param-name> <param-value> org.jboss.spring.vfs.context.VFSXmlWebApplicationContext </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- ###################### struts2 过滤器 ############ --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
3.重新再JBOSS 5.0.0 GA下访问 http://localhost:8080/A_ssh2_basic/example/stuList,通过 |