J2EE development framework construction (2) and j2ee development framework construction

Source: Internet
Author: User

J2EE development framework construction (2) and j2ee development framework construction

1. Open the pom. xml file under the hqhop-framework-parent Project, add springmvc4, spring4, hibernate4, and the dependent packages and plug-ins of the druid data source. The dependent package version number

<! -- Data source related jar? --> <Dependency> <groupId> com. alibaba </groupId> <artifactId> druid </artifactId> <version >$ {druid. version }</version> </dependency> <! -- Web-related jar? --> <Dependency> <groupId> javax. servlet </groupId> <artifactId> javax. servlet-api </artifactId> <version >$ {servlet. version }</version> <scope> provided </scope> </dependency> <! -- Aspectj jar package --> <dependency> <groupId> org. aspectj </groupId> <artifactId> aspectjrt </artifactId> <version >$ {aspectj. version }</version> </dependency> <groupId> org. aspectj </groupId> <artifactId> aspectjweaver </artifactId> <version >$ {aspectj. version }</version> </dependency> <! -- Jar related to spring? --> <Dependency> <groupId> org. springframework </groupId> <artifactId> spring-orm </artifactId> <version >$ {spring. version }</version> <exclusions> <exclusion> <artifactId> commons-logging </artifactId> <groupId> commons-logging </groupId> </exclusion> </exclusions> </dependency> <groupId> org. springframework. data </groupId> <artifactId> spring-data-commons </artifactId> <version >$ {spring. data. commons. version} </v Ersion> </dependency> <groupId> org. springframework </groupId> <artifactId> spring-webmvc </artifactId> <version >$ {spring. version }</version> </dependency> <groupId> org. springframework </groupId> <artifactId> spring-context-support </artifactId> <version >$ {spring. version }</version> </dependency> <! -- Hibernate4 --> <dependency> <groupId> org. hibernate </groupId> <artifactId> hibernate-core </artifactId> <version >$ {hibernate. version} </version> </dependency>

Add the maven-compiler-plugin plug-in to the pom. xml file:

<build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.0</version><configuration><source>${jdk.version}</source><target>${jdk.version}</target><encoding>${project.build.sourceEncoding}</encoding></configuration></plugin></plugins></build>

Add the previous version information to the pom. xml file:

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><jdk.version>1.7</jdk.version><support.basedir>${project.parent.basedir}/src/support</support.basedir><site.basedir>${project.parent.basedir}</site.basedir><aspectj.version>1.7.4</aspectj.version>2. Add springmvc to the hqhop-framework-web project

Add the springmvc interceptor and the character encoding Interceptor to the web. xml file.

<! -- Set servlet Encoding to start --> <filter-name> Set Character Encoding </filter-name> <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> <async-supported> true </async-supported> <init-param> <param-name> encoding </param-name> <param-value> UTF-8 </param-value> </init-param> <param-name> forceEncoding </param-name> <param-value> true </param -value> </init-param> </filter> <filter-m Apping> <filter-name> Set Character Encoding </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <! -- Set servlet encoding end --> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: spring-mvc.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> <async-supported> true </async-supported> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Create a spring-mvc.xml file under the src/main/resources folder and add the following to the spring-mvc.xml file:
<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: p = "http://www.springframework.org/schema/p" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans. Xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <! -- Enable controller annotation support and scan controller --> <context: component-scan base-package = <span style = "color: # ff0000;"> "com. hqhop. framework. *. controller "</span> use-default-filters =" false "> <context: include-filter type =" annotation "expression =" org. springframework. stereotype. controller "/> <context: include-filter type =" annotation "expression =" org. springframework. web. bind. annotation. controllerAdvice "/> </context: compon Ent-scan> <! -- Automatically registers --> <mvc: annotation-driven enable-matrix-variables = "true"/> <mvc: view-controller path = "/" view-name = "redirect:/index"/> <! -- When. in xml, when DispatcherServlet uses <url-pattern>/</url-pattern> ing, it can map static resources --> <mvc: default-servlet-handler/> <! -- Static resource ing --> <mvc: resources mapping = "/static/**" location = "/WEB-INF/static/"/> <! -- ======================================= VIEW definition ================== =================--> <bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" viewClass "value =" org. springframework. web. servlet. view. <property name = "contentType" value = "text/html"/> <property name = "prefix" value = "/WEB-INF/jsp/"/> <property name = "suffix" value = ". jsp "/> </bean> </beans>

Finally, create a controller to test whether springmvc is successfully built:

Package com. hqhop. framework. web. controller; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. controller; import org. springframework. ui. model; import org. springframework. web. bind. annotation. requestMapping; import com. hqhop. framework. common. bind. annotation. currentUser; import com. hqhop. framework. shiro. entity. user; import com. hqhop. framework. shiro. service. userService; @ Controller <span style = "color: # ff0000;"> @ RequestMapping ("/index") </span> public class IndexController {<span style = "color: # ff0000; "> @ RequestMapping ("/test ") </span> public String test () {User user = new User (); user. setName ("silentwu333"); user. setPassword ("123456"); user. setAge (24); User u = userService. save (user); return "welcome"; // return the view name web-inf/jsp/webcome. jsp }}
Access address: http: // localhost: 8080/hqhop-framework-web/index/test

3. Add spring to hqhop-framework-web project

Add the following content to web. xml:

<! -- Start the Spring configuration file --> <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: spring-config.xml </param-value> </context-param> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener> <! -- End of Spring configuration file -->
Create a resources. properties under the src/main/resources folder and add the following content:

# Druid datasource druid data source configuration information # reference https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD% AE _DruidDataSource%E5%8F%82%E8%80%83%E9%85%8D%E7%BD%AEdruid.initialSize=10druid.minIdle=10druid.maxActive=50druid.maxWait=60000druid.timeBetweenEvictionRunsMillis=60000druid.minEvictableIdleTimeMillis=300000druid.validationQuery=SELECT 'X' druid. testWhileIdle = truedruid. testOnBorrow = falsedruid. testOnReturn = falsedr Uid. poolPreparedStatements = truedruid. Rule = 20druid. filters = wall, stat # druid. connectionProperties = config. decrypt = trueconnection. url = jdbc: mysql: // localhost: 3306/hqhop_framework? AutoReconnect = true & useUnicode = true & characterEncoding = utf-8connection.username = rootconnection. password = root # hibernate configuration information hibernate. show_ SQL = truehibernate. format_ SQL = falsehibernate. hbm2ddl. auto = updatehibernate. dialect = org. hibernate. dialect. mySQLDialect
Create an spring-config.xml under the src/main/resources folder

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xmlns: jpa = "http://www.springframework.org/schema/data/jpa" xsi: schemaLocation = "http://www.springframework.org/schema/bea Ns http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org /Schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd "> <context: property-placeholder location <span style =" color: # ff0000; "> =" classpath: resources. properties "</span>/> <! -- Enable AOP listening only for the current configuration file --> <aop: aspectj-autoproxy expose-proxy = "true"/> <! -- Scan annotation Bean --> <context: component-scan base-package = "<span style =" color: # ff0000; "> com. hqhop. framework. *, org. spring </span> "> <context: exclude-filter type =" annotation "expression =" org. springframework. stereotype. controller "/> </context: component-scan> <! -- Data source --> <bean id = "dataSource" class = "com. alibaba. druid. pool. druidDataSource "init-method =" init "destroy-method =" close "> <! -- Basic attribute url, user, password --> <property name = "url" value = "$ {connection. url} "/> <property name =" username "value =" $ {connection. username} "/> <property name =" password "value =" $ {connection. password} "/> <! -- Configure the initialization size, minimum, and maximum --> <property name = "initialSize" value = "$ {druid. initialSize} "/> <property name =" minIdle "value =" $ {druid. minIdle} "/> <property name =" maxActive "value =" $ {druid. maxActive} "/> <! -- Configure the timeout time for obtaining connections --> <property name = "maxWait" value = "$ {druid. maxWait}"/> <! -- Configure the interval for a test to detect idle connections to be closed, in milliseconds --> <property name = "timeBetweenEvictionRunsMillis" value = "$ {druid. timeBetweenEvictionRunsMillis} "/> <! -- Configure the minimum time for a connection to survive in the pool, in milliseconds --> <property name = "minEvictableIdleTimeMillis" value = "$ {druid. minEvictableIdleTimeMillis} "/> <property name =" validationQuery "value =" $ {druid. validationQuery} "/> <property name =" testWhileIdle "value =" $ {druid. testWhileIdle} "/> <property name =" testOnBorrow "value =" $ {druid. testOnBorrow} "/> <property name =" testOnReturn "value =" $ {druid. testOnReturn} "/> <! -- Enable PSCache and specify the PSCache size for each connection. If Oracle is used, set poolPreparedStatements to true and mysql to false. --> <Property name = "poolPreparedStatements" value = "$ {druid. poolPreparedStatements} "/> <property name =" maxPoolPreparedStatementPerConnectionSize "value =" $ {druid. maxPoolPreparedStatementPerConnectionSize} "/> <! -- Configure the monitoring statistic interception filters --> <property name = "filters" value = "$ {druid. filters}"/> <! -- <Property name = "connectionProperties" value = "$ {druid. connectionProperties} "/> --> </bean> <bean id =" sessionFactory "class =" org. springframework. orm. hibernate4.LocalSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "/> <property name =" packagesToScan "> <list> <value> com. hqhop. framework. *. entity </value> </list> </property> <property name = "hibernateProperties"> <props> <prop key = "hibernate. diale Ct ">$ {hibernate. dialect} </prop> <prop key = "hibernate. show_ SQL ">$ {hibernate. show_ SQL} </prop> <prop key = "hibernate. format_ SQL ">$ {hibernate. format_ SQL} </prop> <prop key = "hibernate. hbm2ddl. auto ">$ {hibernate. hbm2ddl. auto} </prop> </props> </property> </bean> <! -- Enable annotation transactions only for the current configuration file --> <tx: annotation-driven transaction-manager = "transactionManager" proxy-target-class = "true"/> <! -- Configure the Hibernate Transaction Manager --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate4.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> </beans>


So far, springmvc4 + spring4 + hibernate4 has been integrated.
















Java interface programming, j2ee development framework

Hello!
Interface-oriented programming means that the interaction between all classes or modules in the object-oriented system is completed by interfaces. Concepts are used for high generalization. The key is to understand the meaning. Explanation: A s = new B; create a B object in the memory heap, create A reference A in the memory stack, and A points to B. It is correct to use the s. print () method in A to call the method with the same name in B, because Class B implements the interface. The so-called interface-oriented programming can reduce the coupling between programs. It means that interfaces are used in specific calls and do not depend on specific classes. In A s = new B, B can be replaced by any other class that implements interface. In the IOC of spring, interface-Oriented Programming reduces the Coupling Degree between classes, which is a good embodiment. We suggest you study it by the way to deepen your understanding.

2. The most basic layer-3:
1 indicates the logic layer (displaying data ).
2. Business logic layer (data processing ).
3. Data Access logic layer (Data Access ).

Thank you!
 
In the J2EE development environment, what is the difference between the development frameworks of Myeclipse and J2EE, such as Hibernate, Spring, and Struts2?

Myeclipse is a development tool, while hibernate (a fully automated framework for data persistence), spring (IOC control invert AOP face-to-face, downcoupled), struts (control redirect) is a framework.

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.