一.SpringMVC
MVC框架是由Model模型(JavaBean),View视图(Jsp) 和 Controller控制器(Servlet)构成。
其中每一部分的作用为:
- Model:承载数据,并对用户提交请求进行计算的模块。其分为两类,一类称为数据承载Bean,一类称为业务处理Bean。所谓数据承载Bean是指实体类,专门承载业务数据的,如Student、User等。而业务处理Bean则是指Service或Dao对象,专门用于处理用户提交请求的。
- View:为用户提供使用界面,与用户直接进行交互
- Controller:用于将用户请求转发给相应的Model进行处理,并将处理Model的计算结果向用户提供相应响应。
二.SpringMVC+mybatis各层关系
Controller层(springMVC)通过要调用Service层的接口来控制业务流程,负责url映射(action),控制界面跳转。
Service层(Spring)负责业务模块的逻辑应用设计。首先设计其接口,然后再实现他的实现类(Serviceimpl类)。
通过对Spring配置文件中配置其实现的关联,完成此步工作,我们 就可以通过调用Service的接口来进行业务处理。最后通过调用DAO层已定义的接口,去实现Service具体的实现类。
Dao层/Mapper层(Mybatis)负责与数据库进行交互设计,用来处理数据的持久化工作。DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,就可在其他模块中调用此接口来进行数据业务的处理,而不用关心接口的具体实现类是哪个类,这里用到的就是反射机制, DAO层的数据源配置,以及有关数据库连接的参数都在Spring的配置文件中进行配置。
View层(视图层)负责前台jsp页面的展示。
二.项目简介
简单的用户管理项目:实现用户登录注册,以及对用户信息的增删改查功能。
项目流程图
四.源码实现
文件目录
JSP文件:
1.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<title>登录页面</title>
</head>
<body style="background:url('http://localhost:8080/maven02/resources/img/backimg.jpg') no-repeat;background-size: cover;">
<div style="width:33% ;height:300px;margin:100px auto;border-radius:25px;;box-shadow: 5px 5px 5px #888888;background-color: white">
<img src="http://localhost:8080/maven02/resources/img/IDcard.png" style="width: 50px;height: 50px;position:absolute;left:649px;top:76px;"/>
<form class="form-horizontal" method="POST" id="form" style="position:absolute;left:492px;top:152px;">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label" style="padding-right:5px">用户名:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="username" id="inputEmail3"style="width:285px;" >
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label" style="padding-right:5px">密码:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword3" name="password" style="width:285px;">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10" style="width:202px;margin:10px auto;float:left">
<button type="submit" class="btn btn-default btn-info" style="width:170px; margin:10px auto" onclick="login()">登录</button>
</div>
<div class="col-sm-offset-2 col-sm-10" style="width:202px;margin:10px auto;float:left">
<button type="submit" class="btn btn-default btn-info "style="width:170px; margin:10px auto" onclick="registe()">注册</button>
</div>
</div>
</div>
</form>
<!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
<script src="https://code.jquery.com/jquery.js"></script>
</body>
<script type="text/javascript">
var login=function(){
$("#form").attr("action","http://localhost:8080/maven02/user/userlogin");
$("#form").submit();
};
var registe=function(){
$("#form").attr("action","http://localhost:8080/maven02/user/registration");
$("#form").submit();
};
</script>
</html>
2.registration.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<title>修改用户信息页面</title>
</head>
<body>
<div style="width:50%;margin:10px auto">
<div style="height:70px;">
<h1 style="font: bold em Brush Script MT ; color:#222; text-shadow: 0px 2px 3px #666;float:left;">User Management<h1/>
<img src="http://localhost:8080/maven02/resources/img/List.png" style="width: 50px;height: 50px;float:left;margin-top: 13px;"/>
</div>
<form class="form-horizontal" action="http://localhost:8080/maven02/user/useradd" method="POST">
<div class="form-group">
<label class="col-sm-2 control-label">用户名:</label>
<div class="col-sm-10 center-block">
<input type="text" class="form-control" name="username" id="inputEmail3">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword3" name="password">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">联系方式</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword3" name="phonenum">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">确定</button>
</div>
</div>
</form>
</div>
<!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- 包括所有已编译的插件 -->
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
$(function(){
if(""=="${code}"){
console.log("没有异常");
}else if("500"=="${code}"){
alert("${msg}");
}
});
</script>
</body>
</html>
3.userList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<jsp:include page="../_header.jsp" />
</head>
<body>
<div style="width:80%;margin:10px auto">
<div style="height:70px;">
<h1 style="font: bold em Brush Script MT ; color:#222; text-shadow: 0px 2px 3px #666;float:left;">User Management<h1/>
<img src="http://localhost:8080/maven02/resources/img/List.png" style="width: 50px;height: 50px;float:left;margin-top: 13px;"/>
</div>
<form id="select" method="post" action="http://localhost:8080/maven02/user/select" style="margin-left: 631px">
<input id="selectbyname" class="form-control" type="text" name="selectbyname" placeholder="请输入用户名" style="width:200px;float:left;margin-top: 19px;"/>
<div class="col-sm-offset-2 col-sm-10 form-group" style="width:202px;margin:10px auto;float:left">
<button type="submit" class="btn btn-default btn-info" style="width:170px; margin:10px auto">查询</button>
</div>
</form>
<form id="form1" method="post">
<table class="table table-hover table-striped">
<tr>
<th>用户账号</th>
<th>用户密码</th>
<th>联系方式</th>
<th>操作</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.user_name }</td>
<td>${user.user_password }</td>
<td>${user.user_phonenum }</td>
<td>
<div cl