在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 * 转发Action 13 */ 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}就可以显示出错误提示了。
如果保存成功要做重定向,这样防止重复提交的问题,(当然还有设置客户端不缓存才可以,不然后退再提交也会重复提交)。重定向成功则跳转到首页显示所有类型
时间: 2024-10-08 02:05:36