------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
在处理方法中,参数写上之后,只要符合特定规则,就可以自动装配
首先
其次是:自定义的参数的自动装配:
案例如下:我的相同的配置文件就在下面不重复展示,不同的就展示一下:
第一种,零散参数自动装配:
在处理器中定义方法:
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="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;@Overridepublic 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;@Overridepublic 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;@Overridepublic 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里面的那个值
延伸出来一个问题:传中文会乱码,怎么办?看下篇博客