Web projects use both STRUTS2 and SPRINGMVC

Source: Internet
Author: User

Take over a project left over from the company's history to add new features to the legacy system. The original system uses only Struts2 to do web MVC, does not use spring to decouple, the data persistence layer uses the JDBC connection, the database connection pool is the c3p0. From the view of design, the original system abandons the independent service layer, merges the service layer and the data persistence layer, and the control layer (STRUTS2 action) calls the persistent Layer object directly. New features use SPRINGMVC and MyBatis.

Because the control layer of the original system and the new function uses different MVC, it intends to deploy two separate sets of systems, the new function as a standalone application, and the old system to invoke the new function through the URL. The solution needs to address the permissions issue, because some features need to be logged in to use, and some features are only available to some users. Users in the old system login, the new system can not access the user login session, need to solve the session sharing and synchronization problems. On the Internet, found that the session sharing implementation is more complex, and then abandon the program.

Back to the original point, to solve how to integrate the new functionality into the system, mainly to solve the coexistence of Struts2 and SPRINGMVC, the original system Struts2 interception path is as follows

<!--Struts2 configuration--><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><!--End Struts2 Configuration--

From the configuration of Web. XML, Struts2 intercepts all requests, and in the Struts.xml configuration file, in effect, the STRUTS2 only handles requests at the end of Do and action.

<!--set the suffix for Struts's action request, separated by commas when supporting multiple. --><constant name= "struts.action.extension" value= "Do,action"/>

Modify the configuration in Web. XML to have the STRUTS2 filter intercept only the path to do and action, and the modified STRUTS2 filter is as follows

<!--Struts2 configuration--><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>*.do</url-pattern> </filter-mapping><filter-mapping><filter-name>struts2</filter-name><url-pattern> *.action</url-pattern></filter-mapping><!--End Struts2 Configuration--

Modify the processing path of the SPRINGMVC and change the SPRINGMVC request to end with MVC, the configuration in Web. XML is as follows

<!--SPRINGMVC configuration--><servlet><servlet-name>springmvc</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name> contextconfiglocation</param-name><param-value>classpath:spring-mvc.xml</param-value></ init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping>< Servlet-name>springmvc</servlet-name><url-pattern>*.mvc</url-pattern></servlet-mapping ><!--End SPRINGMVC Configuration--

With the above configuration, Struts2 and SPRINGMVC respectively handle different requests, realizing the coexistence of Struts2 and SPRINGMVC. Simply modify the configuration file and a small amount of code to integrate the new functionality into the old system.

The complete Web. XML configuration is as follows

<!--spring configuration--><context-param><param-name>contextconfiglocation</param-name>< param-value>classpath:spring-context.xml</param-value></context-param><listener>< Listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener ><!--End Spring configuration--><!--encoding filter--><filter><filter-name>encodingfilter</filter-name ><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>< init-param><param-name>encoding</param-name><param-value>utf-8</param-value></ init-param><init-param><param-name>forceencoding</param-name><param-value>true</ param-value></init-param></filter><filter-mapping><filter-name>encodingfilter</ filter-name><url-pattern>*.mvc</url-pattern></filter-mapping><filter-mapping>< Filter-name>encodingfilter</filter-name><url-pattern>*.do</url-pattern></filter-mapping><filter-mapping>< filter-name>encodingfilter</filter-name><url-pattern>*.action</url-pattern></ Filter-mapping><filter-mapping><filter-name>encodingfilter</filter-name><url-pattern >*.jsp</url-pattern></filter-mapping><!--End encoding filter--><!--SPRINGMVC configuration--><servlet ><servlet-name>springmvc</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name> contextconfiglocation</param-name><param-value>classpath:spring-mvc.xml</param-value></ init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping>< Servlet-name>springmvc</servlet-name><url-pattern>*.mvc</url-pattern></servlet-mapping ><!--End SPRINGMVC configuration--><!--Struts2 configuration--><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>*.action</url-pattern ></filter-mapping><filter-mapping><filter-name>struts2</filter-name><url-pattern >*.do</url-pattern></filter-mapping><!--End Struts2 Configuration--

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Web projects use both STRUTS2 and SPRINGMVC

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.