I am a beginner at struts2, so I am not familiar with the struts2 framework process. This is my first integration program of struts2 + spring + hibernate, a simple login program, I am also a beginner and have encountered a lot of problems. After I finish this, I will release it immediately. I hope it will help my colleagues who are just learning struts2. Because I am also a cainiao and I will not be able to laugh when I make a shift. Okay, let's get started!
First, create a web project. The name here is SSH2. First, import the struts2 package to the Lib under the project. Here, we will describe that the package of HA struts2 should go to worker. Spring2.5 is directly imported in myeclipse. Since myeclipse has not integrated struts2 into its plug-in, struts2 needs to be imported manually. hibernate3.1 is also exported in myeclipse. During the export process, many packages may conflict with spring packages. This does not matter. Just run the keep command, then import the complete project package, and then we start our configuration work. at this time you may not find the packet conflict caused by the consequences, we found the problem later, here I do not detail the Packet Conflict error, I am prompted to delete the asm-2.2.3.jar directly, this package is caused by conflicting names between Hibernate and spring.
Next, we started our configuration work. First, we configured web. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5" 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_2_5.xsd>
<! -- Configure the spring listener -->
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value> classpath *: ORG/test/spring/applicationcontext *. xml </param-value>
</Context-param>
<! -- Enable listener -->
<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>
<! -- Configure opensessioninviewfilter, which must be before the struts2 listener -->
<Filter>
<Filter-Name> lazyloadingfilter </filter-Name>
<Filter-class>
Org. springframework. Orm. hibernate3.support. opensessioninviewfilter
</Filter-class>
</Filter>
<! -- Set listening to load context -->
<Filter>
<Filter-Name> struts2 </filter-Name>
<Filter-class>
Org. Apache. struts2.dispatcher. filterdispatcher
</Filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> lazyloadingfilter </filter-Name>
<URL-pattern> *. Action </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-Name> struts2 </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
</Web-app>
Then configure the applicationcontext. xml file:
<? 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.0.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<Bean id = "datasource"
Class = "org. Apache. commons. DBCP. basicdatasource">
<Property name = "driverclassname"
Value = "com. MySQL. JDBC. Driver">
</Property>
<Property name = "url" value = "JDBC: mysql: // localhost: 3306"> </property>
<Property name = "username" value = "root"> </property>
<Property name = "password" value = "root"> </property>
</Bean>
<Bean id = "sessionfactory"
Class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "datasource">
<Ref bean = "datasource"/>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect">
Org. hibernate. dialect. mysqldialect
</Prop>
</Props>
</Property>
<Property name = "mappinglocations">
<List>
<Value> classpath:/org/test/Vo/*. HBM. xml </value>
</List>
</Property>
</Bean>
<! --
| Transaction Management
-->
<Bean id = "transactionmanager" class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">
<Property name = "sessionfactory" ref = "sessionfactory"/>
</Bean>
<! -- Define a transaction using annotation -->
<TX: annotation-driven transaction-Manager = "transactionmanager"/>
</Beans>
Note: These configurations are the basis. In the early stage, you don't have to worry about their reasons. During the long development process, you will understand the meaning and convenience of these configurations.
Once again, the Struts. xml file is configured. You need to manually create this configuration file, as shown below:
<? 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> <! -- Strust2 version declaration -->
<Struts>
<Constant name = "struts. objectfactory" value = "Spring"/> // This field indicates that struts2 actions are managed by spring.
<Package name = "struts2" extends = "struts-Default">
<Action name = "login" class = "org. Test. Action. loginaction">
<Result name = "success">/result. jsp </result>
<Result name = "failer">/failer. jsp </result>
</Action>
</Package>
</Struts>
Finally, spring needs to manage all the logic inversion controls, so it is necessary to configure the DaO \ service \ action, which has been resident, as follows:
ApplicationContext-dao.xml files:
<? 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.0.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<Bean id = "itestdao" class = "org. Test. Dao. testdao">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
</Beans>
ApplicationContext-service.xml files:
<? 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.0.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<Bean id = "loginservice" class = "org. Test. Service. loginservice">
<Property name = "itestdao">
<Ref bean = "itestdao"/>
</Property>
</Bean>
</Beans>
ApplicationContext-action.xml files:
<? 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.0.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<Bean id = "login" class = "org. Test. Action. loginaction">
<Property name = "loginservice">
<Ref bean = "loginservice"/>
</Property>
</Bean>
</Beans>
Note: The above configuration file is actually the same as the configuration of struts1 + spring = hibernate, and the above configuration file and applicationcontext. XML is in the same folder. test. spring, the path is in the web. configuration already exists in XML.
I will paste all the code.
The database table used is as follows:
Drop table if exists 'user ';
Create Table 'user '(
'Userid' int (11) not null auto_increment,
'Username' varchar (20) not null,
'Password' varchar (16) not null,
'Email 'varchar (30) not null,
Primary Key ('userid ')
) Engine = InnoDB auto_increment = 2 default charset = utf8;
The Org. Test. VO package is as follows:
Package org. Test. VO;
/**
* User entity.
*
* @ Author myeclipse persistence tools
*/
Public class user implements java. Io. serializable {
// Fields
Private integer userid;
Private string username;
Private string password;
Private string email;
// Constructors
/** Default constructor */
Public user (){
}
/** Full constructor */
Public user (string username, string password, string email ){
This. Username = username;
This. Password = password;
This. Email = Email;
}
// Property accessors
Public integer getuserid (){
Return this. userid;
}
Public void setuserid (integer userid ){
This. userid = userid;
}
Public String GetUserName (){
Return this. Username;
}
Public void setusername (string username ){
This. Username = username;
}
Public String GetPassword (){
Return this. Password;
}
Public void setpassword (string password ){
This. Password = password;
}
Public String getemail (){
Return this. Email;
}
Public void setemail (string email ){
This. Email = Email;
}
}
The Org. Test. Dao package is as follows:
Package org. Test. Dao;
Public interface itestdao {
Public object query (string hql) throws exception;
}
Package org. Test. Dao;
Import java. util. List;
Import org. springframework. Orm. hibernate3.support. hibernatedaosupport;
Public class testdao extends hibernatedaosupport implements itestdao {
Public object query (string hql) throws exception {
List list = This. gethibernatetemplate (). Find (hql );
Return list;
}
}
The Org. Test. service package is as follows:
Package org. Test. Service;
Import java. util. List;
Import javax. servlet. http. httpservletrequest;
Import org. Test. Dao. itestdao;
Import org. Test. VO. user;
Public class loginservice {
Private itestdao;
Public void setitestdao (itestdao ){
This. itestdao = itestdao;
}
Public Boolean userlogin (User user) throws exception {
Boolean flag = false;
String name = user. GetUserName ();
String Pwd = user. GetPassword ();
String SQL = "from user as t where T. Username = '" + name
+ "'And T. Password ='" + PWD + "'";
List <user> List = (list <user>) itestdao. Query (SQL );
System. Out. println (SQL );
System. Out. println (list );
If (list! = NULL & list. Size ()> 0 ){
Return true;
} Else {
Return flag;
}
}
}
The Org. Test. action package is as follows:
Package org. Test. Action;
Import org. Test. Service. loginservice;
Import org. Test. VO. user;
Public class loginaction {
Private loginservice;
Private user;
Public user getuser (){
Return user;
}
Public void setuser (User user ){
This. User = user;
}
Public void setloginservice (loginservice ){
This. loginservice = loginservice;
}
Public String execute () throws exception {
Boolean flag = loginservice. userlogin (User );
If (FLAG)
{
System. Out. Print ("successfully ");
Return "success ";
} Else
{
System. Out. Print ("failure! ");
Return "failer ";
}
}
}
Login Page:
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path + "/";
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> my JSP 'login. jsp 'starting page </title>
<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->
</Head>
<Body>
<H1>
User Login
</H1>
<S: Form Action = "login. Action">
<S: textfield name = "user. username" label = "username"> </S: textfield>
<S: password name = "user. Password" label = "password"> </S: Password>
<S: Submit name = "Submit"> </S: Submit>
</S: Form>
</Body>
</Html>
Success page:
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "ISO-8859-1" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path + "/";
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> result </title>
<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->
</Head>
<Body>
Username: $ {requestscope. User. Username}
<Br>
Password: $ {requestscope. User. Password}
</Body>
</Html>
Failure page:
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "ISO-8859-1" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> my JSP 'failer. jsp 'starting page </title>
<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->
</Head>
<Body>
Result failer !!! <Br>
</Body>
</Html>
Note: After the above package is connected, the component becomes the project. After the Lib package is imported, it can run normally.