注意:base-package要和controller层保持一致,涵盖处理器映射器,处理器适配器,视图解析器
其余配置亘古不变,请放心使用
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
//固定不变
<context:component-scan base-package="com.huang.controller" />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
bean>
beans>
org.springframework.web.servlet.DispatcherServlet和
org.springframework.web.filter.CharacterEncodingFilter是
springMVC提供的servlet调度器和编码过滤器
注意的是前者匹配所有请求,后者匹配所有请求和所有jsp页面
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>encodingfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
解析:
1.@Controller注解为控制器
2.return "hello"会根据/springmvc-servlet.xml中视图解析器的prefix和suffix来匹配url对应的前端页面,如:url为 ‘http://localhost:8080/hello/h1’则访问项目目录的/WEB-INF/jsp/hello.jsp
3.Model model;model.addAttribute(“msg”,“12121”);通常用来设置前端数据的回显,下文jackson将详细说明
@Controller
@RequestMapping("hello")
public class HelloController {
@RequestMapping("h1")
public String hello(Model model){
model.addAttribute("msg","12121");
return "hello";
}
@RequestMapping("h2")
public String hello1(Model model){
model.addAttribute("msg1","helloSpringMvc");
return "hello1";
}
}
${msg} 即对应 model.addAttribute(“msg”,“12121”);
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${
msg}
</body>
</html>
前端代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/test1" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
controller层
@Controller
public class EncodingController {
@PostMapping("/test1")
public String test1(String name, Model model) {
System.out.println(name);
model.addAttribute("msg",name);
return "hello";
}
}
解析:
前端输入框的name再没使用jackson时,后台拿到的就是name
@PostMapping("/test1") :由于请求有多种方式,所以RequestMapping就对应有PostMapping/GetMapping/PutMapping等等,只要和前端的method="post"匹配即可,RequestMapping通配
public String test1(String name, Model model) {} 形参String name获取到前端传递的参数
2.jar包
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.10.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.10.0version>
dependency>
3.工具类JsonUtils.getJson类
//抽取出公共的getJson类
public class JsonUtils {
public static String getJson(Object object,String dateFormat){
//jackson中有ObjectMapper对象
ObjectMapper mapper = new ObjectMapper();
// 不使用时间戳的方式
mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
// 自定义日期的格式
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
mapper.setDateFormat(sdf);
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
4.controller类示例
@Controller
public class JsonController {
@RequestMapping("j3")
@ResponseBody //不会执行任何视图,只返回一个字符串
public String json3() throws JsonProcessingException {
List<User> userList = new ArrayList<User>();
userList.add(new User("黄杰1",20,"男"));
userList.add(new User("黄杰2",20,"男"));
userList.add(new User("黄杰3",20,"男"));
userList.add(new User("黄杰4",20,"男"));
return JsonUtils.getJson(userList,"");
}
}
5./springmvc-servlet.xml中配置解决json乱码(都是写死,可以存为模板)
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
bean>
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json;charset=UTF-8" />
bean>
mvc:message-converters>
mvc:annotation-driven>
这些依赖包,基本贯穿所有springMVC全过程,可以存为模板
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.jetbrainsgroupId>
<artifactId>annotationsartifactId>
<version>RELEASEversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
dependency>
<dependency>
<groupId>javax.servlet.jsp.jstlgroupId>
<artifactId>jstl-apiartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>taglibsgroupId>
<artifactId>standardartifactId>
<version>1.1.2version>
dependency>
dependencies>