Tutorial on integrating s2sh framework based on myeclipse 8.0 ga

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

I. Experiment materials

1. Development Environment: myeclipse 8.0 Ga (eclipse3.5.1, jre1.6.0.10 and myeclipse enterprise workbench8.0)
2. runtime environment: JDK 6 Update 20 (jre1.6.0.13)
3. Development Framework: struts 2.1.8.1, spring 2.5, Hibernate 3.2
4. Application Server: Tomcat 6.0.26
5. Database Server: SQL Server 2005.

IIAnd preparations

1. myeclipse 8.0 ga download and install
Official Website: http://www.myeclipseide.com/
Latest Version: myeclipse 8.0 ga
This installation package integrates the automatic installation packages for eclipse3.5.1, jre1.6.0.10, and myeclipse enterprise workbench8.0 (only for Windows ).
The installation directory is c: \ J2EE \ myeclipse.
Myeclipse 8.0 ga Cracking Method: After installing myeclipse, decompress the patch to the current user directory (for example, if the current logon user is qxr, decompress it to c: \ Documents ents and Settings \ qxr, there is a prompt to start the plug-in deletion after cracking.

2. j2sdk download and install
Official Website: http://java.sun.com/
Latest Version: JDK 6 Update 20 (jre1.6.0.13)
① myeclipse 8.0 ga comes with a jre1.6.0.10 by default, however, installing an independent version is more helpful for learning.
② j2sdk(jdk-6u20-windows-i586.exe) the installation directory is j2sdk in c: \ J2EE \ j2sdk, and JRE in c: \ J2EE \ JRE, set system variables (my computer> Properties> advanced> environment variables):
(new) java_home = c: \ J2EE \ j2sdk
(new) classpath = .; c: \ J2EE \ j2sdk \ Lib \ DT. jar; C: \ J2EE \ j2sdk \ Lib \ tools. jar (Note: The dot number cannot be omitted, indicating the current directory)
edit the variable value of path, and add; % java_home % \ bin (Note: separated by semicolons)
at this point, j2sdk has been installed. Run the following command to test Java-version
③ configure JRE in Eclipse: click window-> preferences-> JAVA-> installed jres, click Add, and set J Re name is jre6, set JRE home directory to c: \ J2EE \ j2sdk, and click OK.

3. tomcat6 download and install
Official Website: http://tomcat.apache.org/
Latest Version: Tomcat 6.0.26
① Tomcat(apache-tomcat-6.0.26.rar) the installation directory is c: \ J2EE \ tomcat.
② Configure tomcat in eclipse
Click window-> preferences-> myeclipse-> servers-> tomcat-> Tomcat 6.x, and set the Tomcat server to enable. The paths are c: \ J2EE \ Tomcat and C: \ J2EE \ tomcat, c: \ J2EE \ Tomcat \ Temp, application.
Click window-> preferences-> myeclipse-> servers-> tomcat-> Tomcat 6. x-> JDK, and select j2sdk In the Tomcat JDK name drop-down box.

4. struts2 download
Official Website: http://struts.apache.org/
Latest Version: struts 2.1.8.1

5. SQL server2005 JDBC driver download and install
① Download driver Program
Http://download.microsoft.com, search for Microsoft SQL Server JDBC Driver 3.0 and download, the downloaded file name is sqljdbc_3.0.1301.101_chs.exe (Version 1.1 and later support sql2005, 2.0 support sql2008 ).
Download the downloaded file and decompress it. You do not need to install it. Find sqljdbc4.jar and put it in the $ myeclipsehome/dropins/jdbc directory.
② Configure the sql2005 server.
Configure TCP port: Start-> Program-> Microsoft SQL Server 2005-> Configuration tool-> SQL Server Configuration Manager-> SQL Server 2005 network configuration-> MSSQLServer protocol, enable TCP/IP.
In SQL Server Configuration Manager> SQL Server 2005 network configuration> MSSQLServer protocol, double-click TCP/IP and set all TCP port entries to the default port 1433 on the IP address tab.
SQL Server Configuration Manager-> SQL Server 2005 service to restart the SQL Server server, this completes the sql2005 server configuration.
③ Configuration in myeclipse 8.0 ga
Windows-> open perspective-> myeclipse Database Explorer, select new in dB browser, and set as follows:
Driver template = Microsoft SQL Server 2005
Driver name = SQL Server 2005
Connection url = JDBC: sqlserver: // localhost: 1433; databasename = SSH
User name = sa
Password = Password
Import sqljdbc4.jar from $ myeclipsehome/common/JDBC, select the check box Save Password, and click test driver to test the connection.

3.Build a framework

1. Create web project
Start myeclipse, click File> New> Other> myeclipse> JAVA enterprise projects> Web project, and then click Next.
Set Project name to s2sh, location dircetory to E: \ J2EE \ s2sh, J2EE specification level to Java EE 5.0, and click Finish.
Choose File> New> package to create the following packages:
Com. shawnqiu. Action
Com. shawnqiu. Bean
Com. shawnqiu. Dao
Com. shawnqiu. Dao. impl
Com. shawnqiu. Service
Com. shawnqiu. Service. impl
Com. shawnqiu. util
Dbconfig

2. Add struts 2.1.8.1 capabilities
Import struts2 core jar package (original path: Struts-2.1.8.1-all \ Struts-2.1.8.1 \ Lib), import location: s2sh \ webroot \ WEB-INF \ Lib, right-click the project name refresh to automatically add the project.
Commons-fileupload-1.2.1.jar
Commons-logging-1.0.4.jar
Freemarker-2.3.15.jar
Ognl-2.7.3.jar
Struts2-core-2.1.8.1.jar
Struts2-spring-plugin-2.1.8.1.jar
Xwork-core-2.1.6.jar

3. Add spring 2.5 capabilities
Right-click the project name, and choose myeclipse> Add spring capabilities.
Set spring version to spring 2.5, and select spring 2.5 AOP libraries, spring 2.5 core libraries, spring 2.5 persistence core libraries, spring 2.5 persistence JDBC libraries, and spring 2.5 web libraries. Select copy checked library contents to project folder (TLDs always copied) and click Next.
Click Next, Click Browse after the folder text box, in the pop-up dialog box, select spring configuration file storage location for webroot/WEB-INF, click Finish. Now spring has been loaded and the configuration file applicationcontext. XML has been generated under the WEB-INF folder.

4. Create Database Connection
Create a database named SSH in SQL Server 2005 and create a new data table userinfo in the database. The data items in the table are shown in, where userid is PK and auto-increment.
In myeclipse, choose Window> open perspective> myeclipse Database Explorer to go to The myeclipse Database Explorer view.
In the DB browser window, right-click and select new.
The connection URL is JDBC: sqlserver: // localhost: 1433; databasename = ssh. select Save Password. Click test after setting. Test the driver button, click next, and then click Finish.

5. Add hibernate 3.2 capabilities
Right-click the project name, and choose myeclipse> Add hibernate capabilities.
Select hibernate specification as Hibernate 3.2, select all packages, select copy checked library jars to project folder and add to build-path, and click Next.
Select spring configuration file (applicationcontext. XML) and click Next.
Select existing spring configuration file and click Next
You only need to enter an ID name in the bean ID text box as a substitute name for the database source, which is of little significance. Use the default settings. In the DB driver drop-down list, select the database source SQL Server 2005 created in myeclipse in the previous step. Other information is automatically filled in and click Next.
In the displayed dialog box, remove the create sessionfactory class? Check box. If this class is not created, click Finish.
During the creation process, the loaded jar package files may be duplicated. Therefore, if you are prompted that some jar packages are duplicated, select keep existing.

6. Delete conflictive jars
Right-click the project name and choose Properties> JAVA build path to delete the three repeated jar packages:
Asm-2.2.3.jar
Cglib-2.1.3.jar
Xerces-2.6.2.jar

7. Create struts. xml
Create a struts. xml file under the SRC package. The file content is as follows:

<! Doctype struts public "-// Apache Software Foundation // DTD struts configuration 2.0 // en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name = "struts. objectfactory "value =" Spring "/> <include file =" struts-default.xml "> </include> <package name =" default "extends =" struts-Default "> <action name = "userinfologin" class = "userinfoaction" method = "login"> <result name = "failure">/mainweb/failure. JSP </result> <result name = "success">/mainweb/success. JSP </result> </Action> </package> </struts>

8. Overwrite web. xml
Configure the Web. xml file with the following content:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-Name> contextconfiglocation </param-Name> <param-value>/WEB-INF/applicationcontext. XML </param-value> </context-param> <filter-Name> struts2 </filter-Name> <filter-class>
 
Org. Apache. struts2.dispatcher. Ng. Filter. strutsprepareandexecutefilter
</Filter-class> </filter> <filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern>/* </url-pattern> </filter-mapping> <welcome-file-List> <welcome-File> index. JSP </welcome-File> </welcome-file-List> <listener-class> Org. springframework. web. context. contextloaderlistener </listener-class> </listener> </Web-app>

9. Use hibernate reverse
Userinfo. Java

Userinfodaoimpl. Java

Userinfo. HBM. xml

Reverse generate the hibernate object class file from the userinfo table in the SSH database shown in the data source.

For example, do not generate abstract class, and click Next all the way.

Generate three files: userinfo. Java, userinfodao. Java, and userinfo. HBM. xml under com. shawnqiu. Bean. Rename userinfodao. Java to userinfodaoimpl. Java, place it in COM. shawnqiu. Dao. impl, and set userinfo. HBM. XML to dbconfig.

10. Create userinfoserviceimpl. Java

Package COM. shawnqiu. service. impl; import Java. util. *; import Org. springframework. orm. hibernate3.support. hibernatedaosupport; import COM. shawnqiu. bean. userinfo; public class userinfoserviceimpl extends hibernatedaosupport {// common user logon verification @ suppresswarnings ("unchecked") Public Boolean checkuser (string username, string userpassword) {Boolean flag = false; string hql = "from userinfo as userinfo where userinfo. username = '"
+ Username + "'and userinfo. userpassword = '"+ userpassword +"' "; List <userinfo> userinfolist = This. gethibernatetemplate (). find (hql); If (userinfolist. size ()> 0) {flag = true;} return flag ;}}

11. Create userinfoservice. Java
Right-click userinfoserviceimpl. Java and choose refactor-> extract interface.

Set interface name to userinfoservice, select all, and click OK.

As shown in, userinfoservice. Java and userinfoserviceimpl. Java are in the same package. Right-click userinfoservice. Java and choose refactor-> move

Select com. shawnqiu. Service and click OK.

12. Create userinfoaction. Java

Package COM. shawnqiu. action; import COM. opensymphony. xwork2.actionsupport; import COM. shawnqiu. service. userinfoservice; public class userinfoaction extends actionsupport {private string username; private string userpassword; Public userinfoservice getuserinfoservice () {return userinfoservice;} public void setuserinfoservice (userinfoservice) {This. userinfoservice = userinfoservice;} private userinfoservice; Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String getuserpassword () {return userpassword;} public void setuserpassword (string userpassword) {This. userpassword = userpassword;} Public String execute () {// string page = "failure"; // Boolean flag = false; // flag = personmanage. checkuser (username, userpassword); // If (FLAG) {// page = "success"; //} // return page; Return "" ;}public string login () {string page = "failure"; Boolean flag = false; flag = userinfoservice. checkuser (username, userpassword); If (FLAG) {page = "success";} return page ;}}

13. New JSP pages

Create a mainweb folder under webroot and create three files at the same time: Login. jsp, success. jsp, and failure. jsp,Source codeAs follows.

Login. jsp

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <% @ taglib uri = "/Struts-tags" prefix = "S" %> <HTML>  
 

Success. jsp

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>  
 

Failure. jsp

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>  
 

14. Overwrite applicationcontext. xml

 
    
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <bean id =" datasource "class =" org. apache. commons. DBCP. basicdatasource "> <property name =" driverclassname "value =" com. microsoft. sqlserver. JDBC. sqlserverdriver "> </property> <property name =" url "value =" JDBC: sqlserver: // localhost: 1433; databasename = ssh "> </property> <property name =" username "value =" sa "> </property> <property name =" Password "Value =" wxzy! @ % & Amp; "> </property> </bean> <bean id =" sessionfactory "class =" org. springframework. orm. hibernate3.localsessionfactorybean "> <property name =" datasource "> <ref bean =" datasource "/> </property> <property name =" hibernateproperties "> <props> <prop key =" hibernate. dialect "> Org. hibernate. dialect. sqlserverdialect </prop> </props> </property> <property name = "mappingresources"> <list> <value> dbconfig/userinfo. HBM. XML </value> </List> </property> </bean> <bean id = "userinfoserviceimpl" class = "com. shawnqiu. service. impl. userinfoserviceimpl"
Abstract = "false" lazy-init = "default" autowire = "default"
 
Dependency-check = "default" Scope = "prototype"> <property name = "sessionfactory"> <ref local = "sessionfactory"/> </property> </bean> <Bean id = "userinfoaction" class = "com. shawnqiu. action. userinfoaction"
 
Scope = "prototype"> <property name = "userinfoservice"> <ref bean = "userinfoserviceimpl"/> </property> </bean> </beans>

15. Create log4j. Properties

 # Set root category priority to Info and its only appender to console. log4j. rootcategory = info, console # log4j. rootcategory = info, console, logfile # Set the enterprise logger category to fatal and its only appender to lele.log4j.logger.org. apache. axis. enterprise = fatal, console # console is set to be a consoleappender using a patternlayout. log4j. appender. console = org. apache. log4j. leleappenderlog4j. appender. console. threshold = infolog4j. appender. console. layout = org. apache. log4j. patternlayoutlog4j. appender. console. layout. conversionpattern =-% m % N # logfile is set to be a file appender using a patternlayout. log4j. appender. logfile = org. apache. log4j. fileappenderlog4j. appender. logfile. file = axis. loglog4j. appender. logfile. append = truelog4j. appender. logfile. threshold = infolog4j. appender. logfile. layout = org. apache. log4j. patternlayoutlog4j. appender. logfile. layout. conversionpattern = %-4r [% T] %-5 p % C % x-% m % n 

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.