14.3 create a project mynews under eclipse

Source: Internet
Author: User
Tags tld

 

14.3 create a project mynews under eclipse

Create the project mynews under eclipse and configure spring and hibernate.

14.3.1 create a project under eclipse

(1) Run eclipse and select file → new → project. The new project dialog box is displayed in eclipse, as shown in 14.4.

(2) Select the Tomcat project under Java in the list box and click Next. The new Tomcat project dialog box is displayed, as shown in Figure 14.5.

Figure 14.4 new project dialog box Figure 14.5 new Tomcat project dialog box

(3) In the new Tomcat project dialog box, enter "mynews" in the Project Name text box, and click "finish". The project is successfully created. The directory structure of mynews is 14.6.

(4) log4j-1.2.9.jar, commons-logging.jar, spring. jar, anlr. jar, ASM. jar, spring-hibernate3.jar, asm-attrs.jar, cglib. jar, commons-collections.jar, dom4j. jar, ehcache. jar, JTA. jar, MySQL-connector-Java-
5.0.0-beta-bin.jar, hibernate3.jar these 14 jars are placed under/WEB-INF/lib/to copy them to the mynews/WEB-INF/lib directory, that is, classpath.

(5) use the Windows text editor to create a file log4j. properties, and put log4j. properties to mynews/
Under the WEB-INF/src directory.

(6) edit the log4j. properties file. The content is as follows:

Log4j. rootlogger = debug, stdout, R

Log4j.logger.org = error, A1

Log4j.logger.com. Gd = debug, A2

Log4j. appender. A1 = org. Apache. log4j. rollingfileappender

Log4j. appender. a1.file = org. Log

Log4j. appender. a1.maxfilesize = 500kb

Log4j. appender. a1.maxbackupindex = 50

Log4j. appender. a1.append = true

Log4j. appender. a1.layout = org. Apache. log4j. patternlayout

Log4j. appender. a1.layout. conversionpattern = % d {iso8601}-[% P] [% c {1}]-% m % N

Log4j. appender. A2 = org. Apache. log4j. rollingfileappender

Log4j. appender. a2.file = GC. Log

Log4j. appender. a2.maxfilesize = 500kb

Log4j. appender. a2.maxbackupindex = 50

Log4j. appender. a2.append = true

Log4j. appender. a2.layout = org. Apache. log4j. patternlayout

Log4j. appender. a2.layout. conversionpattern = % d {iso8601}-[% P] [% c {1}]-% m % N

# -------------------- Stdout --------------------------------

Log4j. appender. stdout = org. Apache. log4j. leleappender

Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout

# Pattern to output the caller's file name and line number.

Log4j. appender. stdout. layout. conversionpattern = [%-5 p] % d {yyyy-mm-dd hh: mm: SS} % C-% m % N

# -------------------- R --------------------------------

# Log4j. appender. r = org. Apache. log4j. rollingfileappender

Log4j. appender. r = org. Apache. log4j. dailyrollingfileappender

# This log file will be stored in web server's/bin directory, modify to your path which want to store.

Log4j. appender. R. File = GF. Log

# Log4j. appender. R. datepattern = '. 'yyyy-mm-dd-hh-mm

Log4j. appender. R. datepattern = '. 'yyyy-mm-dd

Log4j. appender. R. append = true

# Keep one backup file

Log4j. appender. R. layout = org. Apache. log4j. patternlayout

Log4j. appender. R. layout. conversionpattern = [%-5 p] % d {yyyy-mm-dd hh: mm: SS} % C-% m % N

#[%-5 p] % d {yyyy-mm-dd hh: mm: SS, SSS} method: % L % N % m % N

(7) Right-click mynews and select the Properties command from the shortcut menu. The properties for mynews dialog box is displayed, as shown in Figure 14.7.

(8) In the properties for mynews dialog box, select Java build path in the list box on the left of the dialog box.

(9) On the libraries tab, click Add jars... The jar selection dialog box is displayed, as shown in Figure 14.8.

Figure 14.7 properties for mynews dialog box figure 14.8 jar selection dialog box

(10) In the jar selection dialog box, open mynews in the list box until the lib directory appears 14 jar: log4j-1.2.9.jar, commons-logging.jar, spring. jar, anlr. jar, spring-hibernate3.jar, ASM. jar, asm-attrs.jar, cglib. jar, commons-collections.jar, dom4j. jar, ehcache. jar, JTA. jar, mysql-connector-java-5.0.0-beta-bin.jar, hibernate3.jar.

(11) press Ctrl, select the 14 jar files, and click OK to return to the properties for mynews dialog box, as shown in Figure 14.9.

Figure 14.9 properties for mynews dialog box

(12) Click the OK button in the properties for mynews dialog box to complete spring and hibernate configuration.

(13) Right-click mynews again and select the new → package command from the shortcut menu. The new Java package dialog box is displayed, as shown in Figure 14.10.

%Note:Spring. jar does not support hibernate. If you need hibernate, You need to include the spring-framework-2.0-m1/Dist/extmodules/spring-hibernate3.jar into classpath.

(14) In the Name text box of the new Java package dialog box, enter "com. Gd. Action" and click "finish" to create the com. Gd. action package.

(15) use the same method to create COM. GD. service package, Com. GD. service. impl package, Com. GD. dao package, Com. GD. dao. impl package and COM. GD. VO package, Com. GD. po package.

(16) Create JSP folders under the WEB-INF directory.

(17) The directory structure 14.11 of the mynews project of spring and Hibernate is configured.

Figure 14.10 new Java package dialog box figure 14.11 configure the directory structure of the mynews project of spring and Hibernate

14.3.2 compile the ant Build File

Create an ant file build. xml in the mynews directory. The sample code is as follows:

<? XML version = "1.0"?>

<Project name = "mynews" default = "init" basedir = ".">

<Property name = "mynews. Home" value = "."/>

<Property name = "mynews. lib" value = "$ {mynews. Home}/WEB-INF/lib"/>

<Property name = "mynews. Jar" value = "$ {mynews. Home}/WEB-INF/lib"/>

<Property name = "mynews. Classes" value = "$ {mynews. Home}/WEB-INF/classes"/>

<Property name = "tomcat. Home" value = "d // jakarta-tomcat-5.5.5"/>

<! -- <Property file = "build. properties"/> the preceding content can also be defined in build. properties. -->

<Target name = "init">

<Path id = "all">

<Fileset dir = "$ {mynews. Lib}">

<Include name = "**/*. Jar"/>

</Fileset>

/* Used to define the contained jar */

<Fileset dir = "$ {tomcat. Home}/common/lib">

<Include name = "*. Jar"/>

</Fileset>

</Path>

<Mkdir dir = "$ {mynews. classes}"/>

</Target>

<Target name = "clean">

<Delete dir = "$ {mynews. classes}">

</Delete>

</Target>

<Target name = "compile" depends = "init">

/* Defines the source file to be compiled and the compiled path */

<Javac srcdir = "$ {mynews. Home}/WEB-INF/src" destdir = "$ {mynews. classes}" target = "1.5">

<Classpath refID = "all"/>

</Javac>

</Target>

<Target name = "jar" depends = "compile">

/* Defines the name and path of the jar package */

<Jar jarfile = "$ {mynews. Jar}/GD. Jar" basedir = "$ {mynews. classes}" des = "com/GD/**">

</Jar>

</Target>

<! -- Package the mynews project into a war file -->

<Target name = "war" depends = "jar">

<War destfile = "$ {mynews. Home}/mynews. War" webxml = "$ {mynews. Home}/WEB-INF/Web. xml">

<Fileset dir = "$ {mynews. Home}" casesensitive = "yes">

<Include name = "WEB-INF/**"/>

<Exclude name = "*. War"/>

</Fileset>

<Lib dir = "$ {mynews. Home}/WEB-INF/lib">

<Include name = "*. Jar"/>

</Lib>

</War>

</Target>

</Project>

14.3.3 configure the Web. xml file

Create a web. xml file in the mynews/WEB-INF directory. The sample code for Web. XML is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.2 //" http://java.sun.com/j2ee/dtds/web-app_2_2.dtd ">

<Web-app>

......

<Servlet>

<Servlet-Name> dispatcherservlet </servlet-Name>

<Servlet-class> org. springframework. Web. servlet. dispatcherservlet </servlet-class>

<Init-param>

<Param-Name> contextconfiglocation </param-Name>

<Param-value>/WEB-INF/CONF/dispatcherServlet-servlet.xml </param-value>

</Init-param>

<Load-on-startup> 1 </load-on-startup>

</Servlet>

<Servlet-mapping>

<Servlet-Name> dispatcherservlet </servlet-Name>

<URL-pattern> *. DO </url-pattern>

</Servlet-mapping>

<Taglib>

<Taglib-Uri>/spring </taglib-Uri>

<Taglib-location>/WEB-INF/TLD/spring. TLD </taglib-location>

</Taglib>

......

</Web-app>

Code Description:

●/WEB-INF/CONF/dispatcherServlet-servlet.xml, for unified management, create a new folder conf under the WEB-INF, put the configuration file under/WEB-INF/CONF.

●/WEB-INF/TLD/spring. TLD, for unified management, create a folder TLD under the WEB-INF, put the custom tag file under/WEB-INF/TLD.

Related Article

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.