Web SSM Configuration StartingCopy the code

Foreword: when I saw a lot of data from the net discovery still did not make out me when I know: it is time to write an introductory configuration article to rescue the programmer lost. Gnome male – “.

Environment: Eclipse 2018, JDK 1.8, Tomcat 7.0

Ps: Personally, ECLIPSE is better than MyEclipse. Idea is better than Eclipse. Do not spray if you do not like it.

Let me first show you the overall directory structure

Config is also a resource package. You can create a Config Folder by right-clicking -> New Other and selecting Source Folder.

Under the folder are the various configuration files: Spring configuration file, database configuration file, log4j configuration file, MyBatis configuration file, resource configuration file and SpringMVC configuration file.

Applicationcontext. XML Configuration file information

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> <! Db.properties --> <context:property-placeholder location="classpath:db.properties"/> <! - configuration data source - > < bean id = "dataSource" class = "org.apache.com mons. Dbcp2. BasicDataSource" > <! <property name="driverClassName" value="${jdbc.driver}" /> <! - connect to the database url - > < property name = "url" value = "${. JDBC url}" / > <! <property name="username" value="${jdbc.username}" /> <! <property name="password" value="${jdbc.password}" /> <! ${jdbc.maxtotal}" /> <! Value ="${jdbc.maxidle}" /> <! InitialSize value="${jdbc.initialSize}" /> </bean> <! - the transaction manager - > < bean id = "transactionManager" class = ". Org. Springframework. JDBC datasource. DataSourceTransactionManager "> <! DataSource ="dataSource" /> </ dataSource > <! <tx:advice ID ="txAdvice" transaction-manager="transactionManager"> < TX: Attributes > <! <tx:method name="insert "Propagation ="REQUIRED" /> <tx:method name="insert" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <! - section - > < aop: config > < aop: advisor advice - ref = "txAdvice pointcut =" execution "(. * com itheima. Core. Service. *. * (..) )" /> </aop:config> <! - configure MyBatis factory - > < bean class = "org. MyBatis. Spring. SqlSessionFactoryBean" > <! --> < dataSource ="dataSource" /> <! <property name="configLocation" value=" clasSPath: MyBatis -config.xml" /> </bean> <! -- Interface development, scanning com.itheima.core.dao package, Written in the book of this package of interface can be scanned to -- > < bean class = "org. Mybatis. Spring. Mapper. MapperScannerConfigurer" > < property name = "basePackage" value="com.itheima.core.dao" /> </bean> <! <context:component-scan base-package="com.itheima.core. Service "/> </beans>Copy the code

The db. The properties file

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/boot_crm
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
Copy the code

Log4j. The properties file

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.itheima.core=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
Copy the code

Mybatis -config. XML file (note that name is your entity class package name, depending on the actual situation)

<? The 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> <! -- Alias definition --> <typeAliases> <package name="com.itheima.core.po" /> </typeAliases> </configuration>Copy the code

The resource properies file

customer.from.type=002
customer.industry.type=001
customer.level.type=006
Copy the code

For springmvc – config. XML file

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> <! - loading properties file - > < context: the property - placeholder location = "classpath: resource. The properties" / > <! - configuration scanner - > < context: component - scan base - package = "com. Itheima. Core. Web. Controller" / > <! -- Annotation-driven: configure processor mapper and adapter --> < MVC :annotation-driven /> <! -- Configure access mapping for static resources, files in this configuration, < MVC :resources location="/js/" mapping="/js/**" /> < MVC :resources location="/ CSS /" mapping="/ CSS /**" /> <mvc:resources location="/fonts/" mapping="/fonts/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <! <bean id="jspViewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <! < MVC :interceptors> < MVC :interceptor> < MVC :mapping path="/**" /> <bean class="com.itheima.core.interceptor.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors> </beans>Copy the code

The parameter information in the above configuration file is very important, indicating that the SSM is dependent on the configuration parameter to live, so do not mix partners.

SRC packages include Po, DAO, Service, serviceImpl, and Controller packages.

These packages add up to hierarchical decoupling. MVC pattern. The Po package contains the entity class, and the DAO package contains two files, one is a.java file and the other is an.xml file. The. Java file in this directory is an interface. It defines some of the methods that we need. Here you need to manipulate the database. And the specific operation of the database part is in the.xml file to write SQL statements to achieve. Under the Service layer, the interface class represents the part of the service layer, where the method name is the same as the DAO layer. Remember, it is exactly the same. Case and parameters are included. Implement the interface methods of the Service under the Impl, instantiating an instance of the interface. Return an interface instance object in an implemented method. The corresponding method name is ok.

Without further ado, above:

There is also a

It is well known that setting parameters is a serious problem when setting up the SSM framework. It’s hard to find if something goes wrong. Next I will give each specific parameter code and git address. You can refer to download. I have a comment section in it.

First and foremost is the JAR package, which can be downloaded from a Git address. I upload it

Okay, so here’s the big story: the web.xml file

<? The 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 - > the TestManager < / display - name > <! <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <! Encoding </filter-name> <filter-class> </filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <! <servlet> <servlet-name> </servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <! <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>crm</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <! <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>Copy the code

Make sure you don’t write the wrong class name in the configuration information. When you’re done, press CTRL to see if it works. By the way, I’ve intentionally omitted jar packages here. Google, Spring and springMvc are the same, so there is no need to associate the JAR package, but when using myBatis, you need a middleman, otherwise the database will not talk to you, so you will understand… Don’t say much. See the screenshot:

There’s a little bit less of it, but it’s pretty much all there is.

There is also hope that you do not directly copy the configuration of the environment, it is best to write a manual. Or even if you copy bits and pieces, copy and then check to see if it works. This makes it easier to check for errors. I didn’t think there would be any problems when I first configured it because of the reference materials. It was only after the final basic build that I ran and discovered a big problem. The home page is not automatically opened in the browser after Tomcat is started. A one-letter error was found in the ApplicationContext argument. In fact, I solved the problem by making a new copy of this part of the parameters, and then succeeded, but BEFORE that I looked at the configuration file many times and still did not find it. So again, just to remind you of the point. Don’t get the letters wrong. The parts that need to be copied must not be handwritten, or they will not be found.

This is the index.jsp folder

Let’s look at the login.jsp folder

Here is the main part of the code. As for the ${} content, I have a comment on it, here is a foreshadowing, I will say in another article. So just to get a sense of what’s going on here, it’s not always going to be a problem but sometimes it’s going to be a problem because of the path.

The last, of course, is to show you the results of the operation, as the saying goes, there is a picture of the truth.

The interface is a little ugly.

Git address: github.com/jiwenjie/Te…