Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use java code instead of xml to implement SSM

Shulou Source: shulou.com Published: 2022-06-02 07:29:14 10月04日 Update

This article introduces the knowledge of "how to use java code instead of xml to achieve SSM". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

SpringBoot recommends that developers use Java configuration to build the framework. A large number of automatic configurations in SpringBoot are realized through Java code configuration rather than XML configuration. Similarly, we can also use pure Java to build a SSM environment, that is, there is no XML configuration in the project, including web.xml.

Environmental requirements:

Tomact version must be above 7

1. Create a normal maven project in IDEA

Dependencies that need to be used in pom.xml joining the project

4.0.0 com.xiao.ssm SSM-demo 1.0-SNAPSHOT war 88 2.2 8888 UTF-8 org.springframework spring-webmvc 5.2.12.RELEASE javax.servlet javax. Servlet-api 4.0.1 provided javax.servlet.jsp jsp-api 2.2 org.apache.tomcat.maven tomcat7-maven-plugin ${tomcat.version} ${webserver.port} / ${project.artifactId} ${project.build.sourceEncoding} central https://maven.aliyun.com/nexus/content/repositories/central Central https://maven.aliyun.com/nexus/content/repositories/central 2. Add Spring configuration to create SpringConfig.java file

Package com.xiao.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Controller / * * @ author xiaoss * @ Configuration annotations indicate that this class is a configuration class, and its function is similar to: applicationContext.xml file * @ ComponentScan annotations identify configuration package scan, useDefaultFilters means to use the default filter, and the configuration in excludeFilters means to remove the comments in Controller, * that is, when the project starts The spring container scans only all bean * @ date except Controller classes at 16:43 * / @ Configuration@ComponentScan on October 29th, 2021 (basePackages = "com.xiao", useDefaultFilters = true, excludeFilters = {@ ComponentScan.Filter (type = FilterType.ANNOTATION,classes = Controller.class)}) public class SpringConfig {} 3. Add SpringMVC configuration to create SpringMVCConfig.java file

Package com.xiao.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Controller;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport / * * @ author xiaoss * @ date 16:56 on October 29th, 2021 * / @ Configuration//@ComponentScan (basePackages = "com.xiao", useDefaultFilters = true,// excludeFilters = {@ ComponentScan.Filter (type = FilterType.ANNOTATION,classes = Controller.class)}) @ ComponentScan (basePackages = "com.xiao") public class SpringMVCConfig extends WebMvcConfigurationSupport {/ * configure static resource filtering Equivalent to * @ param registry * / @ Override protected void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ js/**") .addResourceLocations ("classpath:/") } / * configure the view parser * @ param registry * / @ Override protected void configureViewResolvers (ViewResolverRegistry registry) {registry.jsp ("/ jsp/", ".jsp");} / * path mapping * @ param registry * / @ Override protected void addViewControllers (ViewControllerRegistry registry) {registry.addViewController ("/ hello3") .setViewName ("hello") } / * json conversion configuration * @ param converters * / @ Override protected void configureMessageConverters (List

Tags: Configuration file project container comment code path UTF-8 action content parameter character string actual object scheme method more identity environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL Shulou Tech Info Xiaomi vpn