Spring integrates the use of interception chains and annotations in Struts2

Source: Internet
Author: User

Among the three frameworks Spring, Hibernate, and Struts2, the Spring framework is at the core. Compared with the other two frameworks, Spring plays the role of a manager, it incorporates struts2 and hibernate configurations into its own management scope through injection to simplify struts. xml, hibernate. cfg. xml configuration file and the purpose of centralized management of beans. In fact, this management method is very similar to the interface method that we often use. They also give abstraction first and extend the specific implementation to achieve flexible configuration. This article mainly introduces three common aspects of spring and struts integration, namely the establishment of the spring and struts2 integrated environment, the use of intercept connections, and the introduction of Injection Using annotation methods. I. Establishment of spring and struts2 Integrated Environment (1) Introduction of jar package. introduce struts2 (struts-2.2.3) related jar package 1. commons-fileupload-1.2.2.jar 2. commons-io-2.0.1.jar 3. commons-lang-2.5.jar 4. freemarker-2.3.16.jar 5. javassist-3.11.0.GA.jar 6. ognl-3.0.1.jar 7. struts2-core-2.2.3.jar 8. struts2-spring-plugin-2.2.3.jar 9. xwork-core-2.2.3.jar B. introduce jar packages related to Spring (spring-framework-2.5.6. aspectjrt. jar 2. aspectjweaver. jar 3. cglib-nodep-2.1_3.jar 4. commo N-annotations.jar 5. commons-dbcp.jar 6. commons-logging-1.1.1.jar 7. commons-pool.jar 8. spring. jar (2) configure the web. xml file. Note the spring configuration file application. if the xml file does not have the corresponding path configuration, tomcat will load in the WEB-INF by default, but in general we will put all the configuration files in a unified folder for management, so take a look at the web. for example, we put all the xml configuration files in the config folder under src, therefore, the context configuration label (<param-name> contextConfigLocation </param-name> item) and <param-value> struts-default.xml, struts-plugin.xml, config/struts appear in the configuration file. xml </param-value> to define the configuration file loading path: [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Web-app xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "Xmlns =" http://java.sun.com/xml/ns/javaee "Xsi: schemaLocation =" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /Web-app_2_5.xsd "id =" WebApp_ID "version =" 2.5 "> <display-name> InterceptorTest </display-name> <context-param> <param-name> contextConfigLocation </param -name> <param-value> classpath: config/applicationContext -*. xml </param-value> </context-param> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener> <filter-name> struts2 </filter-name> <filt Er-class> org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter </filter-class> <init-param> <param-name> config </param-name> <param-value> struts-default.xml, struts-plugin.xml, config/struts. xml </param-value> </init-param> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/ * </url-pattern> </filter-mapping> </web-app> 2. Use of anti-leech. In the use of the interception chain, you should pay attention to the problem that whether the interceptor is used separately or the intercept chain is used, the Interceptor (or intercept chain) must be in) call the default Interceptor (or intercept chain ). The content in the struts. xml file using the interception chain is as follows: [html] <? 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> <constant name =" struts. action. extension "value = ", "> </constant> <package name =" test "namespace ="/"extends =" struts-default "> <interceptors> <interceptor name =" checkInterceptor "class =" checkInterceptor "> </interceptor> <interceptor-stack name =" myInterceptor "> <interceptor-ref name =" checkInterceptor "> </interceptor-ref> <! -- Make sure to call the default interception chain --> <interceptor-ref name = "defaultStack"> </interceptor-ref> </interceptor-stack> </interceptors> <! -- Used for Logon --> <action name = "login" class = "loginAction"> <result name = "success">/success. jsp </result> <result name = "error">/error. jsp </result> <result name = "checkSession">/checkSession. jsp </result> <interceptor-ref name = "myInterceptor"> </interceptor-ref> </action> <! -- Clear session --> <action name = "delSession" class = "delSession"> <result name = "back">/login. jsp </result> </action> </package> </struts> 3. Use of annotations. Annotation also provides a polymorphism mechanism, which is more similar to the expansion of interface functions. You can flexibly change the configurations of specific classes in the configuration file. The usage of annotations follows the same ing rules, that is, the variable names of classes using annotations must be the same as the bean name in the configuration file, and the first letter is lowercase. Details: [java] package com. action; import java. util. map; import javax. annotation. resource; import com. common. checkIsNull; import com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionSupport; import com. user. user; public class LoginAction extends ActionSupport {// usage of the annotation here @ Resource private CheckIsNull checkIsNull; private User user User; public user getUser () {return User;} publi C void setUser (User user) {this. user = user ;}@ Override public String execute () throws Exception {boolean flag = checkIsNull. isNull (user. getUsername (); if (flag) {if ("admin ". equals (user. getUsername () & "admin ". equals (user. getPassword () {Map session = (Map) ActionContext. getContext (). get ("session"); session. put ("username", user. getUsername (); return "success";} else {return "error" ;}} else {System. out. println ("neither user name nor password can be blank"); return "error" ;}} applicationContextbean. xml configuration file: [html] <? 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: context =" http://www.springframework.org/schema/context "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/context http://www.springframework.org/schema/context Spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx /Spring-tx-2.5.xsd "> <! -- Spring manages Struts2 Action --> <bean name = "loginAction" class = "com. action. loginAction "scope =" prototype "> </bean> <bean name =" checkInterceptor "class =" com. interceptor. myIntercepor "> </bean> <! -- The ing method of the application annotation --> <bean name = "checkIsNull" class = "com. common. checkPassword "> </bean> <bean name =" delSession "class =" com. action. delSession "> </bean> </beans> is the use of the interception chain and annotation in the use of Spring integrated struts2. In general, spring plays a major role in this process through the ing mechanism.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.