1.导入相关依赖jar包
<!--springIoc的依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--AOP切面依赖包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
<!--springMVC的依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
2.web.xml
<!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>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个参数,Spring默认加载web-inf/applicationContext.xml文件。-->
<param-name>contextConfigLocation</param-name> <!--当servlet第一次被请求时,加载这个xml-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
</web-app>
3.a.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="first/a.spring">去a方法</a>
<a href="first/b.spring">去b方法</a>
</body>
</html>
4.index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Hello World!
</body>
</html>
5.c.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="first/b.spring">
账号:<input type="text" name="name"/>
密码:<input type="password" name="pass"/>
<!--这里面的name要和bean包的属性名一致/就具体情况而定name要相对应-->
<button>登录</button>
</form>
</body>
</html>
6.FirstController.java
@Controller
//这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上。
@RequestMapping("/first")
public class FirstController {
//如下面代码所示,到 /first/a 的请求会由 a 方法来处理。
@RequestMapping("/a")
public String a(){
System.out.println("执行了FirstController的a方法!");
return "abcd";
}
@RequestMapping("/b")
public String b(){
System.out.println("执行了FirstController的b方法!");
return "dddd";//这里相当于返回dddd,然后与applicationContext.xml中的视图解析器拼接,形成以.html结尾的路径,然后访问此html页面
}
@RequestMapping("/c")
public String c(String name,String pass){
System.out.println(name);
System.out.println(pass);
return "index";
}
@RequestMapping("/d")
public String d(UserInfo userInfo){
System.out.println(userInfo);
return "index";
}
@RequestMapping("/e")
public String e(HttpSession session){
session.setAttribute("A","zhangsan");
System.out.println(session.getAttribute("A"));
return "index";
}
@RequestMapping("/f")
public String f(){
System.out.println("fff");
return "redirect:/index.html";//重定向
}
@RequestMapping("/g1")
public void g1(HttpServletResponse response) throws IOException {
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.print("Hello,World!");
}
@RequestMapping(value = "/g2",produces = {"application/json;charset=utf-8"})
@ResponseBody //作用是将方法的返回值以特定的格式写入到response的body区域,进而将数据返回给客户端。
public String g2(){
return "g2 Hello,World!";
}
}
7.applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<context:annotation-config/><!--加载IOC注解-->
<context:component-scan base-package="controller"/><!--扫描相应包-->
<mvc:annotation-driven/><!--加载mvc注解-->
<!--视图解析器:相当于拼装-->
<bean id="irvr" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/> <!--前缀-->
<property name="suffix" value=".html"/> <!--后缀-->
</bean>
</beans>
8.访问a.html时
9.访问c.spring时
10.访问d.spring时
11.访问e.spring时
12.访问f.spring时
13.访问g1.spring时
14.访问g2.spring时