1.参数自动装配(四种)
首先准备一个用户信息类和车类
package demo08ParamAuto01.AutoWire; import java.util.List; /** * Created by mycom on 2018/3/26. */ public class UserInfo { private String username; private String password; private Car car; private List<Car> list; public List<Car> getList() { return list; } public void setList(List<Car> list) { this.list = list; } 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; } }
package demo08ParamAuto01.AutoWire; /** * Created by mycom on 2018/3/26. */ public class Car { private String brand; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } }
在控制器类中
package demo08ParamAuto01.AutoWire; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by mycom on 2018/3/26. */ @Controller public class UserInfoController { //零散参数自动装配 @RequestMapping("/login") public String doLogin(String username, String password, Model model){ model.addAttribute("username",username); System.out.println(username); System.out.println(password); return "success"; } //对象参数自动装配 @RequestMapping("/loginUserInfo") public String doLoginUserInfo(UserInfo info, Model model){ model.addAttribute("username",info.getUsername()); System.out.println(info.getUsername()); System.out.println(info.getPassword()); return "success"; } //域属性自动装配 @RequestMapping("/loginUserInfoCar") public String doLoginUserInfoCar(UserInfo info, Model model){ model.addAttribute("username",info.getUsername()); System.out.println(info.getUsername()); System.out.println(info.getPassword()); System.out.println(info.getCar().getBrand()); return "success"; } //集合自动装配 @RequestMapping("/loginUserInfoList") public String doLoginUserInfoList(UserInfo info, Model model){ model.addAttribute("username",info.getUsername()); System.out.println(info.getUsername()); System.out.println(info.getPassword()); System.out.println(info.getCar().getBrand()); System.out.println(info.getList().get(0).getBrand()); System.out.println(info.getList().get(1).getBrand()); 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="demo08ParamAuto01.AutoWire"></context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
在页面上
<%-- Created by IntelliJ IDEA. User: mycom Date: 2018/3/26 Time: 11:57 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/loginUserInfoList" method="post"> 用户名:<input name="username"/> 密码:<input name="password"/> 爱车:<input name="car.brand"> 车1:<input name="list[0].brand"> 车2:<input name="list[1].brand"> 名:<input name="uname"> <input type="submit" value="提交"> </form> </body> </html>
2.路径变量 @PathVariable
路径参数类似请求参数,但没有key部分,只是一个值。例如下面的URL:
http://localhost:9090/showUser/spring
其中的spring是表示用户的密码字符串。在Spring MVC中,spring被作为路径变量用来发送一个值到服务器。Sping 3以后Spring 3以后支持注解@PathVariable用来接收路径参数。为了使用路径变量,首先需要在RequestMapping注解的值属性中添加一个变量,该变量必须放在花括号之间
//路径变量 @RequestMapping("/loginUserInfoPath/{username}") public String doLoginUserInfoPath(@PathVariable("username") String uname,String username){ System.out.println(uname); System.out.println(username); return "success"; }
不要忘记修改web.xml和springmvc.xml的配置文件的路径和包的路径
在页面上
<%-- Created by IntelliJ IDEA. User: mycom Date: 2018/3/26 Time: 11:57 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/loginUserInfoPath/happy" method="post"> 用户名:<input name="username"/> <input type="submit" value="提交"> </form> </body> </html>