惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - 逝者如斯(乎)

Redis和Memcached的区别详解 分布式系统原理与范型 - 电子支付系统 CSS3 (1) - Beginner Shell Script (2) - global.sh Python (1) - 7 Steps to Mastering Machine Learning With Python Java面试题(1)- 高级特性 Master Nginx(8) - Troubleshooting Techniques Master Nginx(7) - Nginx for the Developer Master Nginx(6) - The Nginx HTTP Server Hadoop实战课程 Self-Paced Training (3) - Docker Operations Self-Paced Training (2) - Docker Fundamentals Self-Paced Training (1) - Introduction to Docker Shell Script Tutorials (0 ~ 62) Master Nginx(5) - Reverse Proxy Advanced Topics Master Nginx(4) - Nginx as a Reverse Proxy Master Nginx(3) - Using the Mail Module Mater Nginx(2) - A Configuration Guide Master Nginx(1) - Installing Nginx and Third-Party Modules
Java EE (14) -- SSH配置
逝者如斯(乎) · 2016-04-03 · via 博客园 - 逝者如斯(乎)

整合SpringStruts1的三种方法总结

无论用那种方法来整合,第一步都是要装载spring的应用环境,有三种方式:

#1. struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
  "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

        <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>

    </plug-in>

</struts-config>

spring 的配置文件被作为参数配置进来。这样可以省略对web.xml 文件中的配置。确保你的applicationContext.xml WEB-INF目录下面

#2. web.xml

<web-app 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_3_0.xsd"

         version="3.0">

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/applicationContext.xml</param-value>

    </context-param>

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

</web-app>

#3. web.xml(低版本tomcat)

web.xml

<web-app 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_3_0.xsd"

         version="3.0">

    <servlet>

        <servlet-name>SpringContextServlet</servlet-name>

        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

    </servlet>

</web-app>

1.使用Spring ActionSupport

StrutsAction继承SpringActionSupport类,这样Struts 1.x就融入Spring框架中了。

Spring ActionSupport 继承至 org.apache.struts.action.Action ActionSupport的子类可以获得 WebApplicationContext类型的全局变量。通过getWebApplicationContext()可以获得这个变量。

public class LoginAction extends org.springframework.web.struts.ActionSupport {

    public ActionForward execute(ActionMapping mapping, ActionForm form,

            HttpServletRequest request, HttpServletResponse response) {

        LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub

        //获得 WebApplicationContext 对象

        WebApplicationContext ctx = this.getWebApplicationContext();

        LoginDao dao = (LoginDao) ctx.getBean("loginDao");

        if (dao.checkLogin(u)) {

            return mapping.findForward("success");

        } 

        else {

            return mapping.findForward("error");

        }

    }

}

applicationContext.xml

<beans>

    <bean id=”loginDao” class=”com.cao.dao.LoginDao”/>

</beans>

这中配置方式同直接在web.xml文件配置差别不大。注意:Action继承自 org.springframework.web.struts.ActionSupport 使得strutsspring耦合在一起。
但实现了表示层和业务逻辑层的解耦(LoginDao dao = (LoginDao) ctx.getBean(“loginDao”))


2.使用Spring DelegatingRequestProcessor 类。

DelegatingRequestProcessor  继承自 org.apache.struts.action.RequestProcessor 并覆盖了里面的方法。

sturts-config.xml 

<struts-config>

    <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>

</struts-config>

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="loginDao" class="com.cao.dao.LoginDao"/>

    <bean name="/login" class="com.cao.struts.action.LoginAction">

        <property name="dao"><ref local="loginDao"/></property>

    </bean>

</beans>

这里name=”/login”struts中的path匹配class=”com.cao.struts.action.LoginAction” struts中的type匹配还要为LoginAction提供必要的setXXX方法。获得ApplicationCotext和依赖注入的工作都在 DelegatingRequestProcessor中完成。


3.全权委托。

Action 的创建和对象的依赖注入全部由IOC容器来完成。使用SpringDelegatingAcionProxy来帮助实现代理的工作.
org.springframework.web.struts.DelegatingActiongProxy继承于org.apache.struts.action.Action.全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)

1) <action> type指向的是spring 的代理类

sturts-config.xml

<struts-config>

    <form-beans >

        <form-bean name="loginForm" type="com.cao.struts.form.LoginForm" />

    </form-beans>

    <action-mappings >

        <!– type指向的是spring 的代理类>

        <action

            attribute="loginForm"

            input="login.jsp"

            name="loginForm"

            path="/login"

            scope="request"

            type="org.springframework.web.struts.DelegatingActionProxy" >

            <forward name="success" path="/ok.jsp" />

            <forward name="error" path="/error.jsp" />

        </action>

    </action-mappings>

    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

        <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>

    </plug-in>

</struts-config>

2) 去掉struts-config.xml <controller >

三种整和方式中我们优先选用 全权委托的方式。
理由:
1,第一种使得过多的耦合了SpringAction .
2,RequestProcessor类已经被代理 如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。

总结一下:
整合工作中的步骤:
1.修改struts-config.xml
2. 配置applicationContext.xml
3.Action添加get/set方法 来获得依赖注入的功能。

Struts2Spring的整合

•Struts2框架为配合与Spring3框架进行整合,提供了相应的拦截器。

该组件名为StrutsSpringObjectFactory,位于struts2-spring-plugin-2.2.1.1.jar

通过在struts.xml中的声明,便可直接使用该组件以实现整合。

struts.xml

<struts>

7      <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>

8      <package name="strutsspring" namespace="/strutsspring" extends="struts-default">

9          <action name="login" class="LoginAction">

10             <result name="success">/success.jsp</result>

11             <result name="input">/index.jsp</result>

12         </action>

13     </package>

14 </struts>

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4         xmlns:context="http://www.springframework.org/schema/context"

5         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

7  

8      <bean id="LoginService" class="model.LoginService">

9      </bean>

10 

11     <bean id="LoginAction" class="controller.LoginAction" scope="prototype">

12         <property name="loginService" ref="LoginService"/>

13     </bean>

14 </beans>

Spring的事务模型

CMT事务属性

企业Bean 的默认事务边界管理设置是CMT。

1 @Stateless 

2 @TransactionManagement(CONTAINER)

3 public PayrollBean implements Payroll {

4     //..

5 }

@TransactionAttribute(TransactionAttributeType.MANDATORY) 

public void setBenefitsDeduction(int empId, double deduction) {...}