Eclipse 搭建Spring Struts2

来源:互联网 发布:mysql update 阻塞 编辑:程序博客网 时间:2024/05/30 05:40

在上一章中写了,搭建struts2的配置,这次接着上一章的博客,集成spring。

上一章:http://blog.csdn.net/lipp555/article/details/50724996

项目结构:


1,首先导入spring的包。

2.创建接口,和实现接口的类

接口:LoginManager

package com.ss02.manager;public interface LoginManager {void loginVali(String name);}
实现类:LoginManagerImp

package com.ss02.manager.imp;import com.ss02.manager.LoginManager;public class LoginManagerImp implements LoginManager {@Overridepublic void loginVali(String name) {System.out.println("登录名:"+name);}}

3.修改Action的写法:

LoginAction

package com.ss02.action;import com.opensymphony.xwork2.ActionSupport;import com.ss02.manager.LoginManager;public class LoginAction extends ActionSupport {/** *  */private static final long serialVersionUID = -7752780747809187029L;LoginManager loginManager;@Overridepublic String execute() throws Exception {System.out.println("execute");loginManager.loginVali("lzz");return super.execute();}public String login() throws Exception {System.out.println("登录");loginManager.loginVali("--lzz--");return SUCCESS;}public LoginManager getLoginManager() {return loginManager;}public void setLoginManager(LoginManager loginManager) {this.loginManager = loginManager;}}


4.添加spring配置文件,在WEB-INF文件夹下,添加springResource文件夹,在springResource文件夹下添加:applicationContext.xml,applicationContext-login.xml

applicationContext.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"default-autowire="byName" default-lazy-init="true"><bean id="baseTransactionProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"abstract="true"><property name="transactionManager" ref="transactionManager" /><property name="transactionAttributes"><props><prop key="insertSynchronized*">PROPAGATION_REQUIRES_NEW</prop><prop key="insert*">PROPAGATION_REQUIRED</prop><prop key="create*">PROPAGATION_REQUIRED</prop><prop key="add*">PROPAGATION_REQUIRED</prop><prop key="update*">PROPAGATION_REQUIRED</prop><prop key="reset*">PROPAGATION_REQUIRED</prop><prop key="delete*">PROPAGATION_REQUIRED</prop><prop key="del*">PROPAGATION_REQUIRED</prop><prop key="save*">PROPAGATION_REQUIRED</prop><prop key="edit*">PROPAGATION_REQUIRED</prop><prop key="release*">PROPAGATION_REQUIRED</prop><prop key="modify*">PROPAGATION_REQUIRED</prop><prop key="change*">PROPAGATION_REQUIRED</prop><prop key="invoke*">PROPAGATION_REQUIRED</prop><prop key="submit*">PROPAGATION_REQUIRED</prop><prop key="batchImport*">PROPAGATION_REQUIRED</prop><prop key="flush*">PROPAGATION_REQUIRED</prop><prop key="excute*">PROPAGATION_REQUIRED</prop><prop key="replace*">PROPAGATION_REQUIRED</prop><prop key="export*">PROPAGATION_REQUIRED</prop><prop key="*">PROPAGATION_REQUIRED,readOnly</prop></props></property></bean></beans>
applicationContext-login.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"default-autowire="byName" default-lazy-init="true"><bean id="loginAction" class="com.ss02.action.LoginAction" scope="prototype"><property name="loginManager" ref="loginManager" /></bean><bean id="loginManager" class="com.ss02.manager.imp.LoginManagerImp"></bean><!--事务控制 <bean id="loginManager" parent="baseTransactionProxy"><property name="target"><bean class="com.ss02.manager.imp.LoginManagerImp"></bean></property></bean> --></beans>  

5.修改struts.xml中的文件,将struts托管给spring,将action中的class,改为spring中action的id:

<?xml version="1.0" encoding="UTF-8" ?>    <!DOCTYPE struts PUBLIC        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"        "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- struts的action配置文件 --><!-- 将action托管给spring --><constant name="struts.objectFactory" value="spring"></constant><!-- 所有的action都应该放在对应的package下 --><package name="ss" extends="struts-default" namespace="/lzz"><action name="login" class="loginAction"><!-- 定义逻辑视图和物理资源之间的映射 --><result name="success">/page/Come.jsp</result><result name="error">/page/Hello.jsp</result></action><action name="login_*" class="loginAction"method="{1}"><!-- 定义逻辑视图和物理资源之间的映射 --><result name="success">/page/Come.jsp</result><result name="error">/page/Hello.jsp</result></action></package></struts>  


6.在web.xml中添加spring的配置,放在struts配置前

  <!-- 初始化 --><context-param><param-name>contextConfigLocation</param-name><!-- 如果有多个文件,在文件之间用英文逗号隔开 --><!-- <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-db.xml </param-value> --><param-value>/WEB-INF/springResource/applicationContext.xml,/WEB-INF/springResource/applicationContext-*.xml</param-value></context-param>      <!-- 监听器 -->      <!-- 配置spring监听器 -->        <listener>            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>        </listener>  

配置结束。

0 0
原创粉丝点击