SSH2 architecture, annotation mode-diaosi version

Source: Internet
Author: User

The SSH architecture is really annoying, and there are many errors, either missing packages, repeated packages, or configuration errors. I spent a whole day organizing and building a runable project with the frameworks struts2.3, hibernate3.0, and spring3.1 Based on annotations, the database used is mysql, And the IDE is myeclipse (other options are acceptable ). The advantage of annotation is that you do not need to write many configuration files, and I like it too.

This project is just the most basic project that can run the ssh2 architecture. It does not contain other functions, so I call it the diaosi version. I will add some advanced functions to it later, so I will sell them first. Haha, I Don't Know What To Add ).

First, create an eclipse web project. The second step is to add a package. I will not go into detail here. Because I have not reached the level of greatness,

650) this. width = 650; "style =" float: right; "title =" 1.png" alt = "094140770.png" src =" http://www.bkjia.com/uploads/allimg/131229/12224a436-0.png "/>

You cannot explain each package carefully

So you can only add them in one brain. I will provide these packages.






















After the package is added, it is under the WEB-INF directory of the web. xml file ). Now configure the struts filter to intercept webpage requests. As follows:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><welcome-file-list><welcome-file>login.jsp</welcome-file></welcome-file-list><filter><filter-name>struts</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts</filter-name><url-pattern>*.action</url-pattern></filter-mapping></web-app>

After configuration, create a new Source Folder in the root directory. I will name IT resources to store the configuration file. The directory structure is as follows.

650) this. width = 650; "title =" 2.png" alt = "095445040.png" src =" http://www.bkjia.com/uploads/allimg/131229/1222495a0-1.png "/>

Create a struts. xml configuration file under the resources directory. Configure basic struts information. To use spring for management, add <constant name = "struts. objectFactory" value = "spring"/>

The Code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- Specify the default dataset set for the wb Application --> <constant name = "struts. i18n. encoding" value = "UTF-8"/> <! -- Specify the request Suffix of struts. The default value is action. --> <constant name = "struts. action. extension" value = "action"/> <! -- Sets whether the browser caches static content. The default value is true. It is best to disable the development environment --> <constant name = "struts. serve. static. browserCache" value = "false"/> <! -- Whether to automatically reload the file when the configuration file is modified. The default value is false. It is best to enable it during development --> <constant name = "struts. configuration. xml. reload "value =" true "/> <! -- Whether to allow dynamic method calls --> <constant name = "struts. enable. DynamicMethodInvocation" value = "true"/> <! -- Development Mode --> <constant name = "struts. devMode" value = "true"/> <! -- Integrate with spring --> <constant name = "struts. objectFactory" value = "spring"/> <! -- Size of the uploaded file --> <constant name = "struts. multipart. maxSize "value =" 10485760 "/> <include file =" ref/login. xml "/> </struts>

Struts is now basically configured. Configure spring now

Create a jdbc. properties file under the resources directory to save database information.

#mysqljdbc.Driver=com.mysql.jdbc.Driverjdbc.Url=jdbc:mysql://localhost:3306/testjdbc.username=gyhjdbc.password=passwdhibernate.dialect=org.hibernate.dialect.MySQLDialect##sql Server#jdbc.Driver=com.microsoft.sqlserver.jdbc.SQLServerDriver#jdbc.Url=jdbc:sqlserver://localhost:4462;DatabaseName=test#jdbc.username=sa#jdbc.password=123#hibernate.dialect=org.hibernate.dialect.SQLServerDialect

Here, I have added information about mysql and SQL server, with the # sign added in front of the comment.

Create a spring. xml file under the resources directory to configure spring. The content is as follows. Since hibernate is handed over to spring for management, you do not need to add a hibernate configuration file.

<? 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: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1. Xsdhttp: // response "> <! -- Use annotation --> <context: annotation-config/> <! -- Automatic scan package --> <context: component-scan base-package = "com. *"/> <! -- Introduce the parameter configuration file --> <bean id = "propertyConfigurer" class = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" location "value =" classpath: jdbc. properties "/> </bean> <! -- Configure the data source --> <bean id = "dataSource" class = "com. mchange. v2.c3p0. comboPooledDataSource "destroy-method =" close "> <property name =" driverClass "value =" $ {jdbc. driver} "/> <property name =" jdbcUrl "value =" $ {jdbc. url} "/> <property name =" user "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> <! -- The number of connections obtained during initialization. The value must be between minPoolSize and maxPoolSize. Default: 3 --> <property name = "initialPoolSize" value = "1"/> <! -- Minimum connections retained in the connection pool --> <property name = "minPoolSize" value = "1"/> <! -- Maximum number of connections retained in the connection pool --> <property name = "maxPoolSize" value = "300"/> <! -- Maximum idle time. It is discarded if it is not used within 60 seconds. The default value is 0, that is, never discard --> <property name = "maxIdleTime" value = "60"/> <! -- Check all idle connections in the connection pool every 60 seconds. Default: 0 --> <property name = "idleConnectionTestPeriod" value = "60"/> </bean> <! -- SessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.annotation. annotationSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "/> <property name =" packagesToScan "value =" com. * "/> <property name =" hibernateProperties "> <props> <prop key =" hibernate. dialect ">$ {hibernate. dialect} </prop> <prop key = "hibernate. show_ SQL "> true </prop> <prop key =" hibernate. query. factory _ Class "> org. hibernate. hql. ast. ASTQueryTranslatorFactory </prop> <prop key = "hibernate. cache. provider_class "> org. hibernate. cache. hashtableCacheProvider </prop> </props> </property> </bean> <! -- HibernateTemplate --> <bean id = "hibernateTemplate" class = "org. springframework. orm. hibernate3.HibernateTemplate "> <constructor-arg index =" 0 "> <ref bean =" sessionFactory "/> </constructor-arg> </bean> </beans>

After configuring the spring information, add the spring listener to the web. xml file. The modified web. xml content is as follows:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><welcome-file-list><welcome-file>login.jsp</welcome-file></welcome-file-list><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml</param-value></context-param><filter><filter-name>struts</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts</filter-name><url-pattern>*.action</url-pattern></filter-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>

Now, the Framework configuration is complete. Now you can add some functions to the test. Add data to the database first.

The Code is as follows.

mysql> use test;Database changedmysql> create table tab_user(-> id int primary key auto_increment,-> username varchar(20),-> password varchar(20)-> );Query OK, 0 rows affected (0.17 sec)mysql> insert into tab_user(username,password) values('gyh','passwd');Query OK, 1 row affected (0.09 sec)

Save the executed SQL file under the newly created update directory. Add some methods for testing after adding them,

The basic configuration is now complete. The following is the directory structure.

650) this. width = 650; "title =" 3.png" alt = "109902783.png" src =" http://www.bkjia.com/uploads/allimg/131229/1222496323-2.png "/>


Here is the project source code.

This article from "Sir fire?" blog, please be sure to keep this source http://88qlp88.blog.51cto.com/4346152/1334092

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.