1.导入架包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>SpringMVC</artifactId>
<groupId>cn.happy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringMVC04Annotation</artifactId>
<packaging>war</packaging>
<name>SpringMVC04Annotation Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--spring对应的版本号-->
<spring.version>4.2.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
<!--引入需要的spring 核心jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC04Annotation</finalName>
</build>
</project>
2.配置*调度器(乱码处理、处理器)
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name> <filter>
<filter-name>CharactorEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>CharactorEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
3、配置映射器
<?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.happy.controller"/>
<!--根据注解来寻找路径-->
<mvc:annotation-driven/>
</beans>
4.1:视图(登录页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>Login</h2>
<form action="/user/login" method="post"> 这里的红色字体要根据RequestMapping的请求方法来进行更换
登陆名:<input name="name"/> 校正参数名称时,这里要改成 uname
汽车品牌1:<input name="userCars[0].brand"/>
汽车品牌2:<input name="userCars[1].brand"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
4.2:登录成功页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>Success!</h2>
<%--${uname}--%> 在最后一个案例要放开注解
<%--${list}--%> 在最后一个案例要放开注解
</body>
</html>
5.1:实体类(用户)
import java.util.List;
public class UserInfo {
private String name;
private Car car;
private List<Car> userCars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public List<Car> getUserCars() {
return userCars;
}
public void setUserCars(List<Car> userCars) {
this.userCars = userCars;
}
}
5.2:实体类(用户的域属性)
public class Car {
private String brand;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
6.处理器(通配符的用法、请求方法的定义)
import cn.happy.bean.UserInfo;
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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
@Controller
@RequestMapping("/user")
public class FirstController {
//*代表0个或者多个字符,以带有first的字符,或者开头、结尾就行
@RequestMapping("/*first*")
public String doFirst() {
return "/success.jsp";
}
//**:0级或者是无限极目录
@RequestMapping("/**/second")
public String doSecond() {
return "/success.jsp";
}
//*:有且只能有一级目录,不能是空目录
@RequestMapping(value = "/*/third", method = RequestMethod.GET)
public String doThird() {
return "/success.jsp";
}
//对象的属性获取和自定义类型的属性获取
@RequestMapping("/login")
public String doFirst(UserInfo info) {
System.out.println(info.getName());
System.out.println(info.getUserCars().get(0).getBrand());
System.out.println(info.getUserCars().get(1).getBrand());
return "/success.jsp";
}
跳转到
控制台
//校正参数名称 将上方的请求路径更改成/user/second 和表单元素的name属性保持一致,即可实现自动装配
@RequestMapping("/second")
public String doSecond(@RequestParam(value = "uname", required = false) String info) {
System.out.println(info);
return "/success.jsp";
}
跳转到
控制台
//路径变量
@RequestMapping("/{runame}/{uage}/third")
public String Third(@PathVariable("runame") String uname, @PathVariable String uage) {
System.out.println(uname);
System.out.println(uage);
return "/success.jsp";
}
控制台
//session和Model的使用
@RequestMapping(value = "/four")
public String doFirst(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {
session.setAttribute("uname", "happy");
model.addAttribute("list", new ArrayList<String>());
return "/success.jsp";
}
}