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

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

首先

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

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

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

  在处理器中定义方法:

package cn.dawn.day11autowire;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; /**
* Created by Dawn on 2018/3/24.
*/
/*注解定义处理器*/
@Controller
/*定义处理器访问路径*/
@RequestMapping("/user")
public class MyAController { /*第一种零散根据name*/
@RequestMapping("/doLogin")
public String doLogin(String username,String password, Model model) throws Exception {
model.addAttribute("username",username);
System.out.println(username);
System.out.println(password);
return "success";
} }

  在自定义配置文件中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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"> <!--包扫描器-->
<context:component-scan base-package="cn.dawn.day11autowire"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

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

  jsp页面:

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

  第二个jsp页面:success.jsp

<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
<html>
<body>
<%--<img src="data:image/1.jpg">--%>
<h2>Success!</h2>
<p>${username}</p>
</body>
</html>

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

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

  处理方法:

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

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

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

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

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

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

package cn.dawn.day11autowire;

import java.util.List;

/**
* Created by Dawn on 2018/3/26.
*/
public class UserInfo {
private String username;
private String password; private Car car; private List<Girls> girlsList; @Override
public String toString() {
return "UserInfo{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", car=" + car +
", girlsList=" + girlsList +
'}';
} public List<Girls> getGirlsList() {
return girlsList;
} public void setGirlsList(List<Girls> girlsList) {
this.girlsList = girlsList;
} public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

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

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

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

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

package cn.dawn.day11autowire;

/**
* Created by Dawn on 2018/3/26.
*/
public class Car {
private String type; @Override
public String toString() {
return "Car{" +
"type='" + type + '\'' +
'}';
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
}
}

  处理方法如下:

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

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

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

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

第五种:集合自动装配

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

package cn.dawn.day11autowire;

/**
* Created by Dawn on 2018/3/26.
*/
public class Girls {
private String cool; @Override
public String toString() {
return "Girls{" +
"cool='" + cool + '\'' +
'}';
} public String getCool() {
return cool;
} public void setCool(String cool) {
this.cool = cool;
}
}

  处理方法如下:

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

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

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

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

  处理方法如下:

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

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

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

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

第七种:

  处理器方法:

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

  jsp页面:

<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>登录</h2>
<form action="${pageContext.request.contextPath}/user/doPathVariable02/happy" method="post"> 用户名:<input name="username"> <input type="submit" value="登录"/>
</form>
</body>
</html>

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

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

SSM-SpringMVC-18:SpringMVC中参数自动装配的更多相关文章

  1. Spring中的自动装配

    src\dayday\Person.java package dayday;/** * Created by I am master on 2016/11/28. */public class Per ...

  2. spring第一课,beans配置(中)——自动装配

    •Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式 •byType(根据类型自动装配): 若 IOC 容器 ...

  3. Spring中类型自动装配--byType

    在Spring中,“类型自动装配”的意思是如果一个bean的数据类型与其它bean属性的数据类型相同,将自动兼容装配它. 例如,一个“persion” bean 公开以“ability”类数据类型作为 ...

  4. MVC参数自动装配

    在拿到一个类型的所有属性以及字段的描述信息后,就可以通过循环的方式,根据这些数据成员的名字去QueryString,Form,Session,Cookie读取所需的数据了. 就是遍历参数,然后用反射遍 ...

  5. Spring boot 梳理 - 在bean中使用命令行参数-自动装配ApplicationArguments

    If you need to access the application arguments that were passed to SpringApplication.run(…​), you c ...

  6. SSM框架的sql中参数注入(#和$的区别)

    <select id="findUsersByUserName2" resultType="java.util.Map" parameterType=&q ...

  7. 切记切记:Spring配置文件中,Component-scan无法扫描到的类中的自动装配对象无法被调用,报空指针错误。

    Spring单例注入,单例对象可设置成Spring元件. 只有Spring的元件中@Autowired才有用,在普通类中@Autowired虽然不会编译报错,但运行时会报空指针错误.

  8. Spring学习--xml 中 Bean 的自动装配

    Spring IOC 容器可以自动装配 Bean. 只要在 <bean> 的 autowire 属性里指定自动装配的模式. byName(根据名称自动装配):必须将目标 Bean 的名称和 ...

  9. SpringBoot启动代码和自动装配源码分析

    ​ 随着互联网的快速发展,各种组件层出不穷,需要框架集成的组件越来越多.每一种组件与Spring容器整合需要实现相关代码.SpringMVC框架配置由于太过于繁琐和依赖XML文件:为了方便快速集成第三 ...

随机推荐

  1. SpriteBuilder中时间线播放音效的弊端

    当你美滋滋的在时间线中播放音效的时候,你要想到音效时间线并不适于播放同步于游戏事件的声音,比如碰撞和加速时. 它同样不能被用来播放背景循环的声音,这就本质上拒绝了通过timeline播放背景音乐.甚至 ...

  2. DEVICE_ATTR

    说道sysfs接口,就不得不提到函数宏 DEVICE_ATTR,原型是 #define DEVICE_ATTR(_name, _mode, _show, _store) \ struct device ...

  3. linux服务搭建----ftp与ftp yum源搭建

    ftp服务     如果没有ftp         yum -y install  vsftpd (前提是你在有yum源的情况下才可以使用这条命令)     service vsftpd  resta ...

  4. 携程Android App的插件化和动态加载框架

    携程Android App的插件化和动态加载框架已上线半年,经历了初期的探索和持续的打磨优化,新框架和工程配置经受住了生产实践的考验.本文将详细介绍Android平台插件式开发和动态加载技术的原理和实 ...

  5. android bitmap的内存分配和优化

    首先Bitmap在Android虚拟机中的内存分配,在Google的网站上给出了下面的一段话 大致的意思也就是说,在Android3.0之前,Bitmap的内存分配分为两部分,一部分是分配在Dalvi ...

  6. c/c++ 表达式求值

    表达式求值 [问题描述] 一个算术表达式是由操作数(operand).运算符(operator)和界限符(delimiter)组成的.假设操作数是正整数,运算符只含加减乘除等四种运算符,界限符有左右括 ...

  7. linux上部署rmi+memcache服务

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/50020437 最近在学习linux上搭建Rmi+Memca ...

  8. Activity之间传递大数据问题

    Android开发人员都知道,Intent适用于在不同的Activity之间传递数据,包括参数.字符串.以及序列化的对象等.但是笔者所做的项目用到了使用Intent 传递Bitmap图片对象,图片的数 ...

  9. MOOS学习笔记4——独立线程不同回调

    MOOS学习笔记4--独立线程不同回调 /** * @fn 独立线程不同回调 * @version v1.0 * @author */ #include "MOOS/libMOOS/Comm ...

  10. 使用padding后内容超出父级元素

    解决方法: