#|项目登录页面框架


项目登录页面框架

    • 运行结果如下:
    • 整体代码布局:
    • 代码如下:
      • pom.xml:
      • spring-mvc.xml:(全局时间转换器)
      • web.xml:
      • index.jsp:
      • User:
      • Emp:
      • CommonResult:
      • LoginController :
      • EmpController :
      • ForwardContoller :
      • login.jsp:
      • emp.jsp:

运行结果如下: #|项目登录页面框架
文章图片

#|项目登录页面框架
文章图片

#|项目登录页面框架
文章图片

#|项目登录页面框架
文章图片

#|项目登录页面框架
文章图片

#|项目登录页面框架
文章图片

#|项目登录页面框架
文章图片

#|项目登录页面框架
文章图片

整体代码布局: #|项目登录页面框架
文章图片

代码如下: pom.xml:
4.0.0org.example lesson0826_web 1.0-SNAPSHOT【#|项目登录页面框架】warlesson0826_web Maven Webapphttp://www.example.com8 8 4.0.1 2.2 1.2 5.3.14 1.4 3.4.6 1.3.3 8.0.11 1.2.78 javax.servlet javax.servlet-api ${servlet.version}provided javax.servlet.jsp jsp-api ${jsp.version}provided jstl jstl ${jstl.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-jdbc ${spring.version} commons-dbcp commons-dbcp ${commons-dbcp.version} org.mybatis mybatis ${mybatis.version} org.mybatis mybatis-spring ${mybatis-spring.version} com.github.pagehelper pagehelper 5.1.0 com.github.abel533 mapper 3.0.1 mysql mysql-connector-java ${mysql-connector-java.version} com.alibaba fastjson ${fastjson.version} org.projectlombok lombok 1.18.24 com.fasterxml.jackson.core jackson-databind 2.13.2.2

spring-mvc.xml:(全局时间转换器)

web.xml:
encodingFilter org.springframework.web.filter.CharacterEncodingFilter encodingUTF-8 encodingFilter /* DispatcherServlet org.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring-mvc.xml DispatcherServlet /

index.jsp:
Hello World! 跳转到登陆

User:
package com.aaa.demo.entity; import lombok.Data; @Data public class User { private Integer userId; private String userName; private String password; }

Emp:
package com.aaa.demo.entity; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.DatabindException; import lombok.Data; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; @Data public class Emp { private Integer empId; private String empName; private String sex; private Double tall; //设置客户端请求时传入的日期格式 @DateTimeFormat(pattern = "yyyy-MM-dd") //让返回的日期数据按指定日期格式呈现 //@JsonFormat(pattern = "yyyy-MM-dd") private Date birthday; }

CommonResult:
package com.aaa.demo.entity; import lombok.Data; /** * @author: hy * @create: 2022-08-26 09:18:10 */ @Data public class CommonResult { //成功或失败编码:0成功 其他失败 private Integer code; //提示信息 private String msg; //总记录数 private Integer count; //返回的数据 private T data; public CommonResult(){}public CommonResult(Integer code,String msg,Integer count,T data){ this.code=code; this.msg=msg; this.count=count; this.data=https://www.it610.com/article/data; }//增删改成功或者其他操作:参数的data是受影响行数 public static CommonResult success(T data){ return new CommonResult(0,"success",0,data); }//查询记录成功 public static CommonResult success(Integer count,T data){ return new CommonResult<>(0,"success",count,data); }//失败 public static CommonResult fail(T data){ return new CommonResult<>(1,"fail",0,data); } }

LoginController :
package com.aaa.demo.controller; import com.aaa.demo.entity.CommonResult; import com.aaa.demo.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpSession; /** * 登陆控制器 */ @RestController @RequestMapping("/user") public class LoginController {//定义session对象 @Autowired private HttpSession session; @RequestMapping("/login") public CommonResult login(String userName,String password){ if("admin".equals(userName) && "123456".equals(password)){ //模拟从数据库查询回来一个用户对象 User user = new User(); user.setUserId(1001); user.setUserName(userName); user.setPassword(password); //将数据存储到session中 session.setAttribute("user",user); return CommonResult.success(user); } return CommonResult.fail("用户名或密码不正确"); }@RequestMapping("/logout") public CommonResult logout(){ //销毁session session.invalidate(); return CommonResult.success("注销成功"); } }

EmpController :
package com.aaa.demo.controller; import com.aaa.demo.entity.CommonResult; import com.aaa.demo.entity.Emp; import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @RestController //只做数据处理,不做视图解析 @RequestMapping("/emp") public class EmpController {/** * 查询所有员工 * @return */ @RequestMapping("/list") public CommonResult list(){ System.out.println("=========查询所有员工=========="); //模拟从mybatis获取到数据 List empList =getEmpList(); return CommonResult.success(100,empList); }/** * 根据编号查询员工 * @param empId * @return */ @RequestMapping("/getById/{empId}") public CommonResult getById(@PathVariable("empId") Integer empId){System.out.println("=========根据编号查询员工:=========="+empId); //模拟根据编号返回一个数据,并没有实际去查询 Emp emp = new Emp(); emp.setEmpId(1001); emp.setEmpName("张三"); emp.setSex("男"); emp.setBirthday(java.sql.Date.valueOf("2000-01-01")); emp.setTall(1.7); return CommonResult.success(emp); }/** * 添加 * @param emp * @return */ @RequestMapping("/add") public CommonResult add(Emp emp){ System.out.println("============添加员工============="); System.out.println(emp); //返回插入成功的提示 return CommonResult.success(1); }/** * 修改 * @param emp * @return */ @RequestMapping("/update") public CommonResult update(Emp emp){ System.out.println("============修改员工============="); System.out.println(emp); return CommonResult.success(1); }/** * 删除 * @param empId * @return */ @RequestMapping("/delete/{empId}") public CommonResult delete(@PathVariable("empId") Integer empId){ System.out.println("============删除员工=============:"+empId); return CommonResult.success(1); }//模拟从mybatis取回数据 public List getEmpList(){ List empList = new ArrayList<>(); Emp emp = new Emp(); emp.setEmpId(1001); emp.setEmpName("张三"); emp.setSex("男"); emp.setBirthday(java.sql.Date.valueOf("2000-01-01")); emp.setTall(1.7); Emp emp2 = new Emp(); emp2.setEmpId(1002); emp2.setEmpName("李四"); emp2.setSex("女"); emp2.setBirthday(java.sql.Date.valueOf("2002-01-01")); emp2.setTall(1.6); empList.add(emp); empList.add(emp2); return empList; }}

ForwardContoller :
package com.aaa.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 统一进行页面跳转 */ @Controller//注意此处没有用@RestController,因为返回的结果要进行视图解析 @RequestMapping("/forward") public class ForwardContoller { @RequestMapping("/toLogin") public String toLogin(){ return "login"; }//跳转到员工管理页面 @RequestMapping("/toEmp") public String toEmp(){ return "emp"; } }

login.jsp:
Title - 锐客网
用户名
密码

emp.jsp:
Title - 锐客网


// A code block var foo = 'bar';


// A code block var foo = 'bar';


// A code block var foo = 'bar';

    推荐阅读