Note:
This manual is just a basic method of Environment configuration and rapid development for beginners, so that the majority of fans can get started quickly and take less of the detours I 've taken, there are no instructions on the java mode and other aspects. I am not able to discuss details about pattern design and development.
Suggestion:
If you do not know the basic principles of struts and hibernate, I hope you can first understand the relevant content.
Recommended:
The author of the hibernate Chinese manual thinks it is enough to learn hibernate. It covers almost all the details, but it may not be suitable for quick start.
Http://www.hibernate.org/hib_docs/v3/reference/zh-CN/html_single/
There is a lot of information about struts. Here we recommend a website that can download some tutorials.
Address: http://www.wnetw.com/jclub/index.jsp
It is strongly recommended that you learn the basic principles first! Otherwise, this article may not help you.
Download related tools: (Note the version)
Http://www.mysql.org mysql5.0
Eclipse 3.1.1 http://www.eclipse.org
Http://www.myeclipseide.com myeclipse4.0.3
Tomcat5.5
Installation:
We won't talk much about the installation of Tomcat and MySQL. It is best to ensure that your JDK version is 1.5 and configure your environment variables. Otherwise, you may encounter some problems.
Solve eclipse and install myeclipse. During installation, You need to specify the path to the eclipse just released. Because myeclipse is a paid software, you need to register it. However, Google can get a registration code based on Chinese characters :}
Development Environment deployment:
Now, make sure that your MySQL and tomcat services can be started normally, and myeclipse can be opened normally (if not, you can go to the relevant instructions or leave a message to the author ). Next we will start to deploy a project with the legendary Tomcat + Struts + hibernate + MySQL structure! (Faint! Preface I am so tired)
First, create a project in myeclipse. Right-click package exporler on the left and choose new> project...
In the displayed menu, choose myeclipse> J2EE projects> Web project.
Click Next To Go To The following screen:
Project name: Test
Click Finish.
Well, if it succeeds, you will see a new test project in package exporler! Now we need to configure the database. First create a database webases in your MySQL, and then create a new table admin in which the three fields are ID, name, in password, the ID is the primary key with the automatic value (MySQL can find the information for specific operations, not the scope of this article ).
Return to myeclipse and select Window> open perspective> other...
A menu named select perspective is displayed, and myeclipse databases exporler is selected in the menu. The following page is displayed.
After entering the relevant fields, click Finish to establish a database connection. Right-click the newly appeared JDBC for MySQL and select open connection ..., Click OK after confirming that the user name and password are correct. If everything goes well, you will see the following picture:
This indicates that you have established a correct connection with the database. Now let's go back to window-> open perspective-> other... Myeclipse, that is, the image we saw when we first came in.
Right-click the project test you just created and choose myeclipse-> Add struts capabilities... In the displayed menu, enter and confirm as follows:
Now you have added struts for your project. Right-click the project and choose myeclipse> Add hibernate capabilities... Click OK to add hibernate to your project. (For convenience, we put hibernatesessionfactory. Java under src/COM when selecting a path. In fact, we 'd better create a separate directory, such as src/COM/hibernate)
For better demonstration, we do not create a usual login page but a registration page. Select the hibernate. cfg. xml file under the src directory. Enter and save as follows. In this way, Hibernate establishes a database connection pool for you.
Next we choose webroot/WEB-INF/struts-config.xml file, right-click in the screen choose new-> form, action and JSP. Enter
Then select the JSP option, as shown below:
Finally, select finish.
Create a success. jsp page,
Right-click the Struts-config. xml file, select addadmin, select Properties, select forwords from the menu, and click Add, as shown in
Finally your struts-config.xml is like this:
Next we will go to hibernate. Switch to the page where we created the database just now, right-click your admin table and choose create hibernate mapping. Select the packaging path and click Finish.
The new file admindaofactory. Java in the path you just selected (src/COM/yourcompanyname/) and enter the following content:
Package com. yourcompanyname;
Import java. util. iterator;
Import org. hibernate. hibernateexception;
Import org. hibernate. query;
Import org. hibernate. Session;
Import org. hibernate. transaction;
Import com. hibernate. sessionfactory;
public class admindaofactory {
session;
transaction TX;
Public void add (Admin admin) throws hibernateexception {
/**
* creation date: 11-17-2005
* todo Add a new admin user.
* @ Param an object of admin
* @ return void
* @ author coder Guo
*/
try {
session = sessionfactory. currentsession ();
Tx = session. begintransaction ();
// Add a new Admin
session. save (Admin);
Tx. commit ();
}catch (hibernateexception e) {
throw E;
}finally {
If (TX! = NULL) {
Tx. rollback ();
}< br> sessionfactory. closesession ();
}< BR >}
Open addadminaction. Java under com. yourcompany. Struts. Action and add it. (If any error occurs, press Ctrl + Shift + O to automatically add the package)
Public class addadminaction extends action {
// ----------------------------------------------------------- Instance variables
// ----------------------------------------------------------- Methods
/**
* Method Execute
* @ Param Mapping
* @ Param form
* @ Param request
* @ Param response
* @ Return actionforward
* @ Author coder Guo
*/
Public actionforward execute (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response ){
Addadminform = (addadminform) form;
// Todo Add a new admin
Admin admin = new admin ();
Admin. setname (addadminform. getname ());
Admin. setpassword (addadminform. GetPassword ());
Admindaofactory admindao = new admindaofactory ();
Admindao. Add (Admin );
Return Mapping. findforward ("success ");
}
}
Open addadminform. Java under com. yourcompanyname. Struts. Form and modify it. (If an error occurs, follow the method described above to import the package)
Public actionerrors validate (
Actionmapping mapping,
Httpservletrequest request ){
// Todo auto-generated method stub
Actionerrors errors = new actionerrors ();
Session session = sessionfactory. currentsession ();
Transaction Tx = session. begintransaction ();
Query query = session. createquery ("Select Admin from Admin as admin where Admin. Name = '" + this. Name + "'");
Iterator it = query. iterate ();
If (it. hasnext ()){
Errors. Add ("addadmin. Err. Name", new actionmessage ("form. addadmin. Err. Name "));
}
TX. Commit ();
Sessionfactory. closesession ();
Return errors;
}
Public void reset (actionmapping mapping, httpservletrequest request ){
// Todo auto-generated method stub
This. Name = NULL;
This. Password = NULL;
}
Open applicationresource. properties under com \ yourcompanyname \ struts and add the error message here:
Form. addadmin. Err. Name = err
Finally, (sweat, so tired -_-!) Open addadmin. jsp and modify it as follows:
<% @ Page contenttype = "text/html; charset = UTF-8" %>
<% @ Page Language = "Java" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "Bean" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html" %>
<Script language = "JavaScript">
<! --
Function check (){
If (loginform. username. value = "" | loginform. Password. value = ""){
Alert ("Enter the complete information! ");
Loginform. username. Focus ();
Return false;
}
}
// -->
</SCRIPT>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML: HTML>
<Head>
<HTML: base/>
<Title> login. jsp </title>
<Link href = "CSS/webcss.css" rel = "stylesheet" type = "text/CSS">
<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">
</Head>
<Body>
<Center>
<P> & nbsp; </P>
<P> & nbsp; </P>
<Table width = "300" border = "0" cellpadding = "0" cellspacing = "0">
<HTML: Form Action = "/addadmin" Focus = "name" method = "get">
<Tr align = "center" valign = "Middle">
<TD colspan = "2" class = "typt_normal"> Add an administrator </TD>
</Tr>
<Tr>
<TD width = "100" align = "center" valign = "Middle" class = "typt_normal"> name: </TD>
<TD width = "200" align = "Left"> <HTML: Text property = "name" styleclass = "text_s"/> <HTML: errors property = "addadmin. err. name "/> </TD>
</Tr>
<Tr>
<TD width = "100" align = "center" valign = "Middle" class = "typt_normal"> password: </TD>
<TD width = "200" align = "Left"> <HTML: Password property = "password" styleclass = "text_s"/> </TD>
</Tr>
<Tr>
<TD colspan = "2" align = "center" valign = "Middle"> <HTML: Submit value = "Submit" onclick = "Return check (); "/> <HTML: reset value =" reset "> </Tr>
</Html: Form>
</Table>
</Center>
</Body>
</Html: HTML>
You can see how to use JavaScript in struts labels.
Configure the connection between myeclipse and tomcat. Set window-> preferences as follows:
Right-click the project file and choose "myeclipse"> "add and remove project deployment", for example:
Now, the configuration is almost complete. Start the Tomcat service on myeclipse.
Open the browser and enter
Http: // FIG: 8080/test/addadmin. jspYou can see your JSP page!