SpringMVC技术与Servlet技术功能等同,均属于web层开发技术
优点:使用简单,开发快捷,灵活性强
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>6.0.5</version>
- </dependency>
- package org.example.config;/*
- * @Auther:huangzhiyang
- * @Date:2023/10/7
- * @Description:
- */
-
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.FilterType;
- import org.springframework.stereotype.Controller;
-
- @Configuration
- @ComponentScan(value = "org.example",
- excludeFilters = @ComponentScan.Filter(
- type = FilterType.ANNOTATION,
- classes = Controller.class
- )
- )
- public class SpringConfig {
- }
- package org.example.config;/*
- * @Auther:huangzhiyang
- * @Date:2023/10/7
- * @Description:
- */
-
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- @ComponentScan({"org.example.service","org.example.dao"})
- public class SpringConfig {
- }
- package org.example.config;/*
- * @Auther:huangzhiyang
- * @Date:2023/10/7
- * @Description:
- */
-
- import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
-
- public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
-
- @Override
- protected Class>[] getRootConfigClasses() {
- return new Class[]{SpringConfig.class};
- }
-
- @Override
- protected Class>[] getServletConfigClasses() {
- return new Class[]{SpringMvcConfig.class};
- }
-
- @Override
- protected String[] getServletMappings() {
- return new String[]{"/"};
- }
- }
- package org.example.config;/*
- * @Auther:huangzhiyang
- * @Date:2023/10/7
- * @Description:
- */
-
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- @ComponentScan("org.example.controller")
- public class SpringMvcConfig {
- }
- package org.example.config;/*
- * @Auther:huangzhiyang
- * @Date:2023/10/7
- * @Description:
- */
-
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.FilterType;
- import org.springframework.stereotype.Controller;
-
- @Configuration
- @ComponentScan(value = "org.example",
- excludeFilters = @ComponentScan.Filter(
- type = FilterType.ANNOTATION,
- classes = Controller.class
- )
- )
- public class SpringConfig {
- }
区别:@RequestParam用于接收ur1地址传参,表单传参【application/x-www-form-urlencoded】,@RequestBody用于接收json数据【application/json】
应用: 后期开发中,发送json格式数据为主,@RequestBody应用较广,如果发送非json格式数据,选用@RequestParam接收请求参数
- package org.example.controller;/*
- * @Auther:huangzhiyang
- * @Date:2023/10/7
- * @Description:
- */
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- @Controller
- @RequestMapping("/user")
- public class UserController {
- @RequestMapping("/save")
- @ResponseBody
- public String save() {
- System.out.println("user save");
- return "{'module':'user save'}";
- }
-
- @RequestMapping("/delete")
- @ResponseBody
- public String delete() {
- System.out.println("user delete");
- return "{'module':'user delete'}";
- }
- }
- @RequestMapping("/commonParam")
- @ResponseBody
- public String commonParam(String name) {
- System.out.println("user save");
- return "{'module':'user commonParam'}";
- }
与get一致
导入依赖
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.14.2</version>
- </dependency>
改变SpringMvcConfig
- package org.example.config;/*
- * @Auther:huangzhiyang
- * @Date:2023/10/7
- * @Description:
- */
-
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
-
- @Configuration
- @ComponentScan("org.example.controller")
- @EnableWebMvc
- public class SpringMvcConfig {
- }
接受json数据
- @RequestMapping("/test")
- @ResponseBody
- public String listParamForJson(@RequestBody List<String> likes){
- return "";
- }
修改ServletContainersInitConfig
- package org.example.config;/*
- * @Auther:huangzhiyang
- * @Date:2023/10/7
- * @Description:
- */
-
- import org.springframework.web.filter.CharacterEncodingFilter;
- import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
-
- import java.util.logging.Filter;
-
- public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
-
- @Override
- protected Class>[] getRootConfigClasses() {
- return new Class[]{SpringConfig.class};
- }
-
- @Override
- protected Class>[] getServletConfigClasses() {
- return new Class[]{SpringMvcConfig.class};
- }
-
- @Override
- protected String[] getServletMappings() {
- return new String[]{"/"};
- }
- @Override
- protected void getServletFilters(){
- CharacterEncodingFilter filter=new CharacterEncodingFilter();
- filter.setEncoding("UTF-8");
- return new Filter[]{filter};
- }
- }
- @RequestMapping("/toJumpPage")
- public String toJumpPage() {
- System.out.println("跳转页面");
- return "index.jsp";
- }
- @RequestMapping("/toText")
- @ResponseBody
- public String delete() {
- System.out.println("返回纯文本数据");
- return "response text";
- }
- @RequestMapping("/toJsonPOJO")
- @ResponseBody
- public User listParamForJson() {
- UserDao user=new UserDao() ;
- user.setAge(15);
- user.setName("15");
- return user;
- }