SSM-SpringMVC-18:SpringMVC中参数自动装配

释放双眼,带上耳机,听听看~!

 

 

 ********** 
————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-**********

 

 

在处理方法中,参数写上之后,只要符合特定规则,就可以自动装配

首先

SSM-SpringMVC-18:SpringMVC中参数自动装配

 

 

其次是:自定义的参数的自动装配:

案例如下:我的相同的配置文件就在下面不重复展示,不同的就展示一下:

 

 

第一种,零散参数自动装配:

在处理器中定义方法:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1package cn.dawn.day11autowire;
2
3import org.springframework.stereotype.Controller;
4import org.springframework.ui.Model;
5import org.springframework.web.bind.annotation.PathVariable;
6import org.springframework.web.bind.annotation.RequestMapping;
7import org.springframework.web.bind.annotation.RequestMethod;
8import org.springframework.web.bind.annotation.RequestParam;
9
10/**
11 * Created by Dawn on 2018/3/24.
12 */
13/*注解定义处理器*/
14@Controller
15/*定义处理器访问路径*/
16@RequestMapping("/user")
17public class MyAController {
18
19
20    /*第一种零散根据name*/
21    @RequestMapping("/doLogin")
22    public String doLogin(String username,String password, Model model) throws Exception {
23        model.addAttribute("username",username);
24        System.out.println(username);
25        System.out.println(password);
26        return "success";
27    }
28
29}
30

在自定义配置文件中:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
4       xmlns:context="http://www.springframework.org/schema/context"
5       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6
7    <!--包扫描器-->
8    <context:component-scan base-package="cn.dawn.day11autowire"></context:component-scan>
9    <!--视图解析器-->
10    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
11        <property name="prefix" value="/"></property>
12        <property name="suffix" value=".jsp"></property>
13    </bean>
14
15</beans>
16

 

xml配置文件中只需要包扫描器和视图解析器

jsp页面:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
2<html>
3<head>
4    <title>Title</title>
5</head>
6<body>
7<h2>登录</h2>
8<form action="${pageContext.request.contextPath}/user/doLogin" method="post">
9    <%--第一种自动装配零散参数--%>
10    用户名:<input name="username">
11    <input type="submit" value="登录"/>
12</form>
13</body>
14</html>
15

第二个jsp页面:success.jsp

 


1
2
3
4
5
6
7
8
9
1<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
2<html>
3<body>
4<%--<img src="image/1.jpg">--%>
5<h2>Success!</h2>
6<p>${username}</p>
7</body>
8</html>
9

 

在web.xml中需要将指定的xml文件改成现在的即可

 

第二种:当写前台提交的name的属性名和实体类字段名或者参数名的不是一个人,有可能出现不一致,怎么解决

处理方法:

 


1
2
3
4
5
6
7
8
9
1    /*第二种零散参数name不一致*/
2    @RequestMapping("/doLoginBie")
3    public String doLoginBie(@RequestParam("uname") String username, String password, Model model) throws Exception {
4        model.addAttribute("username",username);
5        System.out.println(username);
6        System.out.println(password);
7        return "success";
8    }
9

 

form表单中的值,记得改action指向的地址,改为新的处理方法

 


1
2
3
1    <%--第二种自动装配零散参数  别名--%>
2    用户名:<input name="uname">
3

 

第二种用到了@RequestParam("xxx")的这个注解

 

第三种:对象参数自动装配:只要表单里的name属性与对象的字段对应,就可以自动装配

此处我将最终版实体类对象发上来(因为案例全做完才写的博客,不好删减):UserInfo实体类:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
1package cn.dawn.day11autowire;
2
3
4import java.util.List;
5
6/**
7 * Created by Dawn on 2018/3/26.
8 */
9public class UserInfo {
10    private String username;
11    private String password;
12
13    private Car car;
14
15    private List<Girls> girlsList;
16
17
18    @Override
19    public String toString() {
20        return "UserInfo{" +
21                "username='" + username + '\'' +
22                ", password='" + password + '\'' +
23                ", car=" + car +
24                ", girlsList=" + girlsList +
25                '}';
26    }
27
28    public List<Girls> getGirlsList() {
29        return girlsList;
30    }
31
32    public void setGirlsList(List<Girls> girlsList) {
33        this.girlsList = girlsList;
34    }
35
36    public Car getCar() {
37        return car;
38    }
39
40    public void setCar(Car car) {
41        this.car = car;
42    }
43
44    public String getUsername() {
45        return username;
46    }
47
48    public void setUsername(String username) {
49        this.username = username;
50    }
51
52    public String getPassword() {
53        return password;
54    }
55
56    public void setPassword(String password) {
57        this.password = password;
58    }
59}
60

第三个案例只用到了前俩个字段,我们看一下jsp中的form中的内容:记得改action指向的地址,改为新的处理方法

 


1
2
3
1    用户名:<input name="username">
2    密码:<input name="password">
3

 

 

 

第四种:当有域属性的时候怎么办?

用到了UserInfo的car这个字段,所以,建一个Car的实体类:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1package cn.dawn.day11autowire;
2
3/**
4 * Created by Dawn on 2018/3/26.
5 */
6public class Car {
7    private String type;
8
9    @Override
10    public String toString() {
11        return "Car{" +
12                "type='" + type + '\'' +
13                '}';
14    }
15
16    public String getType() {
17        return type;
18    }
19
20    public void setType(String type) {
21        this.type = type;
22    }
23}
24

 

处理方法如下:

 


1
2
3
4
5
6
7
8
1    /*第三种对象参数传*/
2    @RequestMapping("/doLoginObject")
3    public String doLoginObject(UserInfo info, Model model) throws Exception {
4        model.addAttribute("username",info.getUsername());
5        System.out.println(info);
6        return "success";
7    }
8

jsp页面from表单中的如下:action指向新的处理方法地址:

 


1
2
3
4
5
1    用户名:<input name="username">
2    密码:<input name="password">
3    <%--第四种带域属性--%>
4    车:<input name="car.type">
5

这儿需注意的是car.type这个写法

 

第五种:集合自动装配

在实体类中用到了girlsList这个对象List集合字段:所以我们建一个Girls表:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1package cn.dawn.day11autowire;
2
3/**
4 * Created by Dawn on 2018/3/26.
5 */
6public class Girls {
7    private String cool;
8
9    @Override
10    public String toString() {
11        return "Girls{" +
12                "cool='" + cool + '\'' +
13                '}';
14    }
15
16    public String getCool() {
17        return cool;
18    }
19
20    public void setCool(String cool) {
21        this.cool = cool;
22    }
23}
24

 

处理方法如下:

 


1
2
3
4
5
6
7
8
1    /*第五种对象携带集合对象参数传*/
2    @RequestMapping("/doLoginObjectList")
3    public String doLoginObjectList(UserInfo info, Model model) throws Exception {
4        model.addAttribute("username",info.getUsername());
5        System.out.println(info);
6        return "success";
7    }
8

 

form表单内容如下:action指向新地址:

 


1
2
3
4
5
6
7
8
1    用户名:<input name="username">
2    密码:<input name="password">
3    车:<input name="car.type">
4    <%--第五种带List集合--%>
5    女1:<input name="girlsList[0].cool">
6    女2:<input name="girlsList[1].cool">
7    <input type="submit" value="登录"/>
8

 

 

第六种:路径参数自动装配:是可以通过占位的方式来写的:

处理方法如下:

 


1
2
3
4
5
6
7
8
1    /*第六种路径参数传法1*/
2    @RequestMapping("/doPathVariable01/{uname}")
3    public String doPathVariable01(@PathVariable("uname") String username, Model model) throws Exception {
4        model.addAttribute("username",username);
5        System.out.println(username);
6        return "success";
7    }
8

 

URL访问只需要在处理器方法之后加           /值      就可以自动装配

用到了注解@PathVariable()和@RequestMapping中的{占位符}

 

延伸出来一个问题:如果form传过来的name值和action访问的Url地址中占位的那个参数一样,会出现什么

第七种:

处理器方法:

 


1
2
3
4
5
6
7
8
1    /*第七种路径参数传法2*/
2    @RequestMapping("/doPathVariable02/{username}")
3    public String doPathVariable02(@PathVariable String username, Model model) throws Exception {
4        model.addAttribute("username",username);
5        System.out.println(username);
6        return "success";
7    }
8

 

jsp页面:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
2<html>
3<head>
4    <title>Title</title>
5</head>
6<body>
7<h2>登录</h2>
8<form action="${pageContext.request.contextPath}/user/doPathVariable02/happy" method="post">
9
10    用户名:<input name="username">
11
12    <input type="submit" value="登录"/>
13</form>
14</body>
15</html>
16

 

结果肯定是拿到URL里面的那个值

 

延伸出来一个问题:传中文会乱码,怎么办?看下篇博客

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++ 高性能服务器网络框架设计细节

2022-1-11 12:36:11

安全漏洞

lighttpd 1.4.x 爆高危漏洞

2010-2-2 11:12:22

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索