Java Web simple mall project (5)

Source: Internet
Author: User

Java Web simple mall project (5)

Next to the previous javaWEB simple mall project (4), this chapter begins to write JSP on the interface, using the MVC method, this article describes JSTL label learning, MVC learning, and building an MVC framework.

I. JSTL labels

The JSP standard tag Library (JSTL) is a collection of JSP tags, which encapsulates common core functions of JSP applications.

JSTL supports common and structured tasks, such as iteration, condition judgment, XML document operations, international tags, and SQL tags. In addition, it also provides a framework to use custom labels integrated with JSTL.

1. Install JSTL library in Apache Tomcat

Download the Binary Package (jakarta-taglibs-standard-current.zip) from the apachestandard tag library ). : Http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
Download the jakarta-taglibs-standard-1.1.1.zip package and decompress it. Copy the two jar files: standard. jar and jstl. jar files under jakarta-taglibs-standard-1.1.1/lib/to/WEB-INF/lib.
To use any library, you must include tags in the header of each JSP file.

2. Use the JSTL library to first import the tag library. The core tag library prefix is c, followed by the formatting tag library fmt and SQL tag library SQL
<%@ taglib prefix="c"            uri="http://java.sun.com/jsp/jstl/core" %>
Then you can use the JSTL tag like html tags, For more information about label usage, see w3cschool. cc.
II. Introduction to mvc 1. MVC of javaWEB

The full name of MVC is Model View Controller, which is short for model-view-controller. For javaWEBJSP only processes the interface, servlet only processes requests, and javaBean only processes data, As shown in.

2. Implement the MVC-based user list

JavaBean has been encapsulated in previous articles, so now you should write your request processing first.

The method here is similar to the method in the previous DAO layer. We all know that the service () method of Servlet determines whether the request will call post or get or other methods, if we want the request to call our own method, we will overwrite the service () method in the parent class.

Therefore, BaseServlet can be written in this way, but we need to define it in a unified way. If the user. do? Method = list: the list method is called to display the user list.
At the same time, it is defined that when the return value is "way" prefixed with "client:", it is a client jump. Otherwise, a server jump is required.

Import com. util. daoUtil; import javax. servlet. requestDispatcher; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import java. io. IOException; import java. lang. reflect. invocationTargetException; import java. lang. reflect. method;/*** Created by nl101 on 2016/2/29. */public class BaseServl Et extends HttpServlet {/*** rewrite the service method, and use the Reflection Method to Determine the function to be called by the subclass */@ Override protected void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// use dependency injection DaoUtil. daoInject (this); // The following Code determines that the subclass calls the corresponding function String method = req. getParameter ("method"); // get the name of the function to be called try {if (method! = Null &&! Method. equals ("") {Method m = this. getClass (). getMethod (method, HttpServletRequest. class, HttpServletResponse. class); String way = (String) m. invoke (this, req, resp); String skipPre = "client:"; // if (way. startsWith (skipPre) {// client-side jump to resp. sendRedirect ("/WEB-INF/" + way. substring (skipPre. length ();} else {// server jump to RequestDispatcher rd = req. getRequestDispatcher ("/WEB-INF/" + way); rd. forward (req, resp) ;}} catch (NoSuchMethodException e) {e. printStackTrace ();} catch (InvocationTargetException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();}}}
All calls are solved by the parent class, so our subclass will be very simple. The subclass is only responsible for retrieving data, storing it in the request, and then deciding to jump to the page
Import com. dao. userDao; import com. model. dao; import com. model. pager; import com. model. user; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse;/*** Created by nl101 on 2016/2/29. */public class UserServlet extends BaseServlet {private UserDao userDao;/*** use dependency injection to implement this injection. For details, see (4 ), this also reflects the convenience of dependency injection * @ param userDao */@ Dao public void setUserDao (UserDao userDao) {this. userDao = userDao;}/*** Display User list interface */public String list (HttpServletRequest req, HttpServletResponse resp) {Pager
  
   
Pagers = userDao. find (null, null, null); // query all user req. setAttribute ("pagers", pagers); // stores all users in the request. return "user/list. jsp "; // return the page to be displayed }}
  
In this case, the corresponding JSP page is simpler. Some JSTL labels are used in it. For details, refer
<%@ page contentType="text/html;charset=UTF-8" language="java" %><[email protected] prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
User List <% -- Simply use the variable name here. The get method is automatically called -- %>
ID USERNAME PASSWORD NICKNAME TYPE Operation
${u.id} ${u.username} ${u.password} ${u.nickname} Administrator User View modification and Deletion
The last step is to transform the paging framework. The JSTL label is not used before writing the paging framework. Now, you only need to make a slight modification based on the original one. for more information about the paging framework, see the previous article.
<Code class = "hljs xml"> <% @ page contentType = "text/html; charset = UTF-8 "language =" java "pageEncoding =" UTF-8 "%> <[email protected] prefix =" c "uri =" http://java.sun.com/jsp/jstl/core "%> <% -- Introduce paging frame -- %> <[email protected] prefix = "pg" uri = "http://jsptags.com/tags/navigation/pager" %> <% -- the parameters are in sequence the total number of projects per page display quantity below menu display quantity current page curPage -- %> <pg: pager items = "$ {param. items} "maxpageitems =" 10 "maxindexpages =" 10 "export =" curPage = pageNumber "url =" $ {param. url} "> <% -- parameters are accepted here. All parameters passed through jsp: param must use param. to reference -- %> <c: foreach items = "$ {param. params} "var =" p "> <pg: param name =" $ {p} "> </pg: param> </c: foreach> <pg: first> <a data-cke-saved-href = "hyperlink}" href = "hyperlink}"> homepage </a> </pg: first> <pg: prev> <a data-cke-saved-href = "hyperlink}" href = "hyperlink}"> previous page </a> </pg: prev> <pg: pages> <c: if test = "$ {curPage eq pageNumber}"> [$ {pageNumber}] </c: if> <c: if test = "$ {curPage ne pageNumber}"> <a data-cke-saved-href = "signature}" href = "signature}" >$ {pageNumber} </> </c: if> </pg: pages> <pg: next> <a data-cke-saved-href = "hyperlink}" href = "hyperlink}"> next page </a> </pg: next> <pg: last> <a data-cke-saved-href = "hyperlink}" href = "hyperlink}"> last page </a> </pg: last> </pg: pager> </code>

:

MVC Summary

Below is the registration page, login page, and other user-related pages, you only needAdd corresponding methods in UserServletFor example, if I want to add a user, I only need to add an add method, and then request method = add, so that I can directly jump to the add page, it is clearer and easier to write than the previous writing logic.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.