Myeclipse SSH (struts1 + spring + hibernate)

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

(Refer to"Struts + spring + hibernate exercises (complete)")

Download source code:Http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi? Fileid = 1, 2857703

1. Prepare

Tools: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.5,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

Replace "yourcompany" with "login ".

2.2. Create an actionform class

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

Class Name: loginform

After "login" is entered in "Use Case", name and form type are automatically filled.

On the "form properties" tab, add two attributes for loginform: username, password, "type", and "JSP input type" to keep the default "Java. lang. string and text ";

On the "jsp" option, select the "Create JSP form" option and change the new path to "/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" for "name", and enter "/login. jsp" for "Input Source ".

2.4. Create an index. jsp file

If not, create the index. jsp file and add a link to login. jsp: <a 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 select "/index. jsp" for "path ".

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 <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. Enter "test/test" on the login. jsp page. The logon succeeds and the index. jsp page is displayed. Enter "test/123", which should be kept on the login. jsp page.

If the test is successful, structs runs normally.

If an error occurs, see "5. Problem Set" at the end of the article ".

3. Spring part


3.1. added spring support

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

Select "Spring 1" for spring version ″;

For libraries, select spring 1.2 AOP libraries, spring 1.2 core libraries, spring 1.2 persistence core libraries, and spring 1.2 persistence JDBC libraries;

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

Click "Next" to create a configuration file, modify the file path (folder) to the "webroot/WEB-INF" Directory (to 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 (after the <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 use the spring proxy delegatingactionproxy to control the action.

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" is the configuration in the original struts.

3.4. Test

Same test. The test proves that spring runs normally.

If an error occurs, see "5. Problem Set" at the end of the article ".

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" and "connection URL" and enter "JDBC: mysql: // localhost: 3306/test, and then enter the user name and password of MySQL, as needed;

Add mysql-connector-java-5.0.4-bin.jar in driver jars, select com. MySQL. JDBC. Driver in driver classname, and select other options.

Click Finish )".

4.3. Added support for the hibernate Function

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

Choose hibernate 3.1 for hibernate specification and choose hibernate 3.1 core libraries for libraries;

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

Click "Next" to set the configuration file:

Select spring configuration file (applicationcontext. XML )".

Set spring-hibernate for "Next:

Select "existing spring configuration file" and input "sessionfactory ID ".

"Next" to create a data source object:

Enter datasource in the bean ID, select "use JDBC dirver" for the "datasource" item, and select "login-Conn" for the DB driver item. Other items are automatically filled.

Remember to select "Copy dB driver jar (s) to project and add to buidpath" to copy the database file connected to the project to facilitate future deployment.

"Next" creates the sessionfactory class:

Set the "Java package" item to "com. login. hibernate (if not, click "new" to add), change the "Class Name" item to "sessionfactory", and select the Java version for Java compliance level selection and Project Creation. (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

Right-click the user_table table and choose 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 "Configure reverse engineering details" Page, select the "user_table" table, enter "com. com. login. user.

Click Finish )".

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

4.5. Create the userdao. Java Interface

Operation: [Ctrl + N] interface, click "Next )"

In the new Java interface window that appears, set "source folder" to "login/src" and "package" to "com. login, set "name" to "userdao", and click "finish.

The content of userdao. Java is as follows:

Package com. login;

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

4.6. Create userdaoimpl. Java class

Operation: [Ctrl + N] class, click "Next )"

In the new Java class window that appears, set "source folder" to "login/src" and "package" to "com. login, set "name" to "userdaoimpl", and enter "org. springframework. orm. hibernate3.support. hibernatedaosupport, add "com. com. login. userdao interface, and then click Finish
.

Userdaoimpl accesses the database through hibernate for user verification.

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.7. 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.8. Final configuration file applicationcontext. xml of spring

<? 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 between 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>

4.9. Test

Same as the first test.

If an error occurs, see "5. Problem Set" at the end of the article ".

5. Problem Set

5.1. Console error message: java.net. bindexception: address already in use: jvm_bind: 8080

  • Cause: other processes occupy port 8080, leading to binding failure.
  • Solution: If Tomcat is started, close it and myeclipse will start Tomcat on its own.

5.2. HTTP Error message:Message
Servlet action is not available

  • Cause: the related class cannot be found. The path of some classes or files in the configuration file may be incorrect or the jar package is missing.
  • Solution: If it is generated after adding the spring feature, it may be that the spring. jar package is missing in/WEB-INF/lib. Search for version 1.2 spring. jar from the myeclipse directory and copy it to the/WEB-INF/lib/directory of the project.

5.3. Console error message: Java. SQL. sqlexception: Access denied for user: 'root @ localhost' (using password: Yes)

  • Cause: the database access is denied. The MySQL password may be set to null, but myeclipse does not support empty passwords.
  • Solution: Set the MySQL Root User Password to a non-empty password, such as "root", and then modify the password of the login-Conn data source added in myeclipse.

Verification Code: Change one

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.