jsp 實現(xiàn)的簡易mvc模式示例
本文實例講述了jsp 實現(xiàn)的簡易mvc模式。分享給大家供大家參考,具體如下:
jsp : java servlet page
MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫,一種軟件設(shè)計典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼,將業(yè)務(wù)邏輯聚集到一個部件里面,在改進和個性化定制界面及用戶交互的同時,不需要重新編寫業(yè)務(wù)邏輯。MVC被獨特的發(fā)展起來用于映射傳統(tǒng)的輸入、處理和輸出功能在一個邏輯的圖形化用戶界面的結(jié)構(gòu)中。
示例demo
映射處理
/WEB-INF/web.xml
<?xml version="1.0" encoding="utf-8" ?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Test</display-name> <servlet> <servlet-name>indexServlet</servlet-name> <servlet-class> web.app.controller.IndexController </servlet-class> </servlet> <servlet-mapping> <servlet-name>indexServlet</servlet-name> <url-pattern>/index.do</url-pattern> </servlet-mapping> </web-app>
其中servlet截獲瀏覽器請求,將符合servlet-mapping 中url-pattern 規(guī)則的請求交給servlet-mapping中servlet-name 對應(yīng)的servlet去處理。
設(shè)置好后訪問瀏覽器訪問該項目下的index.do會映射給web.app.controller.IndexController 類處理
IndexController.java
package web.app.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/IndexController") public class IndexController extends HttpServlet { /** * */ private static final long serialVersionUID = -8227194254553105913L; @Override public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { try{ response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //response.getWriter().println("success"); request.setAttribute("test","hello world this is class name index controller"); request.getRequestDispatcher("view/index.jsp").forward(request, response);; }catch(Exception e) { System.out.println("error"); e.printStackTrace(); } } }
注釋:@Override 覆蓋超類中定義的該方法。
所有的servlet類要繼承HttpServlet才能處理request請求。
IndexController 處理完成后將生成的數(shù)據(jù)存儲在request作用域中,然后將該request重定向到view/index.jsp
其中doGet 說明的是接受get請求 request 和 response 是請求request 和 響應(yīng) response
view/index.jsp
<%@ page language="java" pageEncoding="utf-8" contentType="text/html;charset=utf-8" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>index</title> </head> <body> <p><%=request.getAttribute("test") %></p> </body> </html>
從request作用域中獲得后端數(shù)據(jù)。
實現(xiàn)的簡單MVC模式。
希望本文所述對大家jsp程序設(shè)計有所幫助。
相關(guān)文章:
1. JSP的Cookie在登錄中的使用2. JSP開發(fā)之hibernate之單向多對一關(guān)聯(lián)的實例3. jsp EL表達式詳解4. JSP實現(xiàn)客戶信息管理系統(tǒng)5. 基于javaweb+jsp實現(xiàn)企業(yè)財務(wù)記賬管理系統(tǒng)6. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法7. 如何在jsp界面中插入圖片8. asp+jsp+JavaScript動態(tài)實現(xiàn)添加數(shù)據(jù)行9. 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)10. jsp實現(xiàn)簡單用戶7天內(nèi)免登錄
