• JavaWeb、JSP


    目录

    一、JSP概念

    二、JSP快速入门

    三、JSP原理

    四、JSP脚本

    使用JSP脚本动态显示网页

    五、JSP的缺点

    六、EL表达式

    七、JSTL标签

     1、JSTL快速入门

     2、foreach标签

    八、MVC模式和三层架构

    九、案例:使用三层架构模式完成增删改查

    1、准备环境

    2、查询所有

    1、 Dao/Mapper层

    2、Service层

    3、Web层

    3、添加数据

    1、Dao/Mapper层

    2、Service层

    3、Servlet层

    4、修改数据

    设置数据回显

    1、Dao/Mapper层

    2、Service层

    3、Servlet层

    4、点击修改获取商品id的方法

    修改数据

    1、Dao/Mapper层

    2、Service层

    3、Servlet层

    5、删除数据

    1、Dao/Mapper层

    2、Service层

    3、Servlet层


    一、JSP概念

    二、JSP快速入门

    三、JSP原理

    四、JSP脚本

    1. <%
    2. /*底层直接写到_jspService方法之中 方法中能写什么就能写什么*/
    3. System.out.println("hello jsp");
    4. int i = 3;
    5. %>
    6. <%=
    7. /*作为out.print的参数,直接输出*/
    8. "hello"
    9. %>
    10. <%=i%>
    11. <%!
    12. /*底层写到_jspService方法之外,被类直接包含,类中能写什么就能写什么*/
    13. String name = "zhangsan";
    14. void show(){}
    15. %>

    使用JSP脚本动态显示网页

    1. <%@ page import="com.itheima.pojo.Brand" %>
    2. <%@ page import="java.util.List" %>
    3. <%@ page import="java.util.ArrayList" %>
    4. <%@ page import="java.util.Arrays" %>
    5. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    6. <%
    7. //准备数据
    8. //集合第二个<>内必须写类型否则报错
    9. List<Brand> brandLists = new ArrayList<Brand>();
    10. brandLists.add(new Brand(0,"三只松鼠","三只松鼠有限公司",100,"好吃不上火",1));
    11. brandLists.add(new Brand(1,"华为","华为有限公司",5,"中国好品牌",0));
    12. brandLists.add(new Brand(2,"小米","小米手机有限公司",10,"小米为发烧而生",1));
    13. %>
    14. html>
    15. <html lang="en">
    16. <head>
    17. <meta charset="UTF-8">
    18. <title>Titletitle>
    19. head>
    20. <body>
    21. <input type="button" value="新增"><br>
    22. <hr>
    23. <table border="1" cellspacing="0" width="800">
    24. <tr>
    25. <th>序号th>
    26. <th>品牌名称th>
    27. <th>企业名称th>
    28. <th>排序th>
    29. <th>品牌介绍th>
    30. <th>状态th>
    31. <th>操作th>
    32. tr>
    33. <%--使用循环将数据库中的数据展示--%>
    34. <%
    35. for (int i = 0; i < brandLists.size(); i++) {
    36. Brand brand = brandLists.get(i);
    37. %>
    38. <%--将java代码截断,中间写html--%>
    39. <tr align="center">
    40. <td><%=brand.getId()%>td>
    41. <td><%=brand.getBrandName()%>td>
    42. <td><%=brand.getCompanyName()%>td>
    43. <td><%=brand.getOrdered()%>td>
    44. <td><%=brand.getDescription()%>td>
    45. <%
    46. if (brand.getStatus() == 1){
    47. //启用
    48. %>
    49. <td><%="启用"%>td>
    50. <%
    51. }else {
    52. //禁用
    53. %>
    54. <td><%="禁用"%>td>
    55. <%
    56. }
    57. %>
    58. <td><a href="#">修改a> <a href="#">删除a>td>
    59. tr>
    60. <%
    61. }
    62. %>
    63. table>
    64. body>
    65. html>

    五、JSP的缺点

    六、EL表达式

    1. <% EL表达式一定要在这里声明isELIgnored="false" %>
    2. <%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
    3. <html>
    4. <head>
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <%--接收数据--%>
    9. ${brandLists}
    10. body>
    11. html>
    1. @WebServlet("/demo1")
    2. public class ServletDemo1 extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. //获取数据
    6. List brandLists = new ArrayList();
    7. brandLists.add(new Brand(0,"三只松鼠","三只松鼠有限公司",100,"好吃不上火",1));
    8. brandLists.add(new Brand(1,"华为","华为有限公司",5,"中国好品牌",0));
    9. brandLists.add(new Brand(2,"小米","小米手机有限公司",10,"小米为发烧而生",1));
    10. //将获取到的数据存入requst域中
    11. req.setAttribute("brandLists",brandLists);
    12. //转发到动态页面
    13. req.getRequestDispatcher("/el-demo.jsp").forward(req,resp);
    14. }
    15. @Override
    16. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    17. this.doGet(req, resp);
    18. }
    19. }

    七、JSTL标签

     1、JSTL快速入门

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. <html>
    4. <head>
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <%--c:if 判断语句--%>
    9. <c:if test="true">
    10. <h1>TRUEh1>
    11. c:if>
    12. body>
    13. html>

     2、foreach标签

    1. <%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. html>
    4. <html lang="en">
    5. <head>
    6. <meta charset="UTF-8">
    7. <title>Titletitle>
    8. head>
    9. <body>
    10. <input type="button" value="新增"><br>
    11. <hr>
    12. <table border="1" cellspacing="0" width="800">
    13. <tr>
    14. <th>序号th>
    15. <th>品牌名称th>
    16. <th>企业名称th>
    17. <th>排序th>
    18. <th>品牌介绍th>
    19. <th>状态th>
    20. <th>操作th>
    21. tr>
    22. <%--foreach遍历数据--%>
    23. <c:forEach items="${brands}" var="brand" varStatus="status">
    24. <tr align="center">
    25. <%--大括号内会对数据进行处理,变成getId,然后在brand中获取数据--%>
    26. <%--使用varStatus设置连续的序号代替id属性--%>
    27. <td>${status.count}td>
    28. <td>${brand.brandName}td>
    29. <td>${brand.companyName}td>
    30. <td>${brand.ordered}td>
    31. <td>${brand.description}td>
    32. <c:if test="${brand.status == 1}">
    33. <td>启用td>
    34. c:if>
    35. <c:if test="${brand.status != 1}">
    36. <td>禁用td>
    37. c:if>
    38. <td><a href="#">修改a> <a href="#">删除a>td>
    39. tr>
    40. c:forEach>
    41. table>
    42. body>
    43. html>

    八、MVC模式和三层架构

    九、案例:使用三层架构模式完成增删改查

    1、准备环境

    2、查询所有

    1、 Dao/Mapper层

    1. public interface BrandMapper {
    2. List<Brand> selectAll();
    3. }
    4. <mapper namespace="com.itheima.mapper.BrandMapper">
    5. <select id="selectAll" resultType="com.itheima.pojo.Brand">
    6. select * from tb_brand;
    7. select>
    8. mapper>

    2、Service层

    1. public class BrandService {
    2. //只需要创建一个会话工厂
    3. static SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    4. /**
    5. * 查询所有
    6. * @return
    7. */
    8. public static List selectAll(){
    9. SqlSession sqlSession = sqlSessionFactory.openSession();
    10. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    11. List brandList = mapper.selectAll();
    12. sqlSession.close();
    13. return brandList;
    14. }
    15. }

    3、Web层

    1. @WebServlet("/select")
    2. public class SelectAllServlet extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. //调用service查询
    6. List brandList = BrandService.selectAll();
    7. //将数据存入request域中
    8. req.setAttribute("brands",brandList);
    9. //转发到index.jsp
    10. req.getRequestDispatcher("/select.jsp").forward(req,resp);
    11. }
    12. @Override
    13. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    14. super.doPost(req, resp);
    15. }
    16. }

    3、添加数据

    1、Dao/Mapper层

    1. public interface BrandMapper {
    2. List<Brand> selectAll();
    3. void add(Brand brand);
    4. }
    5. <insert id="add">
    6. insert into tb_brand(brandName,companyName,ordered,description,status)
    7. values
    8. (#{brandName},#{companyName},#{ordered},#{description},#{status});
    9. insert>

    2、Service层

    1. public static void add(Brand brand){
    2. SqlSession sqlSession = sqlSessionFactory.openSession();
    3. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    4. mapper.add(brand);
    5. //增加操作要提交事务
    6. sqlSession.commit();
    7. sqlSession.close();
    8. }

    3、Servlet层

    1. @WebServlet("/add")
    2. public class AddServlet extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. //处理post请求乱码问题
    6. req.setCharacterEncoding("UTF-8");
    7. Brand brand = new Brand();
    8. brand.setBrandName(req.getParameter("brandName"));
    9. brand.setCompanyName(req.getParameter("companyName"));
    10. brand.setDescription(req.getParameter("description"));
    11. brand.setOrdered(Integer.parseInt(req.getParameter("ordered")));
    12. brand.setStatus(Integer.parseInt(req.getParameter("status")));
    13. //调用servise添加方法
    14. BrandService.add(brand);
    15. //转发到selectServlet
    16. req.getRequestDispatcher("/select").forward(req,resp);
    17. }
    18. @Override
    19. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    20. this.doGet(req, resp);
    21. }
    22. }

    4、修改数据

    设置数据回显

    点击修改后要通过商品id将数据回显给用户

    1、Dao/Mapper层

    1. Brand selectById(int id);
    2. <select id="selectById" resultType="com.itheima.pojo.Brand">
    3. select * from tb_brand where id=#{id};
    4. select>

    2、Service层

    1. public static Brand selectById(int id){
    2. SqlSession sqlSession = sqlSessionFactory.openSession();
    3. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    4. Brand brand = mapper.selectById(id);
    5. sqlSession.close();
    6. return brand;
    7. }

    3、Servlet层

    1. @WebServlet("/select")
    2. public class SelectAllServlet extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. //调用service查询
    6. List brandList = BrandService.selectAll();
    7. //将数据存入request域中
    8. req.setAttribute("brands",brandList);
    9. //转发到index.jsp
    10. req.getRequestDispatcher("/select.jsp").forward(req,resp);
    11. }
    12. @Override
    13. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    14. this.doGet(req, resp);
    15. }
    16. }

    4、点击修改获取商品id的方法

    使用get方式将id提交给Servlet,注意将数据回显后还要将对应实体类的id也要获取到页面上,使用input的hidden类型,将id隐藏,方便update的service方法获取商品对应的id,执行对应的SQL语句

     <td><a href="/jspcase/selectById?id=${brand.id}">修改a> <a href="#">删除a>td>

    修改数据

    1、Dao/Mapper层

    1. void update(Brand brand);
    2. <update id="update">
    3. update tb_brand
    4. set
    5. brandName=#{brandName},companyName=#{companyName},ordered=#{ordered},description=#{description},status=#{status}
    6. where
    7. id = #{id};
    8. update>

    2、Service层

    1. public static void update(Brand brand){
    2. SqlSession sqlSession = sqlSessionFactory.openSession();
    3. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    4. mapper.update(brand);
    5. //修改增加都要提交事务
    6. sqlSession.commit();
    7. sqlSession.close();
    8. }

    3、Servlet层

    1. @WebServlet("/update")
    2. public class UpdateServlet extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. //处理post请求乱码问题
    6. req.setCharacterEncoding("UTF-8");
    7. Brand brand = new Brand();
    8. //获取页面上的隐藏id值
    9. brand.setId(Integer.parseInt(req.getParameter("id")));
    10. brand.setBrandName(req.getParameter("brandName"));
    11. brand.setCompanyName(req.getParameter("companyName"));
    12. brand.setDescription(req.getParameter("description"));
    13. brand.setOrdered(Integer.parseInt(req.getParameter("ordered")));
    14. brand.setStatus(Integer.parseInt(req.getParameter("status")));
    15. //调用servise修改方法
    16. BrandService.update(brand);
    17. req.setAttribute("brand",brand);
    18. //转发到selectServlet
    19. req.getRequestDispatcher("/select").forward(req,resp);
    20. }
    21. @Override
    22. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    23. this.doGet(req,resp);
    24. }
    25. }

    5、删除数据

    1、Dao/Mapper层

    1. void delete(int id);
    2. <delete id="delete">
    3. delete from tb_brand where id=#{id};
    4. delete>

    2、Service层

    1. public static void delete(int id){
    2. SqlSession sqlSession = sqlSessionFactory.openSession();
    3. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    4. mapper.delete(id);
    5. //对数据库的增删改都要提交事务
    6. sqlSession.commit();
    7. sqlSession.close();
    8. }

    3、Servlet层

    1. @WebServlet("/delete")
    2. public class DeleteServlet extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. String id = req.getParameter("id");
    6. BrandService.delete(Integer.parseInt(id));
    7. req.getRequestDispatcher("/select").forward(req,resp);
    8. }
    9. @Override
    10. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    11. this.doGet(req, resp);
    12. }
    13. }

     

  • 相关阅读:
    【git】submodule
    【CMAKE极简教程】不断更新中......
    Nginx中实现自签名SSL证书生成与配置
    [ C++ ] 多态原理 多态(2)
    【C++漂流记】一文搞懂类与对象的封装
    【MM32F5270开发板试用】移植Google Chrome小恐龙游戏到MM32F5270
    Git Commit Message 规范实践
    uni-app 之 Toast 消息提示
    Qt SQL:QSqlField、QSqlRecord、QSqlIndex、QSqlError
    python开发面试-20240715
  • 原文地址:https://blog.csdn.net/m0_56044262/article/details/126378128