第一次项目

萌新刺客的学习小屋
本文总结:第一次写项目有点混乱因jsp太老采用了springmvc的模式
弊端:在 使用Spring MVC 做java Web 项目时,乱码问题时常都会出现, 解决方法也不尽相同,有简单也有复杂的;如果加入了Spring框架之后就容易了
刚开始的大杂烩(留给以后自己优化)
学习spring后将会对本项目进行优化

人生指引
虚心求学,共同进步
新人希望大家多多指导关照



文章目录

  • 第一次项目
  • mybatis框架注意事项
  • 宿舍管理的基本CRUD+用户登录注册(mybatis+servlet+shymeleaf)
  • 项目的流程
  • xml文件
  • pom.xml
  • mybatis-config.xml
  • DormitoryMapper.xml
  • mapper
  • DormitoryMapper
  • UserMapper
  • pojo
  • Dormitory
  • User
  • service
  • UserServiceimpl
  • DormitoryService
  • DormitoryServiceImpl
  • util
  • SqlSessionFactoryUtils
  • StringUtil
  • servlet
  • ViewBaseServlet
  • ModelBaseServlet
  • RegisterServlet
  • LoginServlet
  • ListServlet
  • AddServlet
  • DeleteServlet
  • UpdateServlet
  • EditServlet



mybatis框架注意事项

dao层也叫mapper层,持久层:操作数据库
任务:写接口和配置

service:调用dao层的操作将其写成方法

如果resultmap报错

Result Maps collection does not contain value for
注意:由其他mapper.xml中的resultMap参数导致的错误
(检查其他xml是否有resultmap未定义却被引用)

resultmap
1.id
2.result

<resultMap id="dormitoryResultMap" type="dormitory">
        <result column="student_name" property="studentName" />
        <result column="dormitory_name" property="dormitoryName"/>
    </resultMap>

宿舍管理的基本CRUD+用户登录注册(mybatis+servlet+shymeleaf)

项目的流程

java网上论坛代码 javaweb论坛_java

xml文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>brand-case</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <dependencies>
        <!--Servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>


        <!--MyBatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
        <!--MySQL-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>


        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.15.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>

            </plugin>
        </plugins>
    </build>
</project>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--起别名-->
    <typeAliases>
        <package name="com.ym.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--扫描mapper-->
        <package name="com.ym.mapper"/>
    </mappers>
</configuration>

DormitoryMapper.xml

mapper

DormitoryMapper

package com.ym.mapper;

import com.ym.pojo.Dormitory;
import org.apache.ibatis.annotations.Delete;

import org.apache.ibatis.annotations.*;

import java.util.List;

public interface DormitoryMapper {
    /**
     * 查找全部
     * @return
     */
    @Select("select * from tb_dormitory")
    @ResultMap("dormitoryResultMap")
    List<Dormitory> selectAll();

    @Select("select * from tb_dormitory where id = #{id}")
    @ResultMap("dormitoryResultMap")
    Dormitory getDormitoryById(Integer id);

    @Delete("delete from tb_dormitory where id = #{id}")
    @ResultMap("dormitoryResultMap")
    void delete(Integer id);

    @Insert("insert into tb_dormitory(student_name, dormitory_name,status) values (#{studentName},#{dormitoryName},#{status})")
    @ResultMap("dormitoryResultMap")
    void add(Dormitory dormitory);


    @Update("update tb_dormitory set student_name=#{studentName},dormitory_name=#{dormitoryName},status=#{status} where id=#{id}")
    @ResultMap("dormitoryResultMap")
    void update(Dormitory dormitory);
}

UserMapper

package com.ym.mapper;

import com.ym.pojo.Dormitory;
import com.ym.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface UserMapper {

    /**
     * 根据用户名和密码查询
     * @param username
     * @param password
     * @return
     */
    @Select("select * from tb_user where username = #{username} and password = #{password}")
    User select(@Param("username") String username, @Param("password")String password);

    /**
     * 根据用户名查询用户对象---注册
     * @param username
     * @return
     */
    @Select("select * from tb_user where username = #{username}")
    User selectByUsername(String username);

    /**
     * 添加用户
     * @param user
     */
    @Insert("insert into tb_user values(null,#{username},#{password})")
    void add(User user);



}

pojo

Dormitory

package com.ym.pojo;

/**
 * 宿舍实体类
 */

public class Dormitory {
    // id 主键
    private Integer id;
    // 学生姓名
    private String studentName;
    // 宿舍名称
    private String dormitoryName;
    // 状态:0:未到校  1:到校
    private Integer status;

    public Dormitory() {
    }


    public Dormitory(Integer id, String studentName, String dormitoryName, Integer status) {
        this.id = id;
        this.studentName = studentName;
        this.dormitoryName = dormitoryName;
        this.status = status;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getDormitoryName() {
        return dormitoryName;
    }

    public void setDormitoryName(String dormitoryName) {
        this.dormitoryName = dormitoryName;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", studentName='" + studentName + '\'' +
                ", dormitoryName='" + dormitoryName + '\'' +
                ", status=" + status +
                '}';
    }
}

User

package com.ym.pojo;

public class User {

    private Integer id;
    private String username;
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

service

UserServiceimpl

package com.ym.service;

import com.ym.mapper.UserMapper;
import com.ym.pojo.User;
import com.ym.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


public class UserService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 登录方法
     * @param username
     * @param password
     * @return
     */

    public User selectUser(String username, String password){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4. 调用方法
        User user = mapper.select(username, password);

        //释放资源
        sqlSession.close();

        return  user;
    }



    /**
     * 注册方法
     * @return
     */

    public boolean register(User user){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //4. 判断用户名是否存在
        User u = mapper.selectByUsername(user.getUsername());

        if(u == null){
            // 用户名不存在,注册
            mapper.add(user);
            sqlSession.commit();
            sqlSession.close();
            return true;
        }
        else {
            sqlSession.close();

            return false;
        }

    }
}

DormitoryService

package com.ym.service;

import com.ym.pojo.Dormitory;
import com.ym.pojo.Page;

import java.util.List;

public interface DormitoryService {

    /**
     * 查询所有
     * @return
     */
    List<Dormitory> selectAll();

    /**
     * 添加数据
     * @param dormitory
     */
    void add(Dormitory dormitory);

    /**
     * 批量删除
     * @param id
     */
    void delete(Integer id);

    /**
     * 编辑学生个人信息:将来可以分不同权限限制操作
     * @param id
     * @return
     */
    Dormitory getDormitoryById(Integer id);


    Dormitory update(Dormitory dormitory);
}

DormitoryServiceImpl

package com.ym.service.impl;

import com.ym.mapper.DormitoryMapper;
import com.ym.pojo.Dormitory;
import com.ym.pojo.Page;
import com.ym.service.DormitoryService;
import com.ym.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class DormitoryServiceImpl implements DormitoryService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Dormitory> selectAll() {
        SqlSession sqlSession = factory.openSession();
        DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);
        List<Dormitory> dormitories = mapper.selectAll();
        sqlSession.close();
        return dormitories;
    }

    @Override
    public void add(Dormitory dormitory) {
        SqlSession sqlSession = factory.openSession(true);
        DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);
        mapper.add(dormitory);
        sqlSession.close();
    }

    @Override
    public void delete(Integer id) {
        SqlSession sqlSession = factory.openSession(true);
        DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);
        mapper.delete(id);
        sqlSession.close();
    }


    @Override
    public Dormitory getDormitoryById(Integer id) {
        SqlSession sqlSession = factory.openSession(true);
        DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);
        Dormitory dormitory = mapper.getDormitoryById(id);
        sqlSession.close();
        return dormitory;
    }

    @Override
    public Dormitory update(Dormitory dormitory) {
        SqlSession sqlSession = factory.openSession(true);
        DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);
        mapper.update(dormitory);
        sqlSession.close();
        return dormitory;
    }

}

util

SqlSessionFactoryUtils

package com.ym.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionFactoryUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        //静态代码块会随着类的加载而自动执行,且只执行一次
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static SqlSessionFactory getSqlSessionFactory(){
        return sqlSessionFactory;
    }
}

StringUtil

package com.ym.util;

public class StringUtil {
    public static boolean isEmpty(String str){
        return str==null || "".equals(str);
    }
    public static boolean isNotEmpty(String str){
        return !isEmpty(str);
    }
}

servlet

ViewBaseServlet

package com.ym.web.servlet;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ViewBaseServlet extends HttpServlet {

    private TemplateEngine templateEngine;

    @Override
    public void init() throws ServletException {

        // 1.获取ServletContext对象
        ServletContext servletContext = this.getServletContext();

        // 2.创建Thymeleaf解析器对象
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext);

        // 3.给解析器对象设置参数
        // ①HTML是默认模式,明确设置是为了代码更容易理解
        templateResolver.setTemplateMode(TemplateMode.HTML);

        // ②设置前缀
        String viewPrefix = servletContext.getInitParameter("view-prefix");

        templateResolver.setPrefix(viewPrefix);

        // ③设置后缀
        String viewSuffix = servletContext.getInitParameter("view-suffix");

        templateResolver.setSuffix(viewSuffix);

        // ④设置缓存过期时间(毫秒)
        templateResolver.setCacheTTLMs(60000L);

        // ⑤设置是否缓存
        templateResolver.setCacheable(true);

        // ⑥设置服务器端编码方式
        templateResolver.setCharacterEncoding("utf-8");

        // 4.创建模板引擎对象
        templateEngine = new TemplateEngine();

        // 5.给模板引擎对象设置模板解析器
        templateEngine.setTemplateResolver(templateResolver);

    }

    protected void processTemplate(String templateName, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 1.设置响应体内容类型和字符集
        resp.setContentType("text/html;charset=UTF-8");

        // 2.创建WebContext对象
        WebContext webContext = new WebContext(req, resp, getServletContext());

        // 3.处理模板数据
        templateEngine.process(templateName, webContext, resp.getWriter());
    }
}

ModelBaseServlet

package com.ym.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.Method;

@WebServlet("/ModelBaseServlet")
public class ModelBaseServlet extends ViewBaseServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 在doGet()方法中调用doPost()方法,这样就可以在doPost()方法中集中处理所有请求
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 0.在所有request.getParameter()前面设置解析请求体的字符集
        request.setCharacterEncoding("UTF-8");

        // 1.从请求参数中获取method对应的数据
        String method = request.getParameter("method");

        // 2.通过反射调用method对应的方法
        // ①获取Class对象
        Class<? extends ModelBaseServlet> clazz = this.getClass();

        try {
            // ②获取method对应的Method对象
            Method methodObject = clazz.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);

            // ③打开访问权限
            methodObject.setAccessible(true);

            // ④通过Method对象调用目标方法
            methodObject.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();

            throw new RuntimeException(e);
        }
    }

}

RegisterServlet

package com.ym.web.servlet.no;

import com.ym.pojo.User;
import com.ym.service.UserService;
import com.ym.web.servlet.ViewBaseServlet;

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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/registerServlet")
public class RegisterServlet extends ViewBaseServlet {
    private UserService service = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //1. 获取用户名和密码数据

        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        boolean flag = service.register(user);
        //3. 判断注册成功与否
        if(flag){
            super.processTemplate("login",request,response);
        }
        else {

            super.processTemplate("register",request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

LoginServlet

package com.ym.web.servlet.no;

import com.ym.pojo.User;
import com.ym.service.UserService;
import com.ym.pojo.User;
import com.ym.service.UserService;
import com.ym.web.servlet.ViewBaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/loginServlet")
public class LoginServlet extends ViewBaseServlet {
    private UserService service = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取用户名和密码
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //获取复选框数据
//        String remember = request.getParameter("remember");

        //2. 调用service查询
        User user = service.selectUser(username, password);

        //3. 判断
        //获取字符输出流,并设置content type

        //3. 判断user释放为null
        if(user != null){
            // 登陆成功
            response.sendRedirect("listServlet");

        }else {
            // 登陆失败
            super.processTemplate("login",request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

ListServlet

package com.ym.web.servlet.no;

import com.ym.pojo.Dormitory;
import com.ym.service.DormitoryService;
import com.ym.service.impl.DormitoryServiceImpl;
import com.ym.web.servlet.ViewBaseServlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/listServlet")
public class ListServlet extends ViewBaseServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DormitoryService dormitoryService = new DormitoryServiceImpl();
        //调用service查询
        List<Dormitory> dormitories = dormitoryService.selectAll();

        HttpSession session = request.getSession();
        session.setAttribute("dormitories",dormitories);

        super.processTemplate("list",request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

AddServlet

package com.ym.web.servlet.no;

import com.ym.pojo.Dormitory;
import com.ym.service.DormitoryService;
import com.ym.service.impl.DormitoryServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
     private DormitoryService dormitoryService = new DormitoryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String studentName = request.getParameter("studentName");
        String dormitoryName = request.getParameter("dormitoryName");
        int status = Integer.parseInt(request.getParameter("status"));
        Dormitory dormitory = new Dormitory(null,studentName,dormitoryName,status);
        dormitoryService.add(dormitory);
        response.sendRedirect("listServlet");
        }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

DeleteServlet

package com.ym.web.servlet.no;

import com.ym.service.DormitoryService;
import com.ym.service.impl.DormitoryServiceImpl;
import com.ym.util.StringUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/deleteServlet")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DormitoryService dormitoryService = new DormitoryServiceImpl();
        String Instr = request.getParameter("id");
        if(StringUtil.isNotEmpty(Instr)){
            int id = Integer.parseInt(Instr);
            dormitoryService.delete(id);
            response.sendRedirect("listServlet");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

UpdateServlet

package com.ym.web.servlet.no;

import com.ym.pojo.Dormitory;
import com.ym.service.impl.DormitoryServiceImpl;
import com.ym.web.servlet.ViewBaseServlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/updateServlet")
public class UpdateServlet extends ViewBaseServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");


        DormitoryServiceImpl dormitoryService = new DormitoryServiceImpl();


        String idStr = request.getParameter("id");
        String studentName = request.getParameter("studentName");
        String dormitoryName = request.getParameter("dormitoryName");
        String statusStr = request.getParameter("status");
        int id = Integer.parseInt(idStr);
        int status = Integer.parseInt(statusStr);
        Dormitory dormitory = new Dormitory(id, studentName, dormitoryName, status);

        dormitoryService.update(dormitory);


//        super.processTemplate("list",request,response);
        //相当于list.html

        //修改后需要重定向
        response.sendRedirect("listServlet");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

EditServlet

package com.ym.web.servlet.no;

import com.ym.pojo.Dormitory;
import com.ym.pojo.Page;
import com.ym.service.DormitoryService;
import com.ym.service.impl.DormitoryServiceImpl;
import com.ym.util.StringUtil;
import com.ym.web.servlet.ViewBaseServlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/editServlet")
public class EditServlet extends ViewBaseServlet {
    private DormitoryService dormitoryService=new DormitoryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idStr = request.getParameter("id");
        if (StringUtil.isNotEmpty(idStr)){
            int id = Integer.parseInt(idStr);
            Dormitory dormitory = dormitoryService.getDormitoryById(id);
            request.setAttribute("dormitory",dormitory);
            super.processTemplate("edit",request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}