概述:
详细:
环境配置
IntelliJ IDEA 2019.3.3
apache-tomcat-9.0.35
JDK:1.8
Spring-framework: 4.2.6.RELEASE
1、建一个文件夹叫springmvc
2、新建项目
File-New,选择project,勾选Web Application
点击next,Project location选择刚刚新建的文件夹的路径
3、导入jar包
在web-WEB-INF下新建lib文件夹,再讲jar导入进该文件夹
将所有jar包选中-右击选择add as library
3、tomcat根目录下的lib文件夹中也要添加jar包
4、建一个文件夹叫config
右击 - mark directory as - source root 让config变蓝
5、在src中建三级包
6、在config中导入配置文件
7、修改配置文件 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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- @Controller注解扫描 -->
<context:component-scan base-package="com.lm.controller"></context:component-scan>
<!-- 注解驱动:
替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->
<mvc:annotation-driven ></mvc:annotation-driven>
<!-- 配置视图解析器
作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
下列代码放入web-app标签中
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- springmvc前端控制器 -->
<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:SpringMvc.xml</param-value>
</init-param>
<!-- 在tomcat启动的时候就加载这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置Post请求乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*
配置工作结束。
9、建立HelloController类(包名com.lm.Controller下):
首先要在类的前面添加“Controller”注解,告诉编辑器这是springmvc的控制器类,这里会写一个方法hello()
package com.lm.controller;
import com.lm.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model){
List<Student> students = new ArrayList<>();
students.add(new Student(100,"张三","计算机"));
students.add(new Student(101,"李四","物联网"));
students.add(new Student(102,"王五","大数据"));
model.addAttribute("students",students);
return "hello";
}
}
10、建立Student类(包名com.lm.pojo下(新建的)):
三个属性,自动生成get、set方法,一个无参构造函数和一个有参构造函数
package com.lm.pojo;
import java.io.Serializable;
public class Student implements Serializable {
private Integer id;
private String name;
private String major;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public Student() {
}
public Student(Integer id, String name, String major) {
this.id = id;
this.name = name;
this.major = major;
}
}
11、新建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach items="${students}" var="std">
<div>${std.id} ${std.name} ${std.major}</div>
</c:forEach>
</body>
</html>
12、index.jsp中body改为
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<div><a href="${pageContext.request.contextPath}/hello">学生列表</a></div>
</body>
</html>