Today, with Maven, the Struts2, spring, hibernate integration is implemented.
There were a lot of mistakes in the middle. Most of these are caused by an error in the configuration file. To integrate here:
Note: Here we implement a login function. The user enters the user name and password from the JSP page, the server verifies its correctness, jumps to the different page according to the correct or not.
First, after the integration of the project structure:
You can see that the structure differs from the previous dynamic site project. All of the framework profiles in the MAVEN project are placed under the resources directory. The files in "resources" will eventually be copied to the project's web-inf/classes directory.
Also, you can see an error in your project in the project map. It looks like he's on the Java Resources directory. But you can also see that there are no errors in his subdirectory. In fact, the following error:
If you don't see the problems window, you can choose: Window-to-show view-->other. -General-->problems
And this problem does not affect compilation and packaging. It's like there's no such thing. Temporarily do not know how to fix (Quick fix is invalid).
All right. This is the project structure. The configuration of S2sh at the beginning of the following.
Let's look at the structure of the code:
Two Configuration of the Struts2:
- Add Struts2 Dependency
<!--struts2 Dependent--
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.2.1</version>
</dependency>
Yes. You have not read the wrong. Just write a dependency description for Struts2-core (here we use only the simplest features of STRUTS2, if you want to use more complex features. You will need to add other dependency declarations. Here our function is not so complicated, very simple. ), MAVEN will automatically download the dependent packages associated with it.
- Writing Actoin Transaction classes
Package org.feiruan.bookstore.struts;
Import Org.feiruan.bookstore.tool.HibernateTool;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.opensymphony.xwork2.ActionSupport;
public class Loginaction extends actionsupport{
Private String UserName;
Private String PASSW;
@Override
Public String Execute () throws Exception {
When you finish configuring spring, write again.
}
Public String GetUserName () {
return userName;
}
public void Setusername (String userName) {
This.username = UserName;
}
Public String GETPASSW () {
return PASSW;
}
public void Setpassw (String passw) {
THIS.PASSW = PASSW;
}
}
- Write the Struts.xml file, which should be placed under the resources directory in the MAVEN project:
<?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>
<package name= "Login" namespace= "/login" extends= "Struts-default" >
<action name= "Login" class= "Org.feiruan.bookstore.struts.LoginAction" >
<result name= "Success" >/success.jsp</result>
<result name= "Error" >/fail.jsp</result>
</action>
</package>
</struts>
- To write a landing page:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<form name= "Loginfrom" action= "<%=request.getcontextpath ()%>/login/login" method= "POST" >
Account: <input type= "text" name= "UserName"/>
Password: <input type= "password" name= "PASSW"/>
<input type= "Submit" value= "Submission"/>
</form>
</body>
The reason to put the page in the last write, because in writing the page must pay attention to the form Tag Action property value of the notation. (As above:)
Action= "<%=request.getcontextpath ()%>/login/login"
Or: action= "Login/login"
But for the sake of insurance. It is recommended to use the first notation to add the project path (Request.getcontextpath ()) to the path. If you use the second notation. Do not write this: action= "/login/login" path string in front do not add "/", return 404 error. If you add a "/", you are looking for "login/login" from the WebApps directory of Tomcat. No "/" means to find "login/login" starting from the current project root directory. So in order not to cause such errors, it is advisable to write the Request.getcontextpath () together. But if your landing page is an HTML page. Then you have to use the second way of writing, this time please pay more attention to it.
Also note is the name of the data to be submitted.
Account: <input type= "text" name= "UserName"/>
Password: <input type= "password" name= "PASSW"/>
To write to you. The value range in the Action class
Private String UserName;
Private String PASSW;
The names are exactly the same (including case, so use hump naming) and provide setter and getter methods for these ranges. and give the class an argument-free constructor.
Three Hibernate configuration:
1. Add Hibernate dependencies:
<!--hibernate dependent: Hibernate-core, SLF4J, Javassist--
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.15.0-GA</version>
</dependency>
Hibernate's dependence is not much, on three (Hibernate's official website tutorial on the use of three dependencies, we will follow his.) This is also the basic ORM feature that uses Hibernate only).
2. Write the Hibernate.cfg.xml file. Set Hibernate:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<session-factory>
<property name= "Connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Connection.url" >jdbc:mysql://localhost:3306/bookstoreDB</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" >root</property>
<property name= "dialect" >org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name= "Current_session_context_class" >thread</property>
<mapping resource= "UserInfoBase.hbm.xml"/>
</session-factory>
Each label is what to use do not ask me, basically look at the name also almost guessed it. If you can't guess, Baidu is a bit.
Besides, in our case. The database is using MySQL. So we also have to put the MySQL database link dependency library also in Pom.xml inside declare.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
3. Write the entity class (Userinfobase.java), which represents a single piece of data in the database:
Package org.feiruan.bookstore.util;
public class Userinfobase {
private int id;
private String name;
Private String PASSW;
Public Userinfobase () {
Super ();
}
public userinfobase (int ID, string name, String passw) {
Super ();
This.id = ID;
THIS.name = name;
THIS.PASSW = PASSW;
}
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GETPASSW () {
return PASSW;
}
public void Setpassw (String passw) {
THIS.PASSW = PASSW;
}
}
4. Write a UserInfoBase.hbm.xml that declares the mapping of the entity class to the data table.
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping dtd//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<class name= "Userinfobase" table= "User_info_base" >
<id name= "id" column= "id" >
<generator class= "native"/>
</id>
<property name= "name" column= "name"/>
<property name= "PASSW" column= "PASSW"/>
</class>
to here. Hibernate configuration is actually complete. Let's write a Hibernate tool class to find his password from the database based on the user name:
Package org.feiruan.bookstore.tool;
Import java.util.List;
Import Org.feiruan.bookstore.util.UserInfoBase;
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
public class Hibernatetool {
public string Getpasswordbyname (string name) {
Configuration config = new configuration (). Configure ();
Sessionfactory sessionfactory = Config.buildsessionfactory ();
Session session = Sessionfactory.opensession ();
Query query = Session.createquery ("From Userinfobase where Name= '" +name+ "'");
List List = Query.list ();
if (List!=null&&list.size ()!=0) {
Userinfobase info = (userinfobase) list.get (0);
return INFO.GETPASSW ();
}
return null;
}
}
Ok, last. Let's configure Spring.
Four Spring Configuration:
1. Add Spring dependencies:
<!--spring-dependent
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
2. Write the spring configuration file (Beans.xml), which is the simplest configuration and is not a DTD. Really useful on the line:
<?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:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<bean id= "Hibernatetool" class= "Org.feiruan.bookstore.tool.HibernateTool"/>
</beans>
Here we configure a bean, and the corresponding class is the Hibernate tool class we write.
3. Complete the Struts2 action class that was not completed earlier:
@Override
Public String Execute () throws Exception {
ApplicationContext context = new Classpathxmlapplicationcontext ("Beans.xml");
Hibernatetool tool = (Hibernatetool) context.getbean ("Hibernatetool");
if (Passw.equals (Tool.getpasswordbyname (userName))) {
return SUCCESS;
}else{
return ERROR;
}
}
End The configuration of the struts2+spring+hibernate is complete.
Then you can use MAVEN to package it and publish it.
Using Maven for STRUTS2 spring Hibernate integration