1. Project scaffold reference https://www.cnblogs.com/fishyy/p/10313566.html
2. Add spring MVC dependency in parent pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yyu.cms</groupId> <artifactId>appcms</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>appcms</name> <url>http://maven.apache.org</url> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.6.RELEASE</version> </dependency> </dependencies> </dependencyManagement> <modules> <module>appcms-common</module> <module>appcms-domain</module> <module>appcms-dao</module> <module>appcms-service</module> <module>appcms-web</module> </modules> <properties> <app.version>${project.version}</app.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <junit.version>3.8.1</junit.version> </properties> </project>
Reference spring MVC dependency in appcms Web
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.yyu.cms</groupId> <artifactId>appcms</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.yyu.cms.web</groupId> <artifactId>appcms-web</artifactId> <packaging>war</packaging> <name>appcms-web Maven Webapp</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.yyu.cms.service</groupId> <artifactId>appcms-service</artifactId> <version>${app.version}</version> </dependency> <!-- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> </dependencies> <build> <finalName>appcms-web</finalName> </build> </project>
3. Create a new java package in appcms Web
And create the following controller
4. Create my-dispatcher-servlet.xml
<?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" 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"> <context:component-scan base-package="com.yyu.cms.web.controller"/> </beans>
5. Modify web.xml and configure dispatcher Servlet
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>SpringMvcApp</display-name> <servlet> <servlet-name>myDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:my-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
6. Package and release
Copy the appcms-web.war package to the tomcat webapp directory and change its name to yyucms.war
7. Run tomcat to access yyucms
8. Configure ContextLoaderListener, which is a ServletContextListener. It starts the spring container of the business layer through the spring configuration file specified by the contextConfigLocation parameter.
Configure ContextLoaderListener in the web.xml file. Maybe my maven version is too low. Adding ContextLoaderListener configuration to the generated web.xml file will result in an error
Amend it to read
The modified web.xml is added to the ContextLoaderListener configuration
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMvcApp</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>myDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:my-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
9. Before adding applicationContext, add related dependency
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.yyu.cms</groupId> <artifactId>appcms</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.yyu.cms.dao</groupId> <artifactId>appcms-dao</artifactId> <packaging>jar</packaging> <name>appcms-dao</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.yyu.cms.common</groupId> <artifactId>appcms-common</artifactId> <version>${app.version}</version> </dependency> <dependency> <groupId>com.yyu.cms.domain</groupId> <artifactId>appcms-domain</artifactId> <version>${app.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> </dependencies> </project>
10. Add applicationContext.xml file
<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/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- Configure component scanner --> <!-- stay my-dispatcher-servlet.xml This property can also be configured in the file --> <context:component-scan base-package="com.yyu.cms"/> <!-- data source --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="yyu010383" /> </bean> <!-- To configure session factory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- Transaction manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- To configure AOP notice --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- Configure transaction properties --> <tx:attributes> <!-- Add transaction management method --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="select*" read-only="true"/> </tx:attributes> </tx:advice> <!-- To configure AOP,Add operation configuration for transaction management AOP --> <aop:config> <!-- Introduced Spring Transaction notifications defined, need to use aop:advisor --> <!-- Next difficult --> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.yyu.cms.service.*.*(..))" /> </aop:config> </beans>
11. Add mybstis-config.xml file
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- Set hump matching --> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- Set package scan(alias) --> <typeAliases> <package name="com.yyu.cms.domain"/> </typeAliases> <!-- Configuration environment: multiple environments can be configured, default: Configure a unique ID of an environment, indicating which environment is used by default --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!-- Configure connection information --> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="yyu010383"/> </dataSource> </environment> </environments> <!-- Configuration mapping files: for configuration sql Statement and result set type, etc --> <mappers> <!--<mapper resource="UserMapper.xml"/>--> </mappers> </configuration>
12. Add service class
TestService.java
package com.yyu.cms.service; public interface TestService { int calc(int a, int b); }
TestServiceImpl.java
package com.yyu.cms.service.impl; import com.yyu.cms.service.TestService; import org.springframework.stereotype.Service; @Service public class TestServiceImpl implements TestService{ public int calc(int a, int b) { return a + b; } }
To use @ Service annotation in appcms Service, you need to add spring context
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.yyu.cms</groupId> <artifactId>appcms</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.yyu.cms.service</groupId> <artifactId>appcms-service</artifactId> <packaging>jar</packaging> <name>appcms-service</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.yyu.cms.dao</groupId> <artifactId>appcms-dao</artifactId> <version>${app.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> </dependencies> </project>
13. Use service in controller
package com.yyu.cms.web.controller; import com.yyu.cms.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "/test") public class TestController { @Autowired private TestService testService; @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET) @ResponseBody public String welcome(@PathVariable String name) { return "hello " + name; } @RequestMapping(value = "/calc/{a}/{b}", method = RequestMethod.GET) @ResponseBody public String calc(@PathVariable int a, @PathVariable int b) { int result = testService.calc(a, b); return a + "plus" + b + "The result is" + result; } }
14. Do not package and publish to tomcat for testing this time. Use IDEA to run the test directly
These two paths can be accessed normally
Previously, the version of spring JDBC in the app CMS Dao layer was 5.1.3.RELEASE, and the version of spring was 4.2.6.RELEASE. The version of spring JDBC was too high, which led to jar package compatibility problems. Visit http://localhost:8080/test/calc/1/1 to report errors, and the following errors appear. For details, refer to https://blog.csdn.net/z4331016/article/details/78257165
ps: Hsin Kui now has springboot, otherwise he will be really bothered by these wonderful problems<
After reducing the spring JDBC version, visit again, and the response is as follows
This is the problem of Chinese scrambling. You need to configure the converter for http message conversion of string type
Modify my-dispatcher-servlet.xml
<?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: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-3.2.xsd"> <!-- scanning web Layer bean --> <context:component-scan base-package="com.yyu.cms.web.controller"/> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- Solve Controller Return Chinese string garbled problem --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
Visit OK
Original source: https://www.cnblogs.com/fishyy/p/10315473.html