Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java code instead of xml to implement SSM

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report