The Struts2+spring+mybatis framework built under MAVEN management

Source: Internet
Author: User
Tags aop
STEP1, create a new MAVEN project next step, select Maven-archetype-webapp
Next:
It was supposed to be a SSM .... Finish finished. Now the directory structure is like this

Next: Add source Folder:src/main/java, Src/test/java, src/test/resources
Right-click Engineering, build path, configure build path, and modify the output path of the source file
Then configure Project Facets
Click "Convert to Faceted" from ...
OK, start the project, enter http://localhost:8080/S2SS-demo/in the browser


STEP2: Adding the MAVEN framework first adds the MAVEN-dependent jar pack to the Pom.xml file. <!--add struts dependencies--> <dependency> <groupId>org.apache.struts</groupId>   ;artifactid>struts2-core</artifactid> <version>2.3.15.1</version> </dependency> Then add the Struts.xml file under the Resource folder.
Then add struts configuration to the Web.xml file
Feel no problem, also configured in the Web.xml welcome-file=index.jsp, but access http://localhost:8080/S2SS-demo/will be an exception, There is no Action mapped for namespace [/] and action name [] associated with the context path [/s2ss-demo], http://localhost:8080/S2SS-demo/index.jsp can be normal Visit, should be by struts intercept, did not find the broken method. Useraction defines two methods, Login_input & login. In the Struts2 action, name can use wildcard characters. For example <action name= "*user" class= "com.s2ss.demo.UserAction" method= "{1}", You can match any action method in the user-terminated class com.s2ss.demo.UserAction.
Index.jsp page is like this
step3: Adding the Spring framework and struts thinking almost .... 1, first add spring-dependent dependencies in the Pom.xml file. <!--add Spring dependencies-->     <dependency>   <groupId>org.springframework</groupId> & nbsp <artifactId>spring-core</artifactId>   <version>${spring.version}</version>   </dependency>    <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-beans</artifactId>   <version>${spring.version}</version>  </dependency>    <dependency>   <groupid>org.springframework</groupid >   <artifactId>spring-context</artifactId>   <version>${spring.version}</ version>  </dependency>    <dependency>   <groupId>org.springframework< /groupid>   <artifactId>spring-web</artifactId>   <version>${spring.version}</ Version>  </dependency>
Add a plugin using spring in struts <dependency> <groupId>org.apache.struts</groupId> <artifactId> Struts2-spring-plugin</artifactid> <version>${struts.version}</version> </dependency>

2, then configure Spring listening <listener> <listener-class>org.springframework.web.context.contextloader in Web.xml listener</listener-class> </listener> <context-param> <param-name>contextcon Figlocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> &L T;/context-param>
3, add applicationcontext.xml, inject bean <?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"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

<bean id= "useraction" class= "Com.s2ss.demo.UserAction" scope= "prototype" >
</bean>
</beans>
4, this time, struts in the action of class can be so written ~
To be consistent with the Bean's ID ~ ~ Glue code, in case you want to be lazy ~ ~ <package name= "P_user" extends= "Struts-default" > <action name= "Login_input" class= "Useraction" method= "Login_input" > <result name= "Success" >/login.jsp </result> </acti on> <action name= "Login" class= "useraction" method= "Login" > <result name= "Success" >/login_success . JSP </result> </action> </package>
step4: Adding MyBatis frames
1, Pom.xml file Add dependency <!--add MyBatis dependent--> <!--injection DataSource dependency-->
<dependency> <groupId>org.springframework</groupId> <artifactid>spring-jdbc</ Artifactid> <version>${spring.version}</version> </dependency>

<dependency> <groupId>org.mybatis</groupId> <artifactid>mybatis-spring</artifactid > <version>1.2.0</version> </dependency> <dependency> <groupid>org.mybatis</g roupid> <artifactId>mybatis</artifactId> <version>3.2.3</version> </dependency& Gt <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> < Version>10.2.0.2</version> </dependency>
2, Spring injected sqlsessionfactory <bean id= "DataSource" class= " Org.springframework.jdbc.datasource.DriverManagerDataSource "> <property name=" Driverclassname "> < value>oracle.jdbc.driver.oracledriver</value> </property> <property name= "url" > <value> Jdbc:oracle:thin: @ip:p ort/**</value> </property> <property name= "username" > <value>**</ value> </property> <property name= "password" > <value>**</value> </property> ;/bean>
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "DataSource" "ref=" DataSource "/> <!--automatically under the Mappers package to search MyBatis's mapping file--> <property name=" mapperlocations "value=" cl Asspath*:mapper/*.xml "/> </bean>
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "DataSource" ref= "DataSource"/> </bean>
<!--DAO--> <bean id= "Userdao" class= "Org.mybatis.spring.mapper.MapperFactoryBean" > <property name= "M Apperinterface "value=" Com.s2ss.demo.dao.UserDao "></property> <property name=" sqlsessionfactory "ref=" Sqlsessionfactory "/> </bean> <bean id=" UserService "class=" Com.s2ss.demo.service.impl.UserServiceImpl " > <property name= "Userdao" ref= "Userdao"/> </bean>
<bean id= "useraction" class= "Com.s2ss.demo.action.UserAction" scope= "prototype" > <property "name=" UserService "ref=" UserService "/> </bean>
Service,dao interfaces and class files are not pasted. Only one simple SQL is listed in the Mapper
This is probably the way it is.

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.