刚开始学习Spring MVC,之前了解过MVC模式,因此学习这个的时候,也不是那么困难.先上代码:
项目文件目录:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/Spring-mvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Spring-mvc-servlet.xml: <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/Spring-mvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Login.java package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author myvina@qq.com
* @date 18-4-20 下午6:54
*/
@Controller
public class Login {
@RequestMapping("/login")
public String index(Model model) {
model.addAttribute("greetings", "Hello, Spring!");
return "home/login";
}
@RequestMapping("/doLogin")
public String doLogin(@RequestParam("name") String name,
@RequestParam("password") String password, Model model) {
model.addAttribute("name", name);
model.addAttribute("password", password);
return "home/welcome";
}
}
Login.jsp <%--
Created by IntelliJ IDEA.
User: vina
Date: 18-4-20
Time: 下午6:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>HomePage</title>
</head>
<body>
<h2>${greetings}</h2>
<form action="/doLogin" method="get">
<input type="text" name="name">
<br>
<input type="password" name="password">
<br>
<input type="submit" value="登录"/>
</form>
</body>
</html>
Welcome.jsp <%--
Created by IntelliJ IDEA.
User: vina
Date: 18-4-22
Time: 上午9:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>${name}, welcome.</h1>
<h1>Your password is ${password}</h1>
</body>
</html>
以上就是对Spring MVC的一次初尝试.
(本人觉得这篇博客讲解得非常好)