標籤:
在type-index.jsp中展示了所有類型,同時也可以添加類型,如下JSP代碼:
1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4 <%@ include file="top.jsp"%> 5 <title>商品展示平台</title> 6 </head> 7 <body> 8 <div> 9 <div><a href="${root}/send?url=type-save.jsp">添加商品類型</a></div>10 <c:forEach items="${entities}" var="obj">11 <div> 12 <a href="${root}/goods?action=tid&tid=${obj.id}">${obj.name}</a>13 </div>14 </c:forEach>15 </div>16 </body>17 </html>
可以看到添加商品類型的串連是${root}/send?url=type-save.jsp。其中${root}是在top.jsp設定的項目的ContextPath。send是一個轉寄serlvet,它的功能就是轉寄到url參數指定的URL地址中,之所有這樣做,是因為所有的JSP頁面都存放在WEB-INF是無法通過URL直接存取的,所以我寫了一個Serlvet來轉寄它們。
SendAction.java
1 package action; 2 3 import java.io.IOException; 4 5 import javax.servlet.RequestDispatcher; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 11 /*12 * 轉寄Action13 */14 public class SendAction extends HttpServlet {15 private static final long serialVersionUID = 1L;16 17 protected void doGet(HttpServletRequest request, HttpServletResponse response)18 throws ServletException, IOException {19 String url = request.getParameter("url");20 if (null == url || "".equals(url)) {21 url = "WEB-INF/jsp/404.jsp";22 } else {23 url = "WEB-INF/jsp/" + url;24 }25 RequestDispatcher rd = request.getRequestDispatcher(url);26 rd.forward(request, response);27 }28 29 protected void doPost(HttpServletRequest request, HttpServletResponse response)30 throws ServletException, IOException {31 doPost(request, response);32 }33 }
在web.xml配置SendAction.
1 <servlet>2 <servlet-name>SendAction</servlet-name>3 <servlet-class>action.SendAction</servlet-class>4 </servlet>5 <servlet-mapping>6 <servlet-name>SendAction</servlet-name>7 <url-pattern>/send</url-pattern>8 </servlet-mapping>
這樣點擊添加商品類型就會轉寄到type-save.jsp中。
1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4 <%@ include file="top.jsp"%> 5 <title>添加商品類型</title> 6 </head> 7 <body> 8 <form action="${root}/type?action=save" method="post"> 9 <table>10 <caption>添加商品類型</caption>11 <tr>12 <td>類型名稱:</td>13 <td><input type="text" name="name"/></td>14 <td>${msg}</td>15 </tr>16 <tr>17 <td colspan="3"><input type="submit" value="添加"/></td>18 </tr>19 </table>20 </form>21 </body>
表單的action是type?action=save,也就是調用type這個Servlet中的save方法:
1 // 儲存實體 2 private String[] save(HttpServletRequest request, HttpServletResponse response) { 3 String name = request.getParameter("name"); 4 if (name == null || "".equals(name)) {// 非空校正 5 request.setAttribute("msg", "類型名稱不可為空"); 6 return new String[] { "d", "WEB-INF/jsp/type-save.jsp" }; 7 } 8 Type t = new Type(); 9 t.setName(name);10 typeService.save(t);11 12 String root = request.getContextPath();13 // 重新導向,用戶端跳轉14 return new String[] { "r", root + "/type" };15 }
首先進行非空檢驗,如果欄位為空白,將錯誤資訊存放在request中然後轉寄會type-save.jsp。在type-save.jsp中${msg}就可以顯示出錯誤提示了。
如果儲存成功要做重新導向,這樣防止重複提交的問題,(當然還有設定用戶端不緩衝才可以,不然後退再提交也會重複提交)。重新導向成功則跳轉到首頁顯示所有類型
基於JSP+Serlvet+JDBC的開發(4)-- 繼續TypeSerlvet的save功能