目录




- <%
- /*底层直接写到_jspService方法之中 方法中能写什么就能写什么*/
- System.out.println("hello jsp");
- int i = 3;
- %>
-
- <%=
- /*作为out.print的参数,直接输出*/
- "hello"
- %>
- <%=i%>
-
- <%!
- /*底层写到_jspService方法之外,被类直接包含,类中能写什么就能写什么*/
- String name = "zhangsan";
- void show(){}
- %>
- <%@ page import="com.itheima.pojo.Brand" %>
- <%@ page import="java.util.List" %>
- <%@ page import="java.util.ArrayList" %>
- <%@ page import="java.util.Arrays" %>
-
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%
- //准备数据
- //集合第二个<>内必须写类型否则报错
- List<Brand> brandLists = new ArrayList<Brand>();
- brandLists.add(new Brand(0,"三只松鼠","三只松鼠有限公司",100,"好吃不上火",1));
- brandLists.add(new Brand(1,"华为","华为有限公司",5,"中国好品牌",0));
- brandLists.add(new Brand(2,"小米","小米手机有限公司",10,"小米为发烧而生",1));
-
- %>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <input type="button" value="新增"><br>
- <hr>
- <table border="1" cellspacing="0" width="800">
- <tr>
- <th>序号th>
- <th>品牌名称th>
- <th>企业名称th>
- <th>排序th>
- <th>品牌介绍th>
- <th>状态th>
- <th>操作th>
- tr>
- <%--使用循环将数据库中的数据展示--%>
- <%
- for (int i = 0; i < brandLists.size(); i++) {
- Brand brand = brandLists.get(i);
- %>
- <%--将java代码截断,中间写html--%>
- <tr align="center">
- <td><%=brand.getId()%>td>
- <td><%=brand.getBrandName()%>td>
- <td><%=brand.getCompanyName()%>td>
- <td><%=brand.getOrdered()%>td>
- <td><%=brand.getDescription()%>td>
-
- <%
- if (brand.getStatus() == 1){
- //启用
- %>
- <td><%="启用"%>td>
- <%
- }else {
- //禁用
- %>
- <td><%="禁用"%>td>
- <%
- }
- %>
- <td><a href="#">修改a> <a href="#">删除a>td>
- tr>
- <%
- }
- %>
-
- table>
- body>
- html>


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


- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
-
- <%--c:if 判断语句--%>
- <c:if test="true">
- <h1>TRUEh1>
- c:if>
-
- body>
- html>

- <%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <input type="button" value="新增"><br>
- <hr>
- <table border="1" cellspacing="0" width="800">
- <tr>
- <th>序号th>
- <th>品牌名称th>
- <th>企业名称th>
- <th>排序th>
- <th>品牌介绍th>
- <th>状态th>
- <th>操作th>
- tr>
-
- <%--foreach遍历数据--%>
- <c:forEach items="${brands}" var="brand" varStatus="status">
- <tr align="center">
- <%--大括号内会对数据进行处理,变成getId,然后在brand中获取数据--%>
- <%--使用varStatus设置连续的序号代替id属性--%>
- <td>${status.count}td>
- <td>${brand.brandName}td>
- <td>${brand.companyName}td>
- <td>${brand.ordered}td>
- <td>${brand.description}td>
- <c:if test="${brand.status == 1}">
- <td>启用td>
- c:if>
- <c:if test="${brand.status != 1}">
- <td>禁用td>
- c:if>
-
- <td><a href="#">修改a> <a href="#">删除a>td>
- tr>
- c:forEach>
-
- table>
- body>
- html>





- public interface BrandMapper {
-
- List<Brand> selectAll();
-
- }
-
-
- <mapper namespace="com.itheima.mapper.BrandMapper">
-
-
- <select id="selectAll" resultType="com.itheima.pojo.Brand">
- select * from tb_brand;
- select>
- mapper>
- public class BrandService {
- //只需要创建一个会话工厂
- static SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
-
- /**
- * 查询所有
- * @return
- */
- public static List
selectAll(){ -
- SqlSession sqlSession = sqlSessionFactory.openSession();
-
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
-
- List
brandList = mapper.selectAll(); -
- sqlSession.close();
-
- return brandList;
- }
- }
- @WebServlet("/select")
- public class SelectAllServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //调用service查询
- List
brandList = BrandService.selectAll(); -
- //将数据存入request域中
- req.setAttribute("brands",brandList);
-
- //转发到index.jsp
- req.getRequestDispatcher("/select.jsp").forward(req,resp);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- super.doPost(req, resp);
- }
- }

- public interface BrandMapper {
-
- List<Brand> selectAll();
-
- void add(Brand brand);
- }
-
- <insert id="add">
- insert into tb_brand(brandName,companyName,ordered,description,status)
- values
- (#{brandName},#{companyName},#{ordered},#{description},#{status});
- insert>
- public static void add(Brand brand){
-
- SqlSession sqlSession = sqlSessionFactory.openSession();
-
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
-
- mapper.add(brand);
-
- //增加操作要提交事务
- sqlSession.commit();
-
- sqlSession.close();
- }
- @WebServlet("/add")
- public class AddServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //处理post请求乱码问题
- req.setCharacterEncoding("UTF-8");
-
- Brand brand = new Brand();
- brand.setBrandName(req.getParameter("brandName"));
- brand.setCompanyName(req.getParameter("companyName"));
- brand.setDescription(req.getParameter("description"));
- brand.setOrdered(Integer.parseInt(req.getParameter("ordered")));
- brand.setStatus(Integer.parseInt(req.getParameter("status")));
-
- //调用servise添加方法
- BrandService.add(brand);
-
- //转发到selectServlet
- req.getRequestDispatcher("/select").forward(req,resp);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- this.doGet(req, resp);
- }
- }
点击修改后要通过商品id将数据回显给用户

- Brand selectById(int id);
-
-
- <select id="selectById" resultType="com.itheima.pojo.Brand">
- select * from tb_brand where id=#{id};
- select>
- public static Brand selectById(int id){
- SqlSession sqlSession = sqlSessionFactory.openSession();
-
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
-
- Brand brand = mapper.selectById(id);
-
- sqlSession.close();
-
- return brand;
- }
- @WebServlet("/select")
- public class SelectAllServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //调用service查询
- List
brandList = BrandService.selectAll(); -
- //将数据存入request域中
- req.setAttribute("brands",brandList);
-
- //转发到index.jsp
- req.getRequestDispatcher("/select.jsp").forward(req,resp);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- this.doGet(req, resp);
- }
- }
使用get方式将id提交给Servlet,注意将数据回显后还要将对应实体类的id也要获取到页面上,使用input的hidden类型,将id隐藏,方便update的service方法获取商品对应的id,执行对应的SQL语句
<td><a href="/jspcase/selectById?id=${brand.id}">修改a> <a href="#">删除a>td>
- void update(Brand brand);
-
- <update id="update">
- update tb_brand
- set
- brandName=#{brandName},companyName=#{companyName},ordered=#{ordered},description=#{description},status=#{status}
- where
- id = #{id};
- update>
- public static void update(Brand brand){
- SqlSession sqlSession = sqlSessionFactory.openSession();
-
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
-
- mapper.update(brand);
-
- //修改增加都要提交事务
- sqlSession.commit();
-
- sqlSession.close();
- }
- @WebServlet("/update")
- public class UpdateServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //处理post请求乱码问题
- req.setCharacterEncoding("UTF-8");
-
-
- Brand brand = new Brand();
- //获取页面上的隐藏id值
- brand.setId(Integer.parseInt(req.getParameter("id")));
- brand.setBrandName(req.getParameter("brandName"));
- brand.setCompanyName(req.getParameter("companyName"));
- brand.setDescription(req.getParameter("description"));
- brand.setOrdered(Integer.parseInt(req.getParameter("ordered")));
- brand.setStatus(Integer.parseInt(req.getParameter("status")));
-
- //调用servise修改方法
- BrandService.update(brand);
-
- req.setAttribute("brand",brand);
-
- //转发到selectServlet
- req.getRequestDispatcher("/select").forward(req,resp);
-
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- this.doGet(req,resp);
- }
- }
- void delete(int id);
-
- <delete id="delete">
- delete from tb_brand where id=#{id};
- delete>
- public static void delete(int id){
- SqlSession sqlSession = sqlSessionFactory.openSession();
-
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
-
- mapper.delete(id);
-
- //对数据库的增删改都要提交事务
- sqlSession.commit();
-
- sqlSession.close();
- }
- @WebServlet("/delete")
- public class DeleteServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- String id = req.getParameter("id");
-
- BrandService.delete(Integer.parseInt(id));
-
- req.getRequestDispatcher("/select").forward(req,resp);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- this.doGet(req, resp);
- }
- }
