在千锋“逆战”学习第16天
今天初步学习了springmvc
继续加油,奥利给!
课程作业笔记
项目结构
index.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/3/2 0002
Time: 17:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<c:if test="${not empty errors}">
<c:forEach var="error" items="${errors}">
<div class="alert alert-danger fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>提交失败!</strong>${error}
</div>
</c:forEach>
</c:if>
<form action="<%=basePath%>ShowController" method="post">
<div class="container-fluid bg-danger d-flex flex-column align-items-center text-white py-5">
<div class="form-group w-25">
<label for="name">用户名</label>
<input type="text" class="form-control" name="name" id="name">
</div>
<div class="form-group w-25">
<label for="password">密码</label>
<input type="text" class="form-control" name="password" id="password">
</div>
<div class="form-group w-25">
<label for="age">年龄</label>
<input type="text" class="form-control" name="age" id="age">
</div>
<div class="form-group w-25">
<label for="hobby">爱好</label>
<input type="text" class="form-control" name="hobby" id="hobby">
</div>
<div class="form-group w-25">
<input type="submit" class="btn btn-primary" value="提交">
</div>
</div>
</form>
</body>
</html>
User.java 这个类CheckUser差不多
package bean;
/**
* @author xw
* @Title:
* @Package
* @Description:
* @date 2020/3/2 000220:24
*/
public class User {
private int id;
private String name;
private String password;
private int age;
private String hobby;
public User(int id, String name, String password, int age, String hobby) {
this.id = id;
this.name = name;
this.password = password;
this.age = age;
this.hobby = hobby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", age=" + age +
", hobby='" + hobby + '\'' +
'}';
}
}
DispatcherServlet
package servlet;
import controller.Controller;
import controller.ShowController;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author xw
* @Title: ${file_name}
* @Package ${package_name}
* @Description: ${todo}
* @date 2020/3/2 000219:42
*/
@WebServlet(name = "DispatcherServlet", urlPatterns = "/ShowController")
public class DispatcherServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获取请求路径
String requestURI = request.getRequestURI();
//如果带工程名就要截取
requestURI = requestURI.substring(requestURI.lastIndexOf("/")+1);
Controller controller = null;
String returnUrl = null;
if ("ShowController".equals(requestURI)){
controller = new ShowController();
returnUrl = controller.show(request, response);
}
request.getRequestDispatcher(returnUrl).forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
Controller.java接口
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author xw
* @Title:
* @Package
* @Description:
* @date 2020/3/2 000220:05
*/
public interface Controller {
public String show(HttpServletRequest request, HttpServletResponse response);
}
ShowController.java
package controller;
import bean.CheckUser;
import bean.User;
import utils.ParameterCheck;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* @author xw
* @Title:
* @Package
* @Description:
* @date 2020/3/2 000220:06
*/
public class ShowController implements Controller {
@Override
public String show(HttpServletRequest request, HttpServletResponse response) {
//获取参数
String name = request.getParameter("name");
String password = request.getParameter("password");
String a = request.getParameter("age");
int age = a.trim()==""?0:Integer.parseInt(request.getParameter("age"));
String hobby = request.getParameter("hobby");
//校验值是否符合条件
ParameterCheck parameterCheck = new ParameterCheck();
CheckUser checkUser = new CheckUser(name, password, age, hobby);
List<String> errors = parameterCheck.check(checkUser);
if (errors.size()!=0){
//参数不合法
request.setAttribute("errors",errors);
return "index.jsp";
}
//验证成功后跳转
User user = new User(1, name, password, age, hobby);
request.setAttribute("user",user);
return "WEB-INF/view/show.jsp";
}
}
ParameterCheck.java
package utils;
import bean.CheckUser;
import java.util.ArrayList;
import java.util.List;
/**
* @author xw
* @Title:
* @Package
* @Description:
* @date 2020/3/2 000220:17
*/
public class ParameterCheck {
public List<String> check(CheckUser user){
//创建错误集合
List<String> errors = new ArrayList<>();
//取值
String name = user.getName();
String password = user.getPassword();
int age = user.getAge();
String hobby = user.getHobby();
//验证属性
if ("".equals(name.trim())||name==null){
errors.add("用户名不能为空!");
}
if ("".equals(password.trim())||password==null){
errors.add("密码不能为空!");
}
if (age<=0){
errors.add("年龄输入不合法!");
}
if ("".equals(hobby.trim())||hobby==null){
errors.add("兴趣爱好不能为空!");
}
return errors;
};
}
show.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/3/2 0002
Time: 19:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<table class="table table-bordered text-center">
<thead class="thead-dark">
<tr>
<th>用户名</th>
<th>密码</th>
<th>年龄</th>
<th>爱好</th>
</tr>
</thead>
<tbody>
<tr>
<td>${user.name}</td>
<td>${user.password}</td>
<td>${user.age}</td>
<td>${user.hobby}</td>
</tr>
</tbody>
</table>
</body>
</html>
效果截图