struts2+spring+mybatis入门教程三之配置spring2
2014-01-10 23:35
381 查看
(1)将spring相关jar包及struts2-spring-plugin-2.0.11.2.jar拷贝到web-inf/lib下,在web.xml中设置spring监听器、配置文件路径。web.xml代码:
(2)在web-inf根目录下新建文件spring.xml,配置要扫描的包路径,及需要注册的实体bean。spring.xml代码:
(3)修改struts.xml,将com.xy6.DemoAction替换为demoAction。struts.xml代码:
(4)在包com.xy6下新建类DemoHelper.java,以测试spring的自动注入功能,由于该类使用@Component标记,则无需在spring.xml中注册该实体bean。DemoHelper.java代码:
(6)在DemoAction类中调用DemoHelper类的getCurYear()方法。DemoAction.java代码:
(7)重启weblogic域服务,浏览器中访问http://localhost:9001/web/demo.action,页面显示demo.jsp,控制台输出2014。如下图:
(8)至此,spring配置结束。
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>SSM</display-name> <welcome-file-list> <welcome-file>/demo/demo.jsp</welcome-file> </welcome-file-list> <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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
(2)在web-inf根目录下新建文件spring.xml,配置要扫描的包路径,及需要注册的实体bean。spring.xml代码:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 采用注释的方式配置bean --> <context:annotation-config /> <!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 --> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <!-- 配置要扫描的包 --> <context:component-scan base-package="com.xy6"></context:component-scan> <bean id="demoAction" class="com.xy6.DemoAction" /> </beans>
(3)修改struts.xml,将com.xy6.DemoAction替换为demoAction。struts.xml代码:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.objectFactory" value="spring"/> <package name="demo" extends="struts-default"> <action name="demo" class="demoAction" method="execute"> <result name="success">/demo/demo.jsp</result> </action> </package> </struts>
(4)在包com.xy6下新建类DemoHelper.java,以测试spring的自动注入功能,由于该类使用@Component标记,则无需在spring.xml中注册该实体bean。DemoHelper.java代码:
package com.xy6; import org.springframework.stereotype.Component; @Component public class DemoHelper { public String getCurYear(){ return "2014"; } }
(6)在DemoAction类中调用DemoHelper类的getCurYear()方法。DemoAction.java代码:
package com.xy6; import org.springframework.beans.factory.annotation.Autowired; import com.opensymphony.xwork2.ActionSupport; public class DemoAction extends ActionSupport { public static final long serialVersionUID = 1L; @Autowired private DemoHelper demoHelper = new DemoHelper(); public String execute(){ System.out.println("---curren year:"+demoHelper.getCurYear()); return "success"; } }
(7)重启weblogic域服务,浏览器中访问http://localhost:9001/web/demo.action,页面显示demo.jsp,控制台输出2014。如下图:
(8)至此,spring配置结束。
相关文章推荐
- struts2+spring+mybatis入门教程四之配置mybatis
- struts2+spring+mybatis入门教程五之配置log4j
- struts2+spring+mybatis入门教程六之配置json
- spring整合mybatis(入门级简单教程1)--在spring中配置c3p0,并成功测试
- SpringBoot 入门教程:集成mybatis,redis
- SpringBoot 入门教程:集成mybatis,redis
- MyBatis入门学习教程-优化MyBatis配置文件中的配置
- Java+Spring+MySql环境中安装和配置MyBatis的教程
- struts2+spring+mybatis入门教程七之常见问题
- Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)
- MyBatis 入门之+Spring 配置
- Spring整合Mybatis实现动态数据源切换教程配置
- Spring Boot教程(三十八)使用MyBatis注解配置详解(1)
- Spring+SpringMVC+Mybatis(SSM)框架搭建教程(三)-配置文件详解
- SpringBoot入门-21(springboot集成mybatis注解形式增删查改properties配置,利用@Provider实现动态SQL)
- MyBatis入门教程(三):优化配置文件
- spring cloud快速入门教程(三)配置中心spring cloud config
- Spring整合Mybatis实现动态数据源切换教程配置
- Spring整合Mybatis实现动态数据源切换教程配置
- Spring+SpringMVC+Mybatis(SSM)框架搭建教程(一)-Maven工具配置