在main目录下新建directory,命名为webapp,在java文件夹下建立如图所示的目录结构
war
org.springframework
spring-webmvc
5.2.10.RELEASE
org.springframework
spring-jdbc
5.2.10.RELEASE
ch.qos.logback
logback-classic
1.2.3
javax.servlet
javax.servlet-api
3.0.1
provided
org.thymeleaf
thymeleaf-spring5
3.0.12.RELEASE
mysql
mysql-connector-java
5.1.47
runtime
com.alibaba
druid
1.1.12
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
/
8081
然后点击IDEA右上角的刷新,IDEA会自动下载依赖、完成配置
完成上面的操作后webapp图标会变为下图所示的样式
PageBean中的内容
package com.tencent.mvc.util;
import java.util.List;
public class PageBean {
//页号的默认值
public static final Integer DEFAULT_PAGENO=1;
//每页显示的记录数的默认值
public static final Integer DEFAULT_PAGESIZE=5;
//当前页码
private Integer pageNo;
//每页的记录数
private Integer pageSize;
//总页数
private Integer totalPage;
//总记录数
private Integer totalCount;
//当前页的数据集合
private List data;
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
//返回总页数
public Integer getTotalPage() {
if (this.totalCount % this.pageSize == 0) {
return this.totalCount / this.pageSize;
}
return this.totalCount / this.pageSize + 1;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
}
WebRequestUtil中的内容
package com.tencent.mvc.util;
import javax.servlet.http.HttpServletRequest;
/**
* web请求的工具类,封装请求处理过程的重复代码
*/
public class WebRequestUtil {
/**
*通用的根据请求参数名获取其值的方法
* @param paramName 请求参数名
* @param defaultValue 默认值
* @param request 请求对象
* @return 返回参数的整型
*/
public static Integer getParamValue(String paramName, Integer defaultValue, HttpServletRequest request){
String str = request.getParameter(paramName);
Integer value=defaultValue;
if(null!=str){
value=Integer.parseInt(str);
}
return value;
}
}
templates目录用来存放我们的前端html页面
web.xml文件中的内容
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceResponseEncoding
true
CharacterEncodingFilter
/*
contextConfigLocation
classpath:beans.xml
org.springframework.web.context.ContextLoaderListener
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
1
springMVC
/
根据web.xml文件的配置,可以发现需要在resources目录下创建beans.xml和springMVC.xml文件
beans.xml文件内容如下:
在beans.xml文件中需要读取与数据库连接相关的配置文件druid.properties
druid.properties文件的内容:
# key=value
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false&useServerPrepStmts=true
jdbc.username=root
jdbc.password=mysql数据库密码
springMVC.xml文件的内容
数据库中的表结构
在pojo文件夹中新建Job.java内容如下
public class Job {
private Integer id;
private String janem;
private String description;
private Integer need;
@Override
public String toString() {
return "Job{" +
"id=" + id +
", janem='" + janem + '\'' +
", description='" + description + '\'' +
", need=" + need +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getJanem() {
return janem;
}
public void setJanem(String janem) {
this.janem = janem;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getNeed() {
return need;
}
public void setNeed(Integer need) {
this.need = need;
}
public Job() {
}
public Job(Integer id, String janem, String description, Integer need) {
this.id = id;
this.janem = janem;
this.description = description;
this.need = need;
}
}