All types are shown in type-index.jsp, and you can add types, such as the following JSP code:
1 <HTML>2 <Head>3 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">4 <%@ include file="top.jsp"%>5 <title>Commodity Display Platform</title>6 </Head>7 <Body>8 <Div>9 <Div><ahref= "${root}/send?url=type-save.jsp">Add a Product Type</a></Div>Ten <C:foreachItems= "${entities}"var= "obj"> One <Div> A <ahref= "${root}/goods?action=tid&tid=${obj.id}">${obj.name}</a> - </Div> - </C:foreach> the </Div> - </Body> - </HTML>
You can see that the connection to the added item type is ${root}/send?url=type-save.jsp. Where ${root} is the contextpath of the project set in top.jsp. Send is a forwarding Serlvet, its function is to forward to the URL parameter specified in the URL address, all this is because all the JSP pages are stored in the Web-inf is not directly accessible through the URL, so I wrote a serlvet to forward them.
Sendaction.java
1 PackageAction;2 3 Importjava.io.IOException;4 5 ImportJavax.servlet.RequestDispatcher;6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten One /* A * Forward Action - */ - Public classSendActionextendsHttpServlet { the Private Static Final LongSerialversionuid = 1L; - - protected voiddoget (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { +String url = request.getparameter ("url"); - if(NULL= = URL | | "". Equals (URL)) { +url = "Web-inf/jsp/404.jsp"; A}Else { aturl = "web-inf/jsp/" +URL; - } -RequestDispatcher rd =request.getrequestdispatcher (URL); - Rd.forward (request, response); - } - in protected voidDoPost (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { to DoPost (request, response); + } -}
Configure sendaction on the Web. Xml.
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>
Clicking on the Add item type will then be forwarded to type-save.jsp.
1 <HTML>2 <Head>3 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">4 <%@ include file="top.jsp"%>5 <title>Add a Product Type</title>6 </Head>7 <Body>8 <formAction= "${root}/type?action=save"Method= "POST">9 <Table>Ten <caption>Add a Product Type</caption> One <TR> A <TD>Type name:</TD> - <TD><inputtype= "text"name= "Name"/></TD> - <TD>${MSG}</TD> the </TR> - <TR> - <TDcolspan= "3"><inputtype= "Submit"value= "Add"/></TD> - </TR> + </Table> - </form> + </Body>
The action of the form is Type?action=save, which is called the Save method in the servlet of type:
1 //Save entity2 Privatestring[] Save (httpservletrequest request, httpservletresponse response) {3String name = Request.getparameter ("name");4 if(Name = =NULL|| "". Equals (name)) {//non-null check5Request.setattribute ("msg", "type name cannot be empty");6 return NewString[] {"D", "web-inf/jsp/type-save.jsp" };7 }8Type T =NewType ();9 t.setname (name);Ten Typeservice.save (t); One AString root =Request.getcontextpath (); - //REDIRECT , client jump - return NewString[] {"R", Root + "/type" }; the}
A non-null test is performed first, and if the field is empty, the error message is stored in the request and then forwarded type-save.jsp. ${MSG} in type-save.jsp can display the error prompt.
If the save succeeds to redirect, this prevents the problem of repeating the commit, (and of course the setting client does not cache it, otherwise the back-up commits will also be repeated). Redirect succeeds Jump to first page show all types
JSP+SERLVET+JDBC-based development (4)--Continue Typeserlvet's Save function