使用注解开发SpringMVC
配置文件中常见BUG
一、地址数必须成偶
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
二、配置中避免中文注释,否则会500!!(字符集问题)
1.新建项目
2.导入jar包
3.编写web.xml,注册DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 注册servlet-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动级别,数字越小,启动越早-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.编写springmvc配置文件
<!-- 自动扫描包,让指定包下注解生效-->
<context:component-scan base-package="com.bao"></context:component-scan>
<!-- 让Spring MVC不处理静态资源 .css .js .html -->
<mvc:default-servlet-handler/>
<!-- 处理器映射器HandlerMapping 处理器适配器HandlerAdapter 自动配置自动注入-->
<mvc:annotation-driven/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- 前缀-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
5.创建对应的控制类 controller
@Controller//自动走视图解析器
public class HelloController {
@RequestMapping("/hello")
//@ResponseBodt 不走视图解析器
public String hello(Model model){
//封装数据
model.addAttribute("msg","你好,SpringMVC");
return "hello";
//return "redirect:/index.jsp"; 视图重定向,默认为转发
}
}
6.最后完善前端视图和controller之间的对应
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
7.测试运行调试
使用springMVC必须配置的三大件:
处理器映射器 处理器适配器 视图解析器
控制器Colltroller @Colltroller
@Colltroller //走视图解析器
@RestColltroller //返回字符串
控制器负责解析用户的请求并将其转换为一个模型。
在Spring MVC中一个控制器类可以包含多个方法
@RequestMapping("/hello") //映射地址
@RequestMapping("/chen")
public String test1(@RequestParam("name") String name, Model model)
加上 @RequestParam 会更直观的看到这是从前端所获取的参数
JSON(数据交换格式)
前后端分离时代:
后端部署后端,提供接口,提供数据;
前端独立部署,负责渲染后端的数据;
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
@RequestMapping("/j1")
@ResponseBody //不走视图解析器
public String json1() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
List<User> userList = new ArrayList<User>();
User user1 = new User(50,"陈1",3);
User user2 = new User(49,"陈2",3);
User user3 = new User(51,"陈3",3);
userList.add(user1);
userList.add(user2);
userList.add(user3);
String s = mapper.writeValueAsString(userList);
return s;
}
JSON乱码问题
<mvc:annotation-driven>
<!--解决json乱码问题-->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
FastJson
fastjson.jar是阿里开发的一款专门用于Java开发的包,可以方便实现json对象与JavaBean对象的转换
FastJson需要的jar包、测试
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
@RequestMapping("/j2")
@ResponseBody //不走视图解析器
public String json2() throws JsonProcessingException {
List<User> userList = new ArrayList<User>();
User user1 = new User(50,"陈1",3);
User user2 = new User(49,"陈2",3);
User user3 = new User(51,"陈3",3);
userList.add(user1);
userList.add(user2);
userList.add(user3);
String s = JSON.toJSONString(userList);
return s;
}