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};
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++最简化示例完整结束+++++++++++++++++++++++++++++++++++++ 以下是参数使用:
标签:web,return,springMvc,springframework,整理,org,精简,import,new 来源: https://www.cnblogs.com/fgxwan/p/16535552.html