SSH integration based on MyEclipse6.5

Source: Internet
Author: User
Tags microsoft sql server 2005 jboss

1 . Purpose of writing

In order to learn, in order to learn Java better.

In order to want to learn this integration of people less detours!!!

2 . Experimental environment

L MyEclipse6.5

L JBoss4.2.1

L SQL2005

L Database Script Program

CREATE TABLE Student (

ID Int (one) not NULL auto_increment,

Name varchar (not-null),

Password varchar () default NULL,

PRIMARY KEY (ID)

);

Initial settings user name: Rush Password: 123456

Myeclipse,sql2005,jboss environment of the building here will not write, estimated that everyone should be very familiar with, and finally will say that JBoss configuration problems, will not be able to look at the Internet, there are many such information.

3 . Start creating a project

To open the MyEclipse, note that this should be in MyEclipse Java Enterprise view; Create a new Web project, enter the appropriate project name, here we enter SSH, where the EE specification level option, select Java EE 5.0,finish.

Once determined, the myeclipse generates a Web project named SSH, where the resulting directory structure is as follows:

4. Add Struts1.2 Framework Support

Right-click on the SSH project, and in the pop-up menu choose MyEclipse, add struts capabilities ..., adding struts support.

Select Struts 1.2 in the pop-up dialog box, modify the base pack for new classes to the required package name, the rest remains intact, and finish will

The structure after completion is:

5. Add Spring2.0 Framework Support

Right-click on the SSH project and select MyEclipse, add Spring capabilities ... in the popup menu, adding Spring Framework support

Because spring takes a minimal library release so that spring's library files are separated from each other, we need to choose the libraries we need, which we need to reference such as:

Select Next, select Browse in the window, select the Web-inf folder under SSH Project, and then Finsh.

6. Configure the data source

In the upper right corner of MyEclipse, select MyEclipse Database Explorer to open the Databases administration view.

Right-click on the DB browser on the left and select New ... to open the Database Driver dialog box.

In the Database Driver dialog box, follow the configuration below to select the DB driver. First select driver Template, where we select Microsoft SQL Server 2005. The Driver name is the driver name to use in the future configuration, which is used to differentiate the driver and ssh. Then fill in the Url,user name and password according to the actual situation. Click Add jars to attach the database driver file. For ease of configuration, you can save the password by choosing Save password. After configuration, finish can be completed.

7. Integration of Spring and hibernate

After the data source has been configured, hibernate support can be added. Switch to MyEclipse Java Enterprise view, right click on the SSH project, select MyEclipse, add Hibernate capabilities ... in the pop-up menu, adding hibernate support.

We choose Hibernate3.2, all according to the default settings, and then click Next;

Note: If you do not add only the first item.

Selecting Spring Configuration file in the dialog box indicates that we want hibernate to be managed by spring, which is the basis for integrating Hibernate with spring. then click Next;

In the dialog box that appears, select existing Spring configuration file. Since we have added the spring configuration file, we have chosen a configuration file that already exists. MyEclipse will automatically find the file that exists. Then, in the Sessionfactory ID, enter the name of the bean ID in Hibernate's mysessionfactory in the spring configuration file, where we can enter sessionfactory. then click Next;

In the Bean ID of the dialog box that appears, enter the name of the bean ID in spring for the data source, and here we enter DataSource. Then select the ssh,myeclipse that we just configured in the DB driver to automatically fill in the rest of the information into the form. then click Next;

In the dialog box that appears, cancel the Create Sessionfactory class. Click Finish.

At this point MyEclipse automatically opens the spring configuration file with the following file contents:

Represents a JDBC-based connection to a database

Represents a connection to a database using Jndi

Replace two packets, respectively, and, copy two packets to Web-inf/lib below, package storage address (root directory)

At this point, switch to the MyEclipse Database Explorer view, right-click to open the connection, right button on the data table you need to use, choose Hibernate Reverse Engineering .... Here we use the Student table that we just built in the test database.

In the Open dialog box, modify the Java src folder for our/ssh/src, here we need to select to the SRC directory, and need to fill in the Java package, which is the myeclipse generated class, we name it student. Then select Java Data Object to create the Pojo class. Then select Java Data Access Object. The Pojo class is the Java class corresponding to the database table, and the Jdo class is myeclipse automatically generated by some operations on the database. This will encapsulate some of the common operations and simplify our writing. Click Next after completing the completion.

In the dialog box that appears, select ID Generator as native and click Finish.

At this point the directory structure of the SSH project is as follows. The Student.java is a myeclipse-generated abstraction of a database table using the object-oriented Java language, Student.hbm.xml is the definition of mapping the properties of fields and Pojo classes in a database table, Studentdao.java encapsulates some myecl Ipse automatically generated operations on the database.

At this point we modify the file directory to make it better organized. We set up a DAO package that separates the DAO class from the Pojo class. We then set up an action and form package under the struts package to manage the action and form of struts. In order to decouple the implementation from the interface, we recommend adding the interface below the DAO package and then building the Dao.impl package to put the actual DAO class here. The DAO class is the class that deals with the database directly, in order to encapsulate the business logic, we write the business all in the service class, like DAO, we first set up the service package, inside Add the business interface, the concrete implementation put in Service.impl inside.

The following starts the configuration work:

(1) Create a new package, create a new interface in the package,

The interface implements the encapsulation of the Studentdao, which is the method used for encapsulation. The method encapsulated in this example is

(2) Create a new package and drag the Studentdao.java into the package

Modify the Studentdao method declaration as:

Note: When added implements Istudentdao will prompt the error, this is because Istudentdao package is not introduced, introduction method, mouse click Situdentdao, press CTRL+1, select the first item can be.

Modify the path error message in the Applicationcontext.xml, change the class to the modified package path Com.guet.function.dao.impl.StudentDAO, and correct the red error mark to disappear.

After the package is complete,

(3) Start the Servce package below

Create a new package, create a new interface in the package (interface, mentioned above), and the basic content is the method name in Istudentdao.

Later programming encounters another method, which continues to encapsulate.

(4) Start implementing the methods in service below

New package, New class

In the interface option, click Add, as follows

Enter Istudentservice, and click OK

After the new process is finished, click Finish, as

The framework of the method in Istudentservice is automatically generated and implemented in the framework to

Introduce the DAO encapsulation method into the file and implement its getter and setter methods

, if there is an error, click Istudentdao, use Ctrl+1, select the first item.

Implement getter and setter methods to make sure that they are automatically generated.

The last configuration is:

The final implementation file is:

(5) Implement the background business processing logic.

Adding spring snooping to Web. xml

Create a new folder under Web-inf Configxml, three new files in Configxml, respectively, Struts-config-student.xml (struts file), Applicationcontext-student-actions.xml (action file), Applicationcontext-student-beans.xml (bean file),

Where the Struts-config-student.xml file is initially configured as:

Applicationcontext-student-actions.xml,applicationcontext-student-beans.xml Initial Configuration Same

Note: Configxml files are essentially sub-files of Struts-config.xml and Applicationcontext.xml files, which are placed separately in a separate folder to facilitate management when the project grows.

Configure the path to create a new three XML file in Web.

The Struts path is:

The Spring path is:

Double click to open, click OK, in the blank right click on the new->form,action and JSP.

The first step is to create a form, named Student, and click Add to append the name of the value to be taken on the page.

Click Next to modify the configuration as follows

Where parameter is set as follows:

Click Finish to complete. The Action,form package is generated at this time.

(6) configuration file

The struts file is modified so that it is handed to the spring agent (home lit for modification)

Actions file configuration, which describes the implementation path of the action specific.

Beans file configuration, which describes the service, and the specific implementation path of the DAO file

(7) Implement the following page in index.jsp

The code is:

New in Webroot

The hello.jsp code is:

(8) Configure the jump in struts, the new two ForWord are:

All implementations are:

(9) Write background code in action

The specific implementation method is:

Method invokes the Loginquery method in the service, and the specific implementation of this method is:

(Note: This method is a concrete implementation, called by its encapsulated method, encapsulated in)

(10) Configure JBoss, add 2 files in JBoss, namely: Sqljdbc.jar,mssql.xml, the first file in the directory, the second in the directory, two file address

9. Ah, let's see if our work works.

A simple login operation is completed, the following test.

After starting JBoss, enter the following in the browser:

Enter the user name and password, click Login, if entered correctly, display as follows:

Otherwise display:

All right, all the work is done.

Big Zhizhi said step, because of the complexity, it is not as detailed as the previous several.

Configuring the Environment MyEclipse (connect MySQL)

Implement user registration and verification

1, set up the data table in MySQL, Id,username,password,email, the table name is user

2, new Web project, named SSH, first configure struts

3. Configure Spring

4. Configuring Database connections

5. Configure hibernate to be managed by spring

6. Add Pulgin Plugin

7, add the package, struts.action,dao,struts.form,vo,service,util,struts

8. Generating the mapping file

9. Add class file, implement insert database, configure XML file

10, join the JSP page Index registration interface, Success registration successful interface

11. Write the verification (for example, the user name is not empty, password is not empty, verify that two times the password is consistent, email format is correct, information verification to prevent registration machine registration)

12. Run the project by running Tomcat within MyEclipse. Complete!!

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.