zoukankan      html  css  js  c++  java
  • JavaWeb之博客系统(四)

    1.进一步整合,将所有对博文类别的操作,例如添加,修改,删除等等都放在一个Servlet中,这个时候为了让Servlet知道到底是进行哪个操作,可以在调用的时候参入一个参数method=?.例如,method=add的话就是添加博文类别,调用添加博文类别的方法

    这里,又可以有两种方式,第一种方式是自己将method的值添加到url地址的后面,例如:

    <a href="http://localhost:8080/blog/servlet/AdminCategoryServlet?method=delete&cid=<%=category.getId()%>" onclick="return confirm('Do you want to delete this category?')">Delete</a>
    

    第二种方式是将method放在一个隐藏的input中,作为一个表单项传过去然后获取,例如:

    <form id="form1" name="form1" method="get" action="/blog/servlet/AdminCategoryServlet">
        <input name="method" id="method" type="hidden" value="postModify"/>
        <input name="cid" id="cid" type="hidden" value="<%=category.getId() %>" />            
    </form>

    然后后台的获取和处理方法:

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		String method = request.getParameter("method");
    		if (method == null) {
    			method = "list";
    		}
    		if (method.equalsIgnoreCase("add")) {
    			addCategory(request, response);
    		} else if (method.equalsIgnoreCase("delete")) {
    			deleteCategory(request, response);
    		} else if (method.equalsIgnoreCase("preModify")) {
    			preModifyCategory(request, response);
    		} else if (method.equalsIgnoreCase("postModify")) {
    			postModifyCategory(request, response);
    		} else if (method.equalsIgnoreCase("list")) {
    			listCategory(request, response);
    		}
    
    	}


    2.修改一下添加博文中的类别列表选项,由于列表是动态的,根据数据库取出来的,所以这里就涉及到了数据库操作,但是这样就不符合MVC的设计模式了,所以,最好是在添加博文之前首先获取出类别列表,所以新建一个servlet-PreAddBlogServlet!

    最终前台如下:

    <td>

    <label for="category"></label>
        <select name="category" id="category">
            <%
                List list = (List) request.getAttribute("categorylist");
                Category c = null;
                for (int i = 0, size = list.size(); i < size; i++) {
                    c = (Category) list.get(i);
            %>
            <option value="<%=c.getId()%>">
            <%=c.getName()%>
            </option>
           <%
               }
           %>
        </select>
    </td>
     
     
  • 相关阅读:
    day28 粘包, 合法性连接
    day27 网络通信协议, tcp和udp, 缓冲区, subprocess
    day 26 C/S架构, 网络通信流程, 初识socket
    day25 包语法
    Python9-From-CSS-day48
    Python9-前端基础知识-day47
    Python9-MySQL-MySQL-ORM框架-day48
    Python9-MySQL-MySQL存储过程-视图-触发器-函数-day45
    Python9-MySQL-pymysql模块-day44
    Python9-MySQL-Homework-day43
  • 原文地址:https://www.cnblogs.com/yinger/p/2083485.html
Copyright © 2011-2022 走看看