Stripes最基本的實現是相似於Struts的請求,但沒有Struts那麼配置繁瑣。下面就來看看怎樣實現一個web應用的Action訪問操做。本篇博文爲設計Stripes的三種訪問方式:普通Action的映射訪問、類Servlet訪問方式、Rest變參訪問方式。前幾天還沒將Stripes應用示例打通,今天又本着不解決問題死不罷休的精神繼續去弄,終於搞明白了。css
一、編寫Stripes的web.xml配置文件html
該文件定義了第三方的工具包、過濾器類、以及映射關係和訪問格式;在此配置文件中有兩種Stripes的配置(特定模板的Action過濾配置和動態URL路徑配置),它們互不排斥,相互包容,方便了用戶不一樣業務需求下的訪問模式。其配置內容以下:java
<?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"> <!-- 若是沒使用到,此內容能夠不添加 --> <context-param> <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> <param-value>StripesResources</param-value> </context-param> <!-- 基本ActionBean訪問過濾配置 --> <filter> <display-name>Stripes Filter</display-name> <filter-name>StripesFilter</filter-name> <filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class> <init-param> <param-name>ActionResolver.Packages</param-name> <param-value>com.boonya.stripes.actions</param-value> </init-param> <init-param> <param-name>Interceptor.Classes</param-name> <param-value> net.sourceforge.stripes.integration.spring.SpringInterceptor </param-value> </init-param> </filter> <filter-mapping> <filter-name>StripesFilter</filter-name> <servlet-name>StripesDispatcher</servlet-name> <dispatcher>REQUEST</dispatcher> </filter-mapping> <servlet> <servlet-name>StripesDispatcher</servlet-name> <servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StripesDispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- 動態配置隱射過濾器 --> <filter> <description>Dynamically maps URLs to ActionBeans.</description> <display-name>Stripes Dynamic Mapping Filter</display-name> <filter-name>DynamicMappingFilter</filter-name> <filter-class> net.sourceforge.stripes.controller.DynamicMappingFilter </filter-class> <init-param> <param-name>ActionResolver.Packages</param-name> <param-value>com.boonya.stripes.actions</param-value> </init-param> </filter> <filter-mapping> <filter-name>DynamicMappingFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping> <!-- 歡迎界面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
二、導入須要的jar包到項目WEB-INF的lib目錄下web
此處須要加入的jar包,因爲Stripes實現的時候用到了springframework的jar包,因此須要在項目中添加,不然在訪問頁面時會有異常產生。示例項目目錄結構以下圖:spring
注:上面所示的Jar有個別不須要,若是須要使用Spring整合建議保留;另外,根目錄下下還須要一個消息配置文件StripesResources.properties,缺乏它在數據驗證的時候沒法處理消息信息。app
三、編寫抽象的ActionBean,AbstractActionBeanjsp
由於在每一個ActionBean中都使用到了上下文對象設置,這部分屬於公共行爲,因此將其抽象出來。ide
package com.boonya.stripes.actions; import java.io.Serializable; import net.sourceforge.stripes.action.ActionBean; import net.sourceforge.stripes.action.ActionBeanContext; import net.sourceforge.stripes.action.SimpleMessage; public abstract class AbstractActionBean implements ActionBean, Serializable { private static final long serialVersionUID = -1767714708233127983L; protected static final String ERROR = "/WEB-INF/Error.jsp"; protected transient ActionBeanContext context; protected void setMessage(String value) { context.getMessages().add(new SimpleMessage(value)); } public ActionBeanContext getContext() { return context; } public void setContext(ActionBeanContext context) { this.context = context; } }
注:系統中公用的頁面也能夠設置在這裏,應爲這是全部對象的基本行爲方式和處理邏輯。工具
四、編寫業務須要的ActionBean測試
方式一:基本Action映射訪問
package com.boonya.stripes.actions; import net.sourceforge.stripes.action.DefaultHandler; import net.sourceforge.stripes.action.ForwardResolution; import net.sourceforge.stripes.action.HandlesEvent; import net.sourceforge.stripes.action.SessionScope; @SessionScope public class HelloActionBean extends AbstractActionBean { private static final long serialVersionUID = -8946154817549504368L; protected static final String HELLO ="/WEB-INF/Hello.jsp"; protected static final String BLOG ="/WEB-INF/Blog.jsp"; // 傳遞到指定的頁面的參數 private String name="boonya-only-hellos"; public void setName(String name) { this.name = name; } public String getName() { return name; } /** * 默認跳轉頁面(@DefaultHandler) * @return */ @DefaultHandler public ForwardResolution view() { return new ForwardResolution(HELLO); } /** * 指定跳轉頁面(@HandlesEvent)處理,能夠不添加該註解 * @return */ @HandlesEvent("viewToBlog") public ForwardResolution viewToBlog() { this.name=this.getClass().getName(); return new ForwardResolution(BLOG); } }
方式二:類Servlet訪問方式
package com.boonya.stripes.actions; import net.sourceforge.stripes.action.ActionBeanContext; import net.sourceforge.stripes.action.DefaultHandler; import net.sourceforge.stripes.action.ForwardResolution; import net.sourceforge.stripes.action.HandlesEvent; import net.sourceforge.stripes.action.UrlBinding; // URL 隱射路徑 @UrlBinding("/hellos") public class WorldActionBean extends AbstractActionBean { private static final long serialVersionUID = 3073270783412011577L; protected static final String WORLD ="/WEB-INF/World.jsp"; protected static final String BLOG ="/WEB-INF/Blog.jsp"; // 傳遞到指定的頁面的參數 private String name="boonya-page"; protected transient ActionBeanContext context; @Override public ActionBeanContext getContext() { return context; } @Override public void setContext(ActionBeanContext context) { this.context = context; } public void setName(String name) { this.name = name; } public String getName() { return name; } /** * 默認跳轉頁面(@DefaultHandler) * @return */ @DefaultHandler public ForwardResolution view() { return new ForwardResolution(WORLD); } /** * 指定跳轉頁面(@HandlesEvent)處理,能夠不添加該註解 * @return */ @HandlesEvent("viewToBlog") public ForwardResolution viewToBlog() { this.name=this.getClass().getName(); return new ForwardResolution(BLOG); } }
方式三:Rest風格變參訪問
package com.boonya.stripes.actions; import net.sourceforge.stripes.action.DefaultHandler; import net.sourceforge.stripes.action.ForwardResolution; import net.sourceforge.stripes.action.HandlesEvent; import net.sourceforge.stripes.action.UrlBinding; // URL 隱射路徑 @UrlBinding("/helloworld/{name}") public class HelloWorldActionBean extends AbstractActionBean { private static final long serialVersionUID = 2276458379123668500L; protected static final String WORLD ="/WEB-INF/HelloWorld.jsp"; protected static final String BLOG ="/WEB-INF/Blog.jsp"; // 傳遞到指定的頁面的參數 private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } /** * 默認跳轉頁面(@DefaultHandler) * @return */ @DefaultHandler public ForwardResolution view() { return new ForwardResolution(WORLD); } /** * 指定跳轉頁面(@HandlesEvent)處理,能夠不添加該註解 * @return */ @HandlesEvent("viewToBlog") public ForwardResolution viewToBlog() { this.name=this.getClass().getName(); return new ForwardResolution(BLOG); } }
五、編寫測試JSP頁面
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style type="text/css"> body{ text-decoration: underline; color: blue; font-size: 12px; font-family: '仿宋體'; } </style> </head> <body> <h3> <a href="actions/Hello.action">forward風格測試</a></h3> <h3> <a href="hellos">Servlet風格測試</a></h3> <h3> <a href="helloworld/boonya-test-rest">Rest風格的測試</a></h3> </body> </html>
Hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <title>Hello-page</title> <body> Hello ${actionBean.name} <br /> <br /> <stripes:link beanclass="com.boonya.stripes.actions.HelloActionBean"> <stripes:param name="name" value="boonya-hello" />Try again</stripes:link> <br> <stripes:link beanclass="com.boonya.stripes.actions.HelloActionBean" event="viewToBlog"> from Hello-page to visit boonya's blog </stripes:link> <br> </body> </html>
World.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <title>World-page</title> <body> Hello ${actionBean.name} <br /> <br /> <stripes:link beanclass="com.boonya.stripes.actions.WorldActionBean"> <stripes:param name="name" value="boonya-world" />Try again</stripes:link> <br> <stripes:link beanclass="com.boonya.stripes.actions.WorldActionBean" event="viewToBlog"> from World-page to visit boonya's blog </stripes:link> <br> </body> </html>
HelloWorld.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <title>HelloWorld-page</title> <body> Hello ${actionBean.name} <br /> <br /> <stripes:link beanclass="com.boonya.stripes.actions.HelloWorldActionBean"> <stripes:param name="name" value="boonya-world" />Try again</stripes:link> <br> <stripes:link beanclass="com.boonya.stripes.actions.HelloWorldActionBean" event="viewToBlog"> from HelloWorld-page to visit boonya's blog </stripes:link> <br> </body> </html>
Blog.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Blog-Page</title> </head> <body> <h3>welcome to boonya's blog.</h3> <h2 style="color: blue;">from action's class name: <font color="red">${actionBean.name}</font> </h2> </body> </html>
Error.jsp沒什麼特別的內容故不在此處寫出。
六、項目部署測試訪問
如下是測試訪問的效果圖:
系統首頁顯示:
forward顯示:
servlet顯示:
rest顯示: