Fast integration of SSH projects with myeclipse

Source: Internet
Author: User
Document directory
  • 1. Prepare
  • 2. Struts Section
  • 3. Spring part
  • 4. hibernate
1. Prepare

Tool: myeclipse 6.0.1
GA
Mysql-connector-java-5.0.4-bin.jar
, MySQL
Gui tools 5.0
(It is not required to facilitate MySQL Database Management)

Environment: Tomcat 5.0
, MySQL
5.0

1.1. Create a project

Operation: [menu] File/New/WEB Project

Project name: Login

2. Struts Section


2.1. Added struts support


Operation: [menu] myeclipse/Project capabilities/Add struts
Capabilities

 

2.2. Create an actionform class

Operation: [Ctrl + N] myeclipse/Web-struts/struts 1.2 form

Class Name: loginform

On the "form properties" tab, add two attributes to loginform: username and password;

On the "jsp" option, select the "Create JSP form" option and change the new path
"/Login. jsp" (the login. jsp file will be automatically created ).

2.3. Create action class

Operation: [Ctrl + N] myeclipse/Web-struts/struts 1.2 Action

Class Name: loginaction

On the "form" tab, select "loginform" and "Input Source" for "name ".
"/Login. jsp ".

2.4. Create an index. jsp file

If no, create the index. jsp file and add a link to login. jsp: <
Href = "login. jsp"> login </a>
.

2.5. Create a forword class

Operation: [Ctrl + N] myeclipse/Web-struts/struts 1.2 forword

Class Name: indexforword

Enter "indexforword" for "name" and "path" for "/index. jsp ".

2.6. Modify the loginaction. Java File

Modify the execute method of the loginaction class:

Public class loginaction extends action
{

Public actionforward execute (actionmapping
Mapping, actionform form,
Httpservletrequest request, httpservletresponse response ){
Loginform = (loginform) form;
String
Username = loginform. GetUserName ();
String Password = loginform. GetPassword ();
If (username. Equals ("test") | password. Equals ("test ")){
Return Mapping. findforward ("indexforword ");
} Else {
Return Mapping. getinputforward ();
}

}
}

2.7. Modify the login. jsp file

Modify the <HTML: Form> label: <HTML: Form
Action = "/login">
.

2.8. Test

Operation: [menu] Run/run. Run the command in myeclipse server application mode.

(To perform the run operation normally, install Tomcat 5.5 first.
)

Click the "login" link on the index. jsp page to go To the login. jsp page. On the login. jsp page, enter
"Test/test", should be successfully logged on, and then jump to the index. jsp page; enter "test/123", should be kept in
The login. jsp page.

If the test is successful, structs runs normally.

3. Spring part


3.1. added spring support

Operation: [menu] myeclipse/Project capabilities/Add spring
Capabilities

Select "Spring 1" for spring version ″;

For the Development Kit (Libraries), select "Spring 1.2 AOP libraries, spring 1.2 Core
Libraries, spring 1.2 persistence core libraries, spring 1.2
Persistence JDBC libraries;

Select copy checked… For jar library Installation ..." , "Library folder"
Select "/webroot/WEB-INF/lib" (in this case, the required class libraries will be copied to the project directory for future deployment ).

Click "Next" to create the configuration file, modify the file path (folder) to "webroot/WEB-INF"
Directory (which can be managed together with the struts configuration file). The file name is called the default "applicationcontext. xml ".

Click Finish )".

3.2. Configure the struts-config.xml File

Add the spring plug-in (in <message-resources>
Label ):

<Plug-in
Classname = "org. springframework. Web. Struts. contextloaderplugin">

<Set-Property = "contextconfiglocation"
Value = "/WEB-INF/applicationcontext. xml"/>
</Plug-in>

Modify the loginaction configuration (you only need to modify the type attribute ):

<Action-mappings
>
<Action
Attribute = "loginform"
Input = "/login. jsp"
Name = "loginform"
Path = "/login"
Scope = "request"
Type = "org. springframework. Web. Struts. delegatingactionproxy
"
/>

</Action-mappings>

The green font is the modified content. Here we will use the spring proxy delegatingactionproxy
.

3.3. Modify the spring configuration file applicationcontext. xml

<? XML version = "1.0 ″
Encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en"
"Http://www.springframework.org/dtd/spring-beans.dtd">

<Beans>
<Bean name = "/login"
Class = "com. login. Struts. Action. loginaction"
Singleton = "false"> </bean>

</Beans>


The green font is the configuration content about accepting and processing action control. "com. login. Struts. Action. loginaction"
That is, the configuration in the original struts.

3.4. Test

Same test. The test proves that spring runs normally.

4. hibernate

In the hibernate section, modify the original Instance to use the database for user name/password verification.

4.1. Create a MySQL database and table

The code for adding a table is as follows:

Create Table user_table (
Id int not null auto_increment,
Username varchar (45) not null default ",
Password varchar (45) not null default ",
Primary Key (ID)
)


Add another record:

Insert into user_table (username, password)
Values ('test', 'test ')

4.2. Create a myeclipse database Driver (DB driver)

Operation: [menu] myeclipse/prefrences/myeclipse/Database
Explorer/database driver/DB brower

Select "new" and "driver name" from the menu of DB Brower and enter "login-Conn"
, "Connection URL", enter "JDBC: mysql: // localhost: 3306/test"
Enter the correct user name and password as needed;

Add mysql-connector-java-5.0.4-bin.jar in driver jars item"
(Can be downloaded from the Internet), select "com. MySQL. JDBC. Driver" in "Driver classname"
Other options.

Click Finish )".

4.3. Added support for the hibernate Function

Operation: [menu] myeclipse/Project capabilities/Add hibernate
Capabilities

Select "hibernate 3.1" for hibernate Specification ″
For the Development Kit (Libraries), choose hibernate 3.1 core libraries;

Select copy checked… For jar library Installation ..." , Select "library folder"
"/Webroot/WEB-INF/lib ".

Click "Next" to set the configuration file and select "Spring Configuration File ".
(Applicationcontext. XML )".

Set spring-hibernate for "Next" and select "existing spring"
Configuration file, sessionfactory ID, and sessionfactory ".

 

"Next" to create a data source object. In the bean Id field, enter datasource and select
"Use JDBC dirver", select "login-Conn" for the DB driver item, and fill the remaining items automatically;

Remember to select copy dB driver jar (s) to project and add to buidpath"
Project to facilitate future deployment.

"Next" creates the sessionfactory class, and the "Java package" item is set
"Com. login. hibernate" (if not, click "new" to add it), "class name"
Item to "sessionfactory", Java compliance
The Java version selected when level is selected and the project is created. (In this example, the sessionfactory class is not used for future extension)

Click Finish )".

4.4. Create an object relationship ing (ORM) File

Operation: [menu] window/open perspective/myeclipse Database
Explorer

Select the user_table table, right-click the table, and select "hibernate reverse engnieering" from the menu that appears"
.

In the displayed window, set "Java package" to "com. login ";

Select "hibernate Mapping File (*. HBM. XML) for each databases table"
And keep "Update hibernate ..." Item selected;

Select "Java Data Object" and keep "create abstract class" selected;

Leave the "base persistent class" item blank;

Cancel "Java Data Access Object ..." And "use M templates.

Click "Next", and then click "Next ".
On the engineering details page, select the "user_table" table and enter
"Com. login. User", and others remain unchanged.

Click Finish )".

After this operation is completed"
Create three files under the package: abstractuser. Java, user. Java, and user. HBM. xml.

4.5. Create the userdao. Java interface and userdaoimpl. Java class

Userdao and userdaoimpl access the database through hibernate.

Usedao. Java content is as follows:

Package com. login;

Public interface userdao {
Public Abstract Boolean isvaliduser (string username, string
Password );
}


The content of userdaoimpl. Java is as follows:

Package com. login;

Import java. util. List;
Import
Org. springframework. Orm. hibernate3.support. hibernatedaosupport;

Public class userdaoimpl extends
Hibernatedaosupport implements userdao {
Private Static string hql = "from user u where u. Username =?
";

Public Boolean isvaliduser (string
Username, string password ){
// Verify the user
List userlist = This. gethibernatetemplate (). Find (hql,
Username );
If (userlist. Size ()> 0 ){
Return true;
}
Return false;
}
}

4.6. Modify the loginaction. Java File

Use usedao object for verification:

Package
Com. login. Struts. Action;

Import
Javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. Apache. Struts. action. Action;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionforward;
Import org. Apache. Struts. Action. actionmapping;
Import com. login. Struts. Form. loginform;
Import com. login. userdao;

Public class loginaction extends action
{
Private userdao;

Public userdao
Getuserdao (){
Return userdao;
}

Public void
Setuserdao (userdao ){
This. userdao = userdao;
}

Public actionforward execute (actionmapping
Mapping, actionform form,
Httpservletrequest request, httpservletresponse response)
{

Loginform = (loginform)
Form;
String username = loginform. GetUserName ();
String Password = loginform. GetPassword ();

If (userdao. isvaliduser (
Username, password)
){
Return Mapping. findforward ("indexforword ");
} Else {
Return Mapping. getinputforward ();
}
}
}

The green font is the modified part.

4.7. Spring
Final
Preparation document
Applicationcontext. xml

<? XML version = "1.0 ″
Encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en"
"Http://www.springframework.org/dtd/spring-beans.dtd">

<Beans>

<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/test"> </property>

<Property name = "username"
Value = "root"> </property>

<Property name = "Password"
Value = "root"> </property>

</Bean>

<! -Configure sessionfactory,
Note the differences of the packages introduced here->
<Bean id = "sessionfactory"
Class = "org. springframework. Orm. hibernate3.localsessionfactorybean">

<Property name = "datasource">
<Ref local = "datasource"/>
</Property>
<Property
Name = "mappingresources">
<List>
<Value> COM/login/user. HBM. xml </value>

</List>
</Property>
<Property
Name = "hibernateproperties">
<Props>
<Prop
Key = "hibernate. dialect"> org. hibernate. dialect. mysqldialect </prop>

<Prop
Key = "hibernate. show_ SQL"> true </prop>

</Props>
</Property>
</Bean>

<Bean
Id = "transactionmanager"
Class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">

<Property
Name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
</Bean>

<Bean id = "userdao"
Class = "com. login. userdaoimpl">
<Property
Name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
</Bean>

<Bean id = "userdaoproxy"
Class = "org. springframework. transaction. Interceptor. transactionproxyfactorybean">

<Property
Name = "transactionmanager">
<Ref bean = "transactionmanager"
/>
</Property>
<Property name = "target">
<Ref local = "userdao"/>
</Property>
<Property
Name = "transactionattributes">
<Props>
<Prop
Key = "insert *"> propagation_required </prop>

<Prop
Key = "get *"> propagation_required, readonly </prop>

<Prop
Key = "is *"> propagation_required, readonly </prop>

</Props>
</Property>
</Bean>

<Bean name = "/login"
Class = "com. login. Struts. Action. loginaction"
Singleton = "false">
<Property name = "userdao">
<Ref bean = "userdaoproxy"/>
</Property>
</Bean>

</Beans

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.