web.xml中,只需配置過濾器便可,如圖:java
完整的web.xml配置以下:web
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <filter> <filter-name>xxmvc</filter-name> <filter-class>xx.mvc.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>xxmvc</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <filter-mapping> <filter-name>xxmvc</filter-name> <url-pattern>/$xx/mvc/*</url-pattern> </filter-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
建立一個頁面index.jsp,放在根路徑下。頁面內容以下:session
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>XingXing MVC Demo</title> </head> <body> <h2>${word}</h2> <p> 用法說明. 每個被xingxing mvc 過濾的頁面,都可有一個form類來接受頁面輸入,並處理頁面輸出.form類放在forms包中. </p> <hr/> </body> </html>
建立一個form類,名稱跟頁面名同樣,也叫index,固然文件後綴是java,所在包爲forms。爲這個form類創建頁面加載事件 onLoad,這是一個方法,參數是兩個map,第一個map是request對象的map,第二個map是session的map。返回參數爲 void,表示返回頁面即用戶請求的頁面,沒有跳轉或重置。架構
forms.index.java代碼.mvc
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package forms; import java.util.Map; /** * * @author hicen */ public class index { public void onLoad(Map mapRequest,Map mapSession) { mapRequest.put("word", "Hello world! This is XingXingMVC.");//至關於request.setAttribute("word","Hello world! This is XingXingMVC."); } }
運行,結果以下。app