实例代码分析
3. Spring与Struts框架整合
Spring,负责对象对象创建
Struts, 用Action处理请求
Spring与Struts框架整合,
关键点:让struts框架action对象的创建,交给spring完成!
步骤:
引入jar文件
1)引入struts .jar相关文件
2)spring-core 相关jar文件
3)spring-web 支持jar包
spring-web-3.2.5.RELEASE.jar 【Spring源码】
struts2-spring-plugin-2.3.4.1.jar 【Struts源码】
配置:
4)配置XML
struts.xml 【struts路径与action映射配置】
bean.xml 【spring ioc容器配置】
web.xml
【核心过滤器: 引入struts功能】
【初始化spring的ioc容器】
查看api。
4) 代码以及配置
dao
package com.cx.dao; /** * Created by cxspace on 16-8-9. */ public class UserDao { public void save(){ System.out.println("DB:保存用户"); } }
service
package com.cx.service; import com.cx.dao.UserDao; /** * Created by cxspace on 16-8-9. */ public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save(){ userDao.save(); } }
action
package com.cx.action; import com.cx.service.UserService; import com.opensymphony.xwork2.ActionSupport; /** * Created by cxspace on 16-8-9. */ public class UserAction extends ActionSupport{ private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public String execute(){ userService.save(); return SUCCESS; } }
bean配置
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userDao" class="com.cx.dao.UserDao"></bean> </beans> ================================================================ <?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userService" class="com.cx.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> </beans> ================================================================ <?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userAction" class="com.cx.action.UserAction"> <property name="userService" ref="userService"></property> </bean> </beans> ================================================================
struts配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" extends="struts-default"> <action name="user" class="userAction" method="execute"> <result name="success">/index.jsp</result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--struts配置--> <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> <!--spring配置 主要是初始化spring的ioc容器--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/bean-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
index.jsp
<%-- Created by IntelliJ IDEA. User: cxspace Date: 16-8-9 Time: 下午6:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> Spring-struts整合ok </body> </html>
时间: 2024-10-29 00:56:29