springmvc入门


一、创建工程

1、创建一个maven工程

2、右键工程目录,添加web4.0框架支持

3、最终文件目录结构

二、编写配置文件

1、pom.xml

注意:true 添加这一行之后子项目将不会添加这个依赖

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     
 6         SpringMVC-KS
 7         org.example
 8         1.0-SNAPSHOT
 9     
10     4.0.0
11 
12     springmvc-03-annotion
13 
14     
15     
16         
17             junit
18             junit
19             4.12
20         
21         
22         
23             org.springframework
24             spring-webmvc
25             5.2.13.RELEASE
26         
27         
28         
29             javax.servlet
30             javax.servlet-api
31             4.0.1
32             
33             provided
34         
35         
36             javax.servlet.jsp
37             jsp-api
38             2.2
39         
40         
41             javax.servlet
42             jstl
43             1.2
44         
45     
46 
47     
48         
49             
50                 src/main/java
51                 
52                     **/*.properties
53                     **/*.xml
54                 
55                 false
56             
57             
58                 src/main/resources
59                 
60                     **/*.properties
61                     **/*.xml
62                 
63                 false
64             
65         
66     
67 

2、web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5          version="4.0">
 6 
 7 
 8     SpringMVC
 9     class>org.springframework.web.servlet.DispatcherServletclass>
10     
11         contextConfigLocation
12         
13         classpath:SpringMVC-servlet.xml
14     
15     1
16 
17 
18 
19     SpringMVC
20     /
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35     encoding
36     org.springframework.web.filter.CharacterEncodingFilter
37     
38         encoding
39         utf-8
40     
41 
42 
43     encoding
44     /* 
45 
46 

3、编写SpringMVC-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc"
 6        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 https://www.springframework.org/schema/mvc/spring-mvc.xsd">
 7     
 8     
 9     package="com.han.controller"/>
10     
11     default-servlet-handler/>
12     
13     
14     
15     16           class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17         
18         
19     
20 

三、手动编写过滤器,过滤乱码

注意:如果使用get发送请求即使不加过滤器也不会乱码,只有使用post才需要加过滤器

1、新建一个过滤器

public class EncodingFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    public void destroy() {
    }
}

2、在web.xml中添加如下代码,注意url-pattern必须时“/*”,因为“/"会过滤掉jsp

    
        encoding
        class>com.han.filter.EncodingFilterclass>
    
    
        encoding
        /*
    

3、直接使用springmvc自带的过滤器 

    
        encoding
        class>org.springframework.web.filter.CharacterEncodingFilterclass>
        
            encoding
            utf-8
        
    
    
        encoding
        /*
    

四、编写代码

1、创建user类

 1 package com.han.entity;
 2 
 3 public class User {
 4     public String getName() {
 5         return name;
 6     }
 7 
 8     public void setName(String name) {
 9         this.name = name;
10     }
11 
12     public int getAge() {
13         return age;
14     }
15 
16     public void setAge(int age) {
17         this.age = age;
18     }
19 
20     public String name;
21     public int age;
22 }

2、创建HelloController

 1 package com.han.controller;
 2 
 3 import com.han.entity.User;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.ui.Model;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 
 8 @Controller
 9 @RequestMapping("h1")
10 public class HelloController {
11 
12     @RequestMapping("/hello")
13     public String hello(Model model){
14 
15         model.addAttribute("msg","hello,spring");
16 
17         return "hello";//会被视图解析器处理,找到 hello.jsp
18     }
19 
20     @RequestMapping("/getEntity")
21     public String getEntity(User user,Model model){
22         System.out.println(user.name);
23         model.addAttribute("msg",user.name+":"+user.age);
24         return "hello";
25     }
26 }

3、编写index.jsp

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: han
 4   Date: 2022/4/15
 5   Time: 22:41
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10   <head>
11     <title>$Title$title>
12   head>
13   <body>
14   <form action="${pageContext.request.contextPath}/h1/getEntity" method="post">
15     姓名:<input type="text" name="name"><br/>
16     年龄:<input type="text" name="age"><br/>
17     <input type="submit" value="提  交">
18   form>
19   body>
20 html>

4、编写hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
${msg}
body>
html>