springMvc精简整理

精简共分步

新建一个web工程  导入核心就二包,因为springmvc 包自己有依赖

<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
</dependencies>
</project>
++++++++++++++++++++++++++++++++++++++++++2++++++++++++++++++++++++++
spring核心配置
package com.ithm.zConfig;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.ithm.Config")
public class Springconfig {
}

 

 


+++++++++++++++++++++++++++++++++++++++++++++++++++++++3++++++++++++++++++++++++++++++++++++++

第三步 spring MVC 配置

package com.ithm.zConfig;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{Springconfig.class};
}

@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}

@Override
//本方来自spring web包,解析post 中文乱码
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}

 

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++4++++++++++++++++++++++++++

配置mvc那儿上面是精简写法,原写法为:
package com.ithm.zConfig;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

public class ServletConfigInitbask extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(Springconfig.class);
return ctx;
}

@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}

@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=最后一步是控制器++++++++++++
package com.ithm.Config;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserConfig {
@RequestMapping("/eee")
@ResponseBody
public String save(String name,String age){
System.out.println("name"+""+"="+ name);
System.out.println("age"+""+"="+ age);
return "{'a':'b2002'}";
}
@RequestMapping("/add")
@ResponseBody
public String add(){
System.out.println("kkkk");
return "{'d':'dddd'}";
}

}
 
 如果是乱码以下是乱码解决:

@Override
//本方来自spring web包,解析post 中文乱码
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}

 

 



 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++最简化示例完整结束+++++++++++++++++++++++++++++++++++++
 
以下是参数使用:
 
 客户端传参和服务器不一样使用@RequestParam(“”)括号里面放浏览器传来的参数。
public String save(@RequestParam("name") String name, String age){
如果对方传数组,只需要参数中name一样
public string arrParam(String[] likes)
如果对方传list 只需要参数和数组一样传,服务器指定参数时写法增加@RequestParam
pulic string listParam (@requestParam List<String> likes)

服务器要想支持json 共分4步
要加载json包,还需要在spring配置支持 json转换 @EbdbkeWebMvc,最后还在服务器参数中后中加@requestBody
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 

最后浏览器发送,浏览器发送对像也可以pojo对象接收,json能自动处理

 

 

 

 如果浏览器发送多个List 也可以用json接收

 对于日期类型: 默认只支持Date 2008/2/5这样格式,加“-”这样就不行了

 

 

 

 

 

服务器返回,默认是返回页面,如果想直接返回数据:

 

 

REST 表现形式壮态转换

@RequestMapping(value = /user/{id},method = RequestMethod.DELETE)

Public String delete(@pathVariable Integer id)

 

 

 

 

 全部精简后写法:

不同请求方式示例:

 

 

 

精简前参考:
 

 

 

 

 

最后静态文件放行

 

 

 

 


 

 



++++++++++++++++++++++ist+++++++++++++++++++++++++





 

 
posted @   稷下元歌  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示