2021暑期学习第2天

本文介绍了如何在2021年7月13日使用Spring MVC进行模块整合,创建了SysController和UserController,展示了Controller中的CRUD操作,包括页面跳转和数据操作的Java代码实现。
摘要由CSDN通过智能技术生成

前言

  • 时间:2021.7.13

  • 内容:

    • 新建了模块mvc-demo1
    • 以前学的doxx.java和xxServlet.java,现在全部整合成了xxController.java
    • 实现了页面的查改增。
  • 备注:

    • 这部分的总结需要再去回顾下14:48的内容。(理清跳转)

      web.xml --> mvc-servlet.xml --> UserController.java

代码

目录

在这里插入图片描述

SysController.java

package com.pro.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Yuhua
 * @since 21.7.13 10:06
 */

//加上这个注解,表明这个类是控制器
@Controller
public class SysController {

    //映射的index方法的访问名称
    @RequestMapping("/aaa")
    public String aaa(){
        System.out.println("--SysController--index--");
        return "index"; /*/WEB-INF/jsp/填充.jsp*/
    }
}

UserController.java

package com.pro.controller;

import com.pro.domain.User;
import com.pro.util.DataUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Yuhua
 * @since 21.7.13 14:14
 */

@Controller
@RequestMapping("/user")
public class UserController {

    //将数据库的数据查出来,去users.jsp中显示
    @RequestMapping("/toList")
    public ModelAndView toList(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("users");
        mav.addObject("userList", DataUtil.userList);
        //这里是放在了requsert中
        //mav.addObject("a", "aa");
        return mav;
    }


    //删除
    @RequestMapping("/deleteUser")
    public String deleteUser(int userId){
        List<User> userList = DataUtil.userList;
        for (int i = 0; i < userList.size(); i++) {
            if(userId==DataUtil.userList.get(i).getUserId()){
                DataUtil.userList.remove(i);
                break;
            }
        }
        return "redirect:/user/toList";
    }

    //修改1
    @RequestMapping("/updateUser")
    public ModelAndView updateUser(int userId){
        User nowUser = null;
        for (int i = 0; i < DataUtil.userList.size(); i++) {
            if(userId == DataUtil.userList.get(i).getUserId()){
                nowUser = DataUtil.userList.get(i);
                break;
            }
        }
        ModelAndView mav = new ModelAndView();
        mav.setViewName("updateUser");
        mav.addObject("nowUser", nowUser);
        return mav;
    }

    //修改2
    @RequestMapping("/doUpdateUser")
    public String doUpdateUser(User nowUser){
        for (int i = 0; i < DataUtil.userList.size(); i++) {
            if(nowUser.getUserId() == DataUtil.userList.get(i).getUserId()){
                DataUtil.userList.get(i).setUserName(nowUser.getUserName());
                DataUtil.userList.get(i).setPassword(nowUser.getPassword());
            }
        }
        return "redirect:/user/toList";
    }

    //增加1
    @RequestMapping("/addUser")
    public ModelAndView addUser(){
        User newUser = new User();
        for (int i = 0; i < DataUtil.userList.size(); i++) {
            newUser.setUserId(DataUtil.userList.get(i).getUserId()+1);
        }
        ModelAndView mav = new ModelAndView();
        mav.setViewName("addUser");
        mav.addObject("newUser", newUser);
        return mav;
    }

    //增加2
    @RequestMapping("/doAddUser")
    public String save(User newUser){
        DataUtil.userList.add(newUser);
        return "redirect:/user/toList";
    }

}

User.java

package com.pro.domain;

/**
 * @author Yuhua
 * @since 21.7.13 14:09
 */
public class User {
    private static int idMax=10;
    private int userId;
    private String userName;
    private String password;

    public User() {
    }

    public User(int userId, String userName, String password) {
        this.userId = userId;
        this.userName = userName;
        this.password = password;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

DataUtil.java

package com.pro.util;

import com.pro.domain.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Yuhua
 * @since 21.7.13 14:23
 */
public class DataUtil {
    //加上static就不是对象了,可以直接用类来调用。
    public static List<User> userList = new ArrayList<User>();
    //单纯static说明,在服务器刚跑起来时候就加载,加载出来初始化的10个对象写入userList里。
    static {
        for (int i = 0; i < 10; i++) {
            userList.add(new User(i,"name"+i,"pwd"+i));
        }
    }
}

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Yuhua
  Date: 21.7.13
  Time: 10:10
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<a href="">用户管理</a>

</body>
</html>

users.jsp

<%--
  Created by IntelliJ IDEA.
  User: Yuhua
  Date: 21.7.13
  Time: 14:36
--%>
<%@ 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>
<a href="${pageContext.request.contextPath}/user/addUser">添加用户</a>
<table align="center" width="800" border="1">
    <tr>
        <td colspan="4" align="center">用户列表</td>
    </tr>
    <tr>
        <%-- ctrl+d 复制一层 --%>
        <td>userId</td>
        <td>userName</td>
        <td>password</td>
        <td>action</td>
    </tr>
    <%--
        四种作用域对象可以用el表达获取
        pageContext
        request
        session
        application
     --%>
    <%--${requestScope.a}--%>
    <c:forEach items="${userList}" var="user">
        <tr>
            <td>${user.userId}</td>
            <td>${user.userName}</td>
            <td>${user.password}</td>
            <td>
                <a href="${pageContext.request.contextPath}/user/deleteUser?userId=${user.userId}" οnclick="return confirm('真要删除吗?')">删除</a>
                <a href="${pageContext.request.contextPath}/user/updateUser?userId=${user.userId}">修改</a>
            </td>
        </tr>

    </c:forEach>
</table>
</body>
</html>

updateUser.jsp

<%--
  Created by IntelliJ IDEA.
  User: Yuhua
  Date: 21.7.13
  Time: 14:36
--%>
<%@ 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>
<form action="${pageContext.request.contextPath}/user/doUpdateUser" method="post">
<table align="center" width="400" border="1">
    <tr>
        <td colspan="2" align="center">修改用户</td>
    </tr>
    <tr>
        <td>userId</td>
        <td>
            <input type="text" name="userId" value="${nowUser.userId}" readonly>
        </td>
    </tr>
    <tr>
        <td>userName</td>
        <td>
            <input type="text" name="userName" value="${nowUser.userName}">
        </td>
    </tr>
    <tr>
        <td>password</td>
        <td>
            <input type="text" name="password" value="${nowUser.password}">
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <input type="submit" value="提交">
        </td>
    </tr>
</table>
</form>
</body>
</html>
addUser.jsp
<%--
  Created by IntelliJ IDEA.
  User: Yuhua
  Date: 21.7.13
  Time: 14:36
--%>
<%@ 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>
<form action="${pageContext.request.contextPath}/user/doAddUser" method="post">
<table align="center" width="400" border="1">
    <tr>
        <td colspan="2" align="center">增加用户</td>
    </tr>
    <tr>
        <td>userId</td>
        <td>
            <input type="text" name="userId" value="${newUser.userId}" readonly>
        </td>
    </tr>
    <tr>
        <td>userName</td>
        <td>
            <input type="text" name="userName">
        </td>
    </tr>
    <tr>
        <td>password</td>
        <td>
            <input type="text" name="password">
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <input type="submit" value="提交">
        </td>
    </tr>
</table>
</form>
</body>
</html>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值