Myeclipse+struts+hibernate Configuration Development

Source: Internet
Author: User
Tags commit config key mysql variables query stub window
Description

This manual is only for beginners to make the environment configuration and rapid development of some basic methods, so that the vast number of enthusiasts can quickly start, less to go through the detour, there is no Java model and other relevant instructions. I am limited in level and have no ability to discuss some of the details of pattern design and development.

Suggestions:

If you're not sure about some of the basics of struts and hibernate, I'd like to know something about it first.

Recommended:

Hibernate Chinese Manual The author thinks it's enough to learn hibernate, which contains almost all the details, but it may not be appropriate for a quick start.

Address:http://www.hibernate.org/hib_docs/v3/reference/zh-cn/html_single/

There's a lot of information about struts, and here's a Web site where you can download some introductory tutorials.

Address:http://www.wnetw.com/jclub/index.jsp

It is strongly recommended that the beginner's friends first understand the basic principles! Otherwise this article may not be of any help to you.

Related Tools Download: (Note version)

mysql5.0 http://www.mysql.org
Eclipse 3.1.1 http://www.eclipse.org
myeclipse4.0.3 http://www.myeclipseide.com
tomcat5.5

Installation:

There's nothing to say about Tomcat and MySQL installation, but it's a good idea to make sure your JDK is 1.5 and configure your environment variables, or you may experience problems.

Unlock Eclipse, then install the myeclipse that you just downloaded, and when you install it, you need to specify the path to the eclipse that you just undid, because MyEclipse is a billing software that requires registration. But generally according to Chinese habit, go to Google a registration code on it:}

Development environment Deployment:

OK, now make sure that your MySQL and Tomcat services are up and running, and that myeclipse can be turned on (if not, you can go to the relevant instructions or leave a message to the author). Here we are going to start to really start deploying a legendary TOMCAT+STRUTS+HIBERNATE+MYSQL structure project! (I'm so tired of faint! preface)

First, create a new project in MyEclipse. On the left side of the package Exporler, right click to select New->project ...

Select Myeclipse->j2ee projects->web Project in the pop-up menu.

Click Next to enter the following screen:


Project Name: Test

Click Finish after finishing.

Well, if you succeed, you'll see a new test project in Package Exporler! Now let's configure the database aspect first. First in your MySQL to build a database webases, and then a new table in the admin, where three fields are id,name,password of which ID is automatically value of the primary key (MySQL specific operation can find their own data, not the scope of this article).

Go back to MyEclipse, check Window->open perspective->other ...

You can see now out of a menu named Select Perspective, which selects MyEclipse Databases Exporler, and you can see now to the following page.

According to the above diagram input related fields and click Finish to establish a database connection, in the new JDBC for MySQL on the right button, select Open Connection ..., confirm the username and password after the correct click OK, if all goes well you will see the following screen:

This means that you have established the correct connection with the database. Now we go back to Window->open perspective->other ... MyEclipse, which is the picture we saw when we first came in.

Right-click the project you just created and select Myeclipse->add struts capabilities ... In the pop-up menu, follow the following input and determine:

OK, now that you've added struts to your project, then just like the top, select myeclipse->add Hibernate capabilities after the right key project ... Make sure to add hibernate to your project. (For convenience, we put Hibernatesessionfactory.java under src/com when we choose the path, but it's best to create a separate directory like src/com/hibernate)

To better demonstrate that we don't build the usual landing page, we build a registration page. Select the Hibernate.cfg.xml file under the SRC directory. Fill in and save as follows. So hibernate set up a database connection pool for you.

Next we select the Webroot/web-inf/struts-config.xml file, in the screen click the right button to select new->form, Action and JSP. Fill in the following

Then select the JSP option, as follows

Finally choose Finish.

Then create a new success.jsp page,

In just struts-config.xml file right select Addadmin Select Properties, select Forwords in the menu, then click Add, fill out the following figure

In the end, your struts-config.xml is the following:


Here we go to hibernate. In exchange for the page we set up the database just now, select the right mouse button of your admin to select Create Hibernate Mapping. Choose Finish after choosing a good packing path. As shown in figure:

Under the path you just selected (my convenience is src/com/yourcompanyname/) the newly created file Admindaofactory.java file and enter the following:

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 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 ();
  &NBSP}
   sessionfactory.closesession ();
 &NBSP}
 }

Then open the com.yourcompany.struts.action under the Addadminaction.java add (which if there are errors selected to press Ctrl+shift+o 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 = (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");
}

}

Then open the Com.yourcompanyname.struts.form under the Addadminform.java, modify (if there are errors according to the method mentioned 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;
}

Then open the com\yourcompanyname\struts under Applicationresource.properties to add an error message in this area:

Form.addadmin.err.name=err

Finally, (Khan, so tired ah-_-!) Open addadmin.jsp modified 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 ("Please enter the full information!") ");
LoginForm.userName.focus ();
return false;
}
}
-->
</script>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

<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 are my page" >

<body>
<center>
<p> </p>
<p> </p>
<table width= "border=" 0 "cellpadding=" 0 "cellspacing=" 0 ">
&LT;TR align= "center" valign= "Middle" >
&LT;TD colspan= "2" class= "Typt_normal" > Add admin </td>
</tr>
<tr>
&LT;TD width= "align=" "Center" valign= "Middle" class= "Typt_normal" > Name: </td>
&LT;TD width= "align=" "left" ></tr>
<tr>
&LT;TD width= "align=" "Center" valign= "Middle" class= "Typt_normal" > Password: </td>
&LT;TD width= "align=" "left" ></tr>
<tr>
&LT;TD colspan= "2" align= "center" valign= "middle" ></tr>
</table>
</center>
</body>


There's a way to see how to use JavaScript in the tabs of struts.

Configure the connection MyEclipse to Tomcat. Make the following setting in Window->preferences:

Right-click-> "MyEclipse"-> "Add and Remove Project Deployment" at the project file point, as shown in the following figure:

Well, our configuration work is basically over, and the Tomcat service is on the myeclipse.

Now open the browser and enter

  http://127.0.0.1:8080/test/addAdmin.jsp you will be able to see your JSP page!



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.