JSP Model模式,jspmodel模式

來源:互聯網
上載者:User

JSP Model模式,jspmodel模式

用JSP開發的Web應用程式模型可以分為Model1和Model2

對於小型的Web應用,通常可以使用模型1來完成。

模型1可以分為兩種方式:

一種是完全使用JSP頁面來開發Web應用;

另一種是使用JSP頁面和JavaBean相結合的方式。由JSP頁面來接收用戶端請求,用JavaBean或其它服務來完成商務邏輯和產生返回頁面。

實戰:Model1模式錄入商品資訊

1、構建javabean對象(Goods.java)

package com.wuyudong;public class Goods {    private String name;    private double price;    private String description;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getPrice() {        return price;    }    public void setPrice(double price) {        this.price = price;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }    }

2、建立GoodsDao類來封裝對資料庫的操作

import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement; public class GoodsDao {    public void saveGoods(Goods goods) {        try {            Class.forName("com.mysql.jdbc.Driver");            String url = "jdbc:mysql://localhost:3306/db_database05";            Connection conn = DriverManager.getConnection(url, "root",                    "wuyudong");            String sql = "insert into tb_goods(name,price,description)values(?,?,?)";            PreparedStatement ps = conn.prepareStatement(sql);            ps.setString(1, goods.getName());            ps.setDouble(2, goods.getPrice());            ps.setString(3, goods.getDescription());            ps.executeUpdate();            ps.close();            conn.close();        } catch (Exception e) {            e.printStackTrace();        }    } }

index.jsp檔案

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Insert title here</title></head><body>    <form action="service.jsp" method="post" onsubmit="return save(this);">        <table border="1" align="center" width="300">            <tr>                <td align="center" colspan="2"><br>                <h1>錄入商品資訊</h1></td>            </tr>            <tr>                <td align="right">商品名稱</td>                <td><input type="text" name="name"></td>            </tr>            <tr>                <td align="right">價 格:</td>                <td><input type="text" name="price"></td>            </tr>            <tr>                <td align="right">商品描述:</td>                <td><textarea rows="3" cols="30" name="description"></textarea></td>            </tr>            <tr>                <td align="center" colspan="2"><input type="submit" value="提 交">                    <input type="reset" value="重 置"></td>            </tr>        </table>    </form></body></html>

service.jsp用於處理表單請求並向資料庫中添加資料

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Insert title here</title></head><body>    <%        request.setCharacterEncoding("GBK");    %>    <jsp:useBean id="goods" class="com.wuyudong.Goods"></jsp:useBean>    <jsp:setProperty property="*" name="goods" />    <jsp:useBean id="goodsDao" class="com.wuyudong.GoodsDao"></jsp:useBean>    <%        goodsDao.saveGoods(goods);    %>    <a href="index.jsp">返回添加頁面</a></body></html>

在模型2中,使用了JSP、JavaBean和Servlet三種技術。

其中,Servlet扮演一個控制者的角色。

在模型2中,JSP檔案不再處理商務邏輯。  它的主要功能是產生返回給用戶端的網頁。

在模型2中,各個開發人員的角色劃分非常明確。  因此,對於複雜的Web應用開發,使用模型2的優點不言而喻。

(但對於小型應用,還是使用模型1更簡單)

MVC模型

MVC模型是一種將應用分解成三個獨立部分的通用模型(並不單指JSP應用)。

這三個部分分別是:

模型(Model):描述系統的資料

視圖(view):資料的顯示,包括圖形、文本和  檔案輸出等;

控制器(Controller):擷取系統的輸入,控制系  統的執行;

JSP模型2其實就是一種MVC體繫結構,也稱為MVC模型2。

其中,Servlet處理所有請求,並執行商務邏輯,相當於控制器(Controller)的作用;

而JavaBeans用於操作各種資料和對象,相當於模型(Model)。

JSP檔案用於產生返回給用戶端的頁面,則相當於視圖組件(View)。

實戰:Model2錄入商品資訊

index.jsp

<body>        <form action="GoodsServlet" method="post" onsubmit="return save(this);">            <table border="1" align="center" width="300">                <tr>                    <td align="center" colspan="2">                        <br><h1>錄入商品資訊</h1>                    </td>                </tr>                <tr>                    <td align="right">商品名稱:</td>                    <td><input type="text" name="name"></td>                </tr>                <tr>                    <td align="right">價 格:</td>                    <td><input type="text" name="price"></td>                </tr>                <tr>                    <td align="right">商品描述:</td>                    <td><textarea name="description" cols="30" rows="3"></textarea></td>                </tr>                <tr>                    <td align="center" colspan="2">                        <input type="submit" value="提 交">                        <input type="reset" value="重 置">                    </td>                </tr>            </table>        </form>    </body>

GoodsServlet類

package com.wuyudong; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class GoodsServlet extends HttpServlet {    /**     *      */    private static final long serialVersionUID = 291800654190966979L;     @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        resp.setContentType("text/html");        resp.setCharacterEncoding("GBK");        req.setCharacterEncoding("GBK");         PrintWriter out = resp.getWriter();         String name = req.getParameter("name");        String price = req.getParameter("price");        String description = req.getParameter("description");         Goods goods = new Goods();        goods.setName(name);        goods.setPrice(Double.valueOf(price));        goods.setDescription(description);         GoodsDao goodsDao = new GoodsDao();        goodsDao.saveGoods(goods);        out.print("儲存商品成功!");        out.flush();        out.close();    }}

web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <servlet>    <servlet-name>GoodsServlet</servlet-name>    <servlet-class>com.wuyudong.GoodsServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>GoodsServlet</servlet-name>    <url-pattern>/GoodsServlet</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.