文章目录
- 一、Spring MVC是什么?
- 二、Spring MVC环境配置
- 三、SpringMVC处理示意图
- 四、Spring MVC数据绑定
- 五、解决响应中的中文乱码
- 六、响应输出结果
- 七、SpringMVC整合Freemarker
一、Spring MVC是什么?
二、Spring MVC环境配置
1.Maven依赖spring-wevmvc
<?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>com.imooc</groupId>
<artifactId>first-springmvc</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
</project>
2.web.xml配置DispaerServlet
<?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_3_1.xsd"
version="3.1">
<!--DispatchServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<!--
DispatchServlet是spring MVC最核心的对象
DispatchServlet用于拦截Http请求
并根据请求对URL调用与之对应对Controller方法
-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--applicationContext.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!--
在web应用启动时自动创建Spring IOC容器,
并初始化DispatchServlet
-->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--"/" 代表拦截所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.配置applicationContext.xml的mvc标记
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--
context:component-scan 标签作用
在Spring IOC初始化过程中,自动创建并管理com.imooc.springmvc及子包中
拥有以下注解的对象.
@Repository
@Service
@Controller
@Component
-->
<context:component-scan base-package="com.imooc.springmvc"></context:component-scan>
<!--启动Spring MVC注解开发模式-->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
</beans>
4.开发Controller控制器
@Controller
public class TestController {
@GetMapping("/t")
@ResponseBody
public String test(){
return "hello,SUCCESS";
}
}
三、SpringMVC处理示意图
四、Spring MVC数据绑定
1.URL Mapping
@Controller
@RequestMapping("/um")
public class URLMappingController {
@GetMapping("/g")
@ResponseBody
public String getMapping(){
return "get method";
}
@PostMapping("/p")
@ResponseBody
public String postMapping(){
return "post method";
}
}
2.接收请求参数
@Controller
@RequestMapping("/um")
public class URLMappingController {
@GetMapping("/g")
@ResponseBody
//通过@RequestParam()完成自定义的参数对照关系
public String getMapping(@RequestParam("manager_name") String managerName){
return "get method";
}
@PostMapping("/p")
@ResponseBody
public String postMapping(String username,String password){
System.out.println(username+":"+password);
return "post method";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="/um/p" method="post">
<input name="username"><br/>
<input name="password"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
3.使用Java Bean接受请求参数
@PostMapping("p1")
@ResponseBody
public String postMapping(User user){
System.out.println(user.getUsername()+":"+user.getPassword());
return "post method";
}
需要有对应的实体类,并且实体类中属性的名字必须要跟前端页面的name一致
public class User {
private String username;
private Long password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getPassword() {
return password;
}
public void setPassword(Long password) {
this.password = password;
}
}
4.接收表单复合数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学员调查问卷</title>
<style>
.container {
position: absolute;
border: 1px solid #cccccc;
left: 50%;
top: 50%;
width: 400px;
height: 300px;
margin-left: -200px;
margin-top: -150px;
box-sizing: border-box;
padding: 10px;
}
h2{
margin: 10px 0px;
text-align: center;
}
h3{
margin: 10px 0px;
}
</style>
</head>
<body>
<div class="container">
<h2>学员调查问卷</h2>
<form action="./apply" method="post">
<h3>您的姓名</h3>
<input name="name" class="text" style="width: 150px">
<h3>您正在学习的技术方向</h3>
<select name="course" style="width: 150px">
<option value="java">Java</option>
<option value="h5">HTML5</option>
<option value="python">Python</option>
<option value="php">PHP</option>
</select>
<div>
<h3>您的学习目的:</h3>
<input type="checkbox" name="purpose" value="1">就业找工作
<input type="checkbox" name="purpose" value="2">工作要求
<input type="checkbox" name="purpose" value="3">兴趣爱好
<input type="checkbox" name="purpose" value="4">其他
</div>
<div style="text-align: center;padding-top:10px" >
<input type="submit" value="提交" style="width:100px">
</div>
</form>
</div>
</body>
</html>
@Controller
public class FormController {
// @PostMapping("/apply")
@ResponseBody
public String apply(@RequestParam(value = "n",defaultValue = "ANON") String name, String course, Integer[] purpose){
System.out.println(name);
System.out.println(course);
for (Integer p : purpose) {
System.out.println(p);
}
return "SUCCESS";
}
// @PostMapping("/apply")
@ResponseBody
public String apply(String name, String course, @RequestParam List<Integer> purpose){
System.out.println(name);
System.out.println(course);
for (Integer p : purpose) {
System.out.println(p);
}
return "SUCCESS";
}
// @PostMapping("/apply")
@ResponseBody
public String apply(Form form){
return "SUCCESS";
}
@PostMapping("/apply")
@ResponseBody
public String apply(@RequestParam Map map){
System.out.println(map); //如果存在复合数据,map是无法接收的
return "SUCCESS";
}
}
5.关联对象赋值
6.日期类型转换
方法1
如果前端传来了日期类型的字符串,并且后端想要转化为Date类型,可以在参数类型前加上
@DateTimeFormat(pattern = "yyyy-MM-dd")
具体如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/um/p1" method="post">
<input name="username"><br/>
<input name="password"><br/>
<input name="createTime"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
@PostMapping("/p1")
@ResponseBody
public String postMapping1(User user , String username ,@DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime){
System.out.println(user.getUsername() + ":" + user.getPassword());
return "<h1>这是Post响应</h1>";
}
public class User {
private String username;
private Long password;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createTime;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getPassword() {
return password;
}
public void setPassword(Long password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
方法2
也可以自己写一个转换器,则不需要加注解:
public class MyDateConverter implements Converter<String, Date> {
public Date convert(String s) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdf.parse(s);
return d;
} catch (ParseException e) {
return null;
}
}
}
在applicationContext.xml注册,加入这句话
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.imooc.springmvc.converter.MyDateConverter"/>
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService">
五、解决响应中的中文乱码
解决方法:在applicationContext.xml增加这一段
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!-- response.setContentType("text/html;charset=utf-8") -->
<value>text/plain;charset=utf-8</value>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
六、响应输出结果
ModelAndView
最简单的情况:
@GetMapping("/view")
public ModelAndView showView(){
ModelAndView mav = new ModelAndView("/view.jsp");
return mav;
}
但是如果数据是由controller动态地查询产生,如:
@GetMapping("/view")
public ModelAndView showView(Integer userId){
// ModelAndView mav = new ModelAndView("redirect:/view.jsp");
ModelAndView mav = new ModelAndView();
mav.setViewName("/um/view.jsp");
User user = new User();
if(userId == 1){
user.setUsername("lily");
}else if(userId == 2){
user.setUsername("smith");
}else if(userId == 3){
user.setUsername("lina");
}
mav.addObject("u" , user);
return mav;
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>I'm view page(/um/view.jsp)</h1>
<hr>
<h3>Username:${u.username}</h3>
</body>
</html>
@GetMapping("/view")
public ModelAndView showView(Integer userId){
// ModelAndView mav = new ModelAndView("redirect:/view.jsp");
ModelAndView mav = new ModelAndView();
mav.setViewName("/um/view.jsp");
User user = new User();
if(userId == 1){
user.setUsername("lily");
}else if(userId == 2){
user.setUsername("smith");
}else if(userId == 3){
user.setUsername("lina");
}
mav.addObject("u" , user);
return mav;
}
//String与ModelMap
//Controller方法返回String的情况
//1. 方法被@ResponseBody描述,SpringMVC直接响应String字符串本身
//2. 方法不存在@ResponseBody,则SpringMVC处理String指代的视图(页面)
@GetMapping("/xxxx")
// @ResponseBody
public String showView1(Integer userId , ModelMap modelMap){
String view = "/um/view.jsp";
User user = new User();
if(userId == 1){
user.setUsername("lily");
}else if(userId == 2){
user.setUsername("smith");
}else if(userId == 3){
user.setUsername("lina");
}
modelMap.addAttribute("u", user);
return view;
}
七、SpringMVC整合Freemarker
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<bean id="ViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html;charset=utf-8"/>
<property name="suffix" value=".ftl"/>
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl"/>
<property name="freemarkerSettings">
<props>
<prop key="defaultEncoding">UTF-8</prop>
</props>
</property>
</bean>