Integrate Struts2 with Spring

Source: Internet
Author: User
Tags apache tomcat

Recently, I have been thinking about a question. What is the difference between Huawei's BME framework and streaking Struts2 integrated Spring? These two methods are basically the same. Because the BME framework has been used for many times to develop the MTV Portal series projects, I think the BME framework integration of Spring and Struts2 is quite good and easy to use, as the source code of BME cannot be directly viewed, we decided to compare the similarities and differences between the BME framework and the direct use of Struts2 to integrate Spring, so as to deepen the learning and understanding of the Struts2 framework and Spring.

In Struts2, Action creation has been adjusted from thread security in Struts1 to non-thread security. Simply put, different requests in the same Action in Struts1 are processed by the same Action object, in Struts2, all requests in the same Action are processed using different objects of the same Action class. Each request creates a corresponding Action object.

The main problem to be solved when Struts2 integrates Spring is to use Spring to create Action objects and manage the business objects that Action depends on (managed by Spring) inject to create Action object.

I found some examples on the Internet and tried some experiments. However, many examples seem to be complete. In fact, the language is ominous and I have failed a lot. The detailed process of integrating Struts2 into Spring2.5 is as follows.

First, use eclipse to create a Web Project named SSH2. The main function of this Project is to display the logon page and log on after the user enters the user name and password, call the LoginService Service Interface in LoginAction to obtain user information. If the entered login account information is consistent with the user information returned in the LoginServiceImpl service implementation class, the logon success prompt page is displayed; if the entered login account is inconsistent with the user information in the business layer, the error page is displayed;

The project directory is as follows:

Note: All lib packages in the release package are copied. Of course, this has made me suffer a lot. Although the code and configuration information seem to be okay, it will lead to the failure to start the SSH2 project after it is released to Tomcat, even some inexplicable errors have been reported, making people confused.

The Web. xml file is as follows:<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <web-app id = "WebApp_9" version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <br/> <display-name> Struts Blank </display-name> <br/> <context-param> <br/> <param- name> contextConfigLocation </param-name> <br/> <param-value>/WEB-INF/classes/applicationContext. xml </param-value> <br/> </context-param> <br/> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> <br/> </listener> <br/> <filter-name> struts2 </filter-name> <br /> <filter-class> org. apache. struts2.dispatcher. filterDispatcher </filter-class> <br/> </filter> <br/> <filter-mapping> <br/> <filter-name> struts2 </filter-name> <br/> <url-pattern>/* </url-pattern> <br/> </filter-mapping> <br/> <welcome-file-list> <br /> <welcome-file> login. jsp </welcome-file> <br/> </welcome-file-list> <br/> </web-app>

The applicationcontext. xml file is configured as follows:<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <beans <br/> xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <br/> <bean id = "loginService" class = "com. ssh2.web. login. service. loginServiceImpl "/> <br/> <bean id =" loginAction "class =" com. ssh2.web. login. loginAction "> <br/> <property name =" loginService "ref =" loginService "> </property> <br/> </bean> <br/> </beans>

Struts2 uses the following two configuration files: struts. XML and SSH. XML, you can directly Mount ssh. the content of the XML file is directly merged into struts. in XML, two files are split to indicate more general conditions.

The Struts. xml file is configured as follows:<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <! DOCTYPE struts PUBLIC <br/> "-// Apache Software Foundation/DTD Struts Configuration 2.0/EN" <br/> "http://struts.apache.org/dtds/struts-2.0.dtd"> <br/> <struts> <br/> <constant name = "struts. enable. dynamicMethodInvocation "value =" false "/> <br/> <constant name =" struts. devMode "value =" false "/> <br/> <constant name =" struts. objectFactory "value =" spring "/> <br/> <include file =" ssh2.xml "/> <br/> <! -- Add packages here --> <br/> </struts>

The SSH. xml file shows the login configuration information we have discussed here:<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype struts Public <br/> "-// Apache Software Foundation/DTD struts configuration 2.0/EN" <br/> "http://struts.apache.org/dtds/struts-2.0.dtd"> <br/> <struts> <br/> <package name = "SSH2" namespace = "/SSH2" extends = "struts-Default"> <br/> <action name = "login" class = "loginaction" method = "login"> <br/> <result name = "success">/login. JSP </result> <br/> <result name = "error">/login. JSP </result> <br/> </Action> <br/> <action name = "dologin" class = "loginaction" method = "dologin"> <br/> <result name = "success">/main. JSP </result> <br/> <result name = "error">/error. JSP </result> <br/> </Action> <br/> </package> <br/> </struts>

Now struts2 has integrated the spring configuration file, and the other is the specific implementation code. The Source Code contains the following files: sshaction. java is the base class of all action classes. It inherits from actionsupport and implements sessionaware, servletrequestaware, and servletresponseaware to automatically inject response, request, and session. com. ssh2.web. login. bean. userinfo. java is the user information of the login user, including the user name, password, verification code three fields; COM. ssh2.web. login. service. loginservice. java is a business interface that defines how to obtain user information; COM. ssh2.web. login. service. loginserviceimpl. java is the implementation of the business interface loginservice. A user name is returned. Userinfo object of "zhangzk" and password; COM. ssh2.web. login. loginaction. java is the action used for login. It mainly includes the login () method to jump to the login page and dologin () method to verify whether the user name and password information submitted by the user from the login page are consistent with the name and password of the user information returned by our loginserviceimpl service interface. If they are consistent, the success prompt page is displayed, otherwise, the system jumps to the failure prompt page.

The com. ssh2.web. Common. sshaction. Java file is as follows:Package COM. ssh2.web. common; </P> <p> Import Java. util. map; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; <br/> Import Org. apache. struts2.interceptor. servletrequestaware; <br/> Import Org. apache. struts2.interceptor. servletresponseaware; <br/> Import Org. apache. struts2.interceptor. sessionaware; <br/> Import COM. opensymphony. xwork2.actionsupport; </P> <p> public class sshaction extends actionsupport implements sessionaware, <br/> servletrequestaware, servletresponseaware {<br/> private string message; <br/> private map ATT; <br/> private httpservletrequest request; <br/> private httpservletresponse response; </P> <p> Public void setsession (MAP arg0) {<br/> This. ATT = arg0; <br/>}</P> <p> Public void setservletrequest (httpservletrequest arg0) {<br/> This. request = arg0; <br/>}</P> <p> Public void setservletresponse (httpservletresponse arg0) {<br/> This. response = arg0; <br/>}< br/>}

The com. ssh2.web. login. Bean. userinfo. Java file is as follows:Package com. ssh2.web. login. bean; </p> <p> public class UserInfo {<br/> private String userName; <br/> private String password; <br/> private String validateCode; </p> <p> public String getUserName () {<br/> return userName; <br/>}</p> <p> public void setUserName (String userName) {<br/> this. userName = userName; <br/>}</p> <p> public String getPassword () {<br/> return password; <br/>}</p> <p> public void setPassword (String password) {<br/> this. password = password; <br/>}</p> <p> public String getValidateCode () {<br/> return validateCode; <br/>}</p> <p> public void setValidateCode (String validateCode) {<br/> this. validateCode = validateCode; <br/>}< br/>}

The com. ssh2.web. login. Service. loginservice. Java file is as follows:

Package com. ssh2.web. login. service; </p> <p> import com. ssh2.web. login. bean. userInfo; </p> <p> public interface LoginService {</p> <p> UserInfo getUserInfo (UserInfo userInfo); </p> <p>}

The com. ssh2.web. login. Service. loginserviceimpl. Java file is as follows:Package com. ssh2.web. login. service; <br/> import com. ssh2.web. login. bean. userInfo; </p> <p> public class LoginServiceImpl implements LoginService {</p> <p> public UserInfo getUserInfo (UserInfo userInfo) <br/>{< br/> UserInfo info = new UserInfo (); <br/> info. setUserName ("zhangzk"); <br/> info. setPassword ("zhangzk"); <br/> info. setValidateCode ("111111"); <br/> return info; <br/>}</p> <p>}

The com. ssh2.web. login. LoginAction. java file is as follows: Package com. ssh2.web. login; </p> <p> import com. ssh2.web. common. SSHAction; <br/> import com. ssh2.web. login. bean. userInfo; <br/> import com. ssh2.web. login. service. loginService; </p> <p> public class LoginAction extends SSHAction {</p> <p> private static final long serialVersionUID = 4125383395238086060L; </p> <p> private UserInfo evt = new UserInfo (); </p> <p> private LoginService loginService; </p> <p> public LoginService getLoginService () {<br/> return loginService; <br/>}</p> <p> public void setLoginService (LoginService loginService) {<br/> this. loginService = loginService; <br/>}</p> <p> public String login () {<br/> return SUCCESS; <br/>}</p> <p> public String doLogin () <br/>{< br/> UserInfo user = loginService. getUserInfo (null); <br/> if (user. getUserName (). equals (evt. getUserName () & user. getPassword (). equals (evt. getPassword () <br/>{< br/> return SUCCESS; <br/>}< br/> return ERROR; <br/>}</p> <p> public UserInfo getEvt () {<br/> return evt; <br/>}</p> <p> public void setEvt (UserInfo evt) {<br/> this. evt = evt; <br/>}< br/>}There are three JSP files used in the SSH2 project. login. jsp is the logon page, main. jsp is the page to jump to after successful logon, and error. jsp is the page to jump to after failed logon. The login. jsp file is as follows: <% @ Page contenttype = "text/html; charset = UTF-8 "%> <br/> <HTML> <br/> <pead> <br/> <title> SSH2 login </title> <br/> </pead> </P> <p> <body> <br/> <form action = "dologin. action "method =" Post "> <br/> <input type =" text "value =" Login Name: "name =" EVT. username "/> <br/> <input type =" text "value =" Login PWD: "name =" EVT. password "/> <br/> <input type =" Submit "value =" Logon "name =" Submit "/> <br/> </form> <br/> </body> <br/> </ptml> <br/>The main. jsp file is as follows: <% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" ISO-8859-1 "%> <br/> <% <br/> string Path = request. getcontextpath (); <br/> string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; <br/>%> </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <base href = "<% = basepath %>"> <br/> <title> SSH2 </title> <br/> </pead> <br/> <body> <br/> login success and enter into main page. <br> <br/> </body> <br/> </ptml> <br/>The error. jsp page is as follows: <% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" ISO-8859-1 "%> <br/> <% <br/> string Path = request. getcontextpath (); <br/> string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; <br/>%> </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <base href = "<% = basepath %>"> <br/> <title> SSH2 error </title> <br/> </pead> </P> <p> <body> <br/> Login Failed. <br> <br/> </body> <br/> </ptml>Publish the above SSH2 project to Apache Tomcat/5.5.23 and start Tomcat. The system reports the following error: Severe: Error getconfigured <br/> 2009-12-19 13:35:32 Org. apache. catalina. core. standardcontext start <br/> severe: context [/SSH2] startup failed due to previous errors <br/> 13:35:32 Org. apache. catalina. core. standardcontext stop <br/> information: Container Org. apache. catalina. core. containerbase. [Catalina]. [localhost]. [/SSH2] has not been startedThis error is generally caused by jar package problems, I repeatedly check my project, but can not find any errors, and later on the Net query, Someone prompted to delete the dropped package commons-attributes-compiler.jar, after I delete this package, I start Tomcat again. It works normally together. Access http: // localhost: 9000/SSH2/ssh2/login. action in the following way to see the logon page. Enter "zhangzk" and "zhangzk" on the logon page To Go To The success prompt page; enter "zhangzk" and "zhangzk" on the logon page to go to the error prompt page.

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.