学習目標
- (1)SpringMVC
- (2)SpringMVC-ユーザー
- (3)SpringMVCプロセス
- (4)SpringMVCパラメーター
SpringMVCの紹介
- (1)springmvcとは何ですか?
Springmvcはプレゼンテーションレイヤーのフレームワークに属し、Springフレームワークの一部です。 - (2)springmvcの用途は何ですか?
「リクエストを受信し、パラメータを取得します
」パラメータを処理します
「リダイレクト、リクエスト転送、リターンjsonなどの結果をブラウザに応答します
springmvc就是对Servlet的封装
- (3)springmvcは、モジュールであるspringFrameWorkの一部です。
SpringMVCのシンプルな処理フロー
- (1)springMVCの処理フローはどのようになっていますか?
「コアはフロントコントローラーです
」実際にビジネスを処理するハンドラー
SpringMVC環境のセットアップ-準備
- (1)実装手順:
》》環境の準備
- webapp、pom.xmlパッケージングメソッドwarをコピーします
- jarをインポートします(spring-webmvc.jarまたは依存する構成をコピーします)
- 構成の書き込み
web.xml
フロントコントローラーDispatcherServletを構成し、springmvc.xml
springmvc.xml
構成をロードします。本質は、コントローラーパッケージをスキャンしてオブジェクトを作成するspringのコア構成ファイルです。
- (2)コードを記述します
1)プロセッサクラスを記述し、クラスにメソッドを記述してユーザーの要求を処理します
2)list.jspページを記述します(コンテンツをコピー、変更します)
人
//1,定义实体类
public class Person {
private int id;
private String username;
private String password;
private String city;
Db
//2:假数据
public class Db {
public static List<Person> findAll(){
List<Person> list = new ArrayList<Person>();
list.add(new Person(1,"jack","123456","北京"));
list.add(new Person(2,"rose","123456","changsha"));
list.add(new Person(3,"tony","123456","changsha"));
return list;
}
}
SpringMVC環境の構築-実装
pom.xml
<dependencies>
<!-- 3 servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- spring mvc是对servlet的封装-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
</dependencies>
web.xml
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<!-- 4.2 找到前端控制器-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 4.3 每个框架都有自己的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--4.4让前端控制器去读 正整数,值越小,优先级越高-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!-- 4.1 xxx.action 转给当前servlet-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
springmvc.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"
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">
<!--进行包扫描,创建controller层的对象 只要是指定的包与子包下面有@Controller的类
都要被创建对象,且加入ioc容器中-->
<context:component-scan base-package="com.wzx.controller"/>
</beans>
UserController
@Controller
public class PersonController {
//查询所有的用户的效果
@RequestMapping("list.action")
public ModelAndView listPersons(){
//1:获取集合
List<Person> list = Db.findAll();
System.out.println("listPersons");
//2:转发到页面
// request.setAttribute("list",list)
// request.getDispatcher("/list.jsp").forward(req,resp)
ModelAndView mv = new ModelAndView();//对数据与页面的封装
mv.addObject("list",list);
mv.setViewName("jsp/list.jsp");
//3:在页面上进行forEach循环
return mv;
}
}
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1px" width="100%">
<tr>
<td>编号 </td>
<td>用户名 </td>
<td>密码 </td>
<td>城市 </td>
<td>操作 </td>
</tr>
<c:forEach items="${list}" var="item" >
<tr>
<td>${item.id}</td>
<td>${item.username}</td>
<td>${item.password}</td>
<td>${item.city}</td>
<td><a href="#">修改</a> </td>
</tr>
</c:forEach>
</table>
</body>
</html>
アクセス
http://localhost:8080/web01/list.action